Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-21-2010, 10:56 PM   #1
pegasus1820
Registered User
 
Join Date: Nov 2006
Posts: 10
Default [AS3] NaN and my enemys

Hi all, I am having an odd issue with my code. The idea is when the player is not near the enemy, the enemy moves slowly around the map. When it hits a wall. Flip 135 degrees and add a random number between 0 and 90 to move off the wall. However he just disappears when he hits a wall or the player steps inside and outside the area! When I trace his rotation it returns NaN, which according to the internet means "Not a Number". Anyone have any ideas on what can be making this happen?

This is the constructor:

Quote:
fixedObjects = new Array();
smallEnemys = new Array();
smallEnemysHit = new Array();


for(var i:int=0;i<this.numChildren;i++) {
var mc = this.getChildAt(i);

// add floors and walls to fixedObjects
if (mc is Wall) {
var floorObject:Object = new Object();
floorObject.mc = mc;
floorObject.mc.visible = false;
floorObject.leftside = mc.x;
floorObject.rightside = mc.x+mc.width;
floorObject.topside = mc.y;
floorObject.bottomside = mc.y+mc.height;
fixedObjects.push(floorObject);
} else if(mc is smallEnemy) {

var smallEnemyObj:Object = new Object();
smallEnemyObj.mc = mc;
smallEnemyObj.mc.rotation = Math.floor(randomRange(0,360));
smallEnemys.push(smallEnemyObj);

smallEnemysHit[enemysmallList] = new cbox_smallEnemy();
smallEnemysHit[enemysmallList].x = smallEnemys[smallEnemys.length-1].x;
smallEnemysHit[enemysmallList].y = smallEnemys[smallEnemys.length-1].y;
smallEnemysHit[enemysmallList].visible = true;
enemysmallList++;



}
}

This is the move code in question:

Quote:
for(var t = 0;t<smallEnemys.length;t++) {

if(thisplayer.x<(smallEnemys[t].mc.x+200) && thisplayer.x>(smallEnemys[t].mc.x-200) && thisplayer.y<(smallEnemys[t].mc.y+200) && thisplayer.y>(smallEnemys[t].mc.y-200)) {


trace("ATTACK ATTACK!!!");




} else {
smallEnemys[t].mc.x = smallEnemys[t].mc.x + Math.cos(smallEnemys[t].mc.rotation*(Math.PI/180))*2;
smallEnemys[t].mc.y = smallEnemys[t].mc.y + Math.sin(smallEnemys[t].mc.rotation*(Math.PI/180))* 2 * -1;

//trace("x:" + smallEnemys[t].mc); //*(Math.PI/180))*2);
trace("y:" + smallEnemys[t].mc.rotation); //*(Math.PI/180))* 2 * -1);

smallEnemysHit[t].x = smallEnemys[t].mc.x;
smallEnemysHit[t].y = smallEnemys[t].mc.y;


for(var g:int=0;g<fixedObjects.length;g++) {


if(smallEnemysHit[t].hitTestObject(fixedObjects[g].mc)) {
smallEnemys[t].mc.x = smallEnemys[t].mc.x - Math.cos(smallEnemys[t].mc.rotation*(Math.PI/180))*2;
smallEnemys[t].mc.y = smallEnemys[t].mc.y - Math.sin(smallEnemys[t].mc.rotation*(Math.PI/180))* 2 * -1;

smallEnemysHit[t].x = smallEnemys[t].mc.x;
smallEnemysHit[t].y = smallEnemys[t].mc.y;
//trace("HIT!!!!!!!!!");
smallEnemys[t].mc.rotation = smallEnemys[t].rotation + 135 + randomRange(0,90);
if(smallEnemys[t].mc.rotation > 360){
smallEnemys[t].mc.rotation -= 360;
}

//trace(smallEnemys[t].mc.rotation);


}


}
}
pegasus1820 is offline   Reply With Quote
Old 08-21-2010, 11:38 PM   #2
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

NaN is a state of a floating point value (the Number type) that means it is not a rational value of any sort.

It's part of it's definition. There's several states of the Number object:

positive
negative
zero
rational (a normal numeric value)
positive infinity
negative infinity
and NaN

NaN represents states that just don't make sense... for example a dvision by zero (which is undefined or unknown... in floating point it's NaN).


Somewhere you probably have something like that happening... what's the function 'randomRange' look like? There's probably something happening there.
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!
lordofduct is offline   Reply With Quote
Old 08-21-2010, 11:45 PM   #3
pegasus1820
Registered User
 
Join Date: Nov 2006
Posts: 10
Default

The random range function looks like this:

Quote:
public function randomRange(max:Number, min:Number = 0):Number
{
return Math.floor(Math.random() * (max - min) + min);
}
Also I did a few more traces. I traced each objects X,Y,Rotation, and the value of the math I am using to calc the x and Y move. This is the output I get right before the object disappears:

Quote:
0XMove:NaN
0YMove:NaN
0Rotation:NaN
0x:0
0y:0
-93.14495746469802 ; -0.054862303828868 : + -0.9984939296853982
1XMove:NaN
1YMove:NaN
1Rotation:NaN
1x:0
1y:0
-93.14495746469802 ; -0.054862303828868 : + -0.9984939296853982
2XMove:1.462707403238341
2YMove:-1.363996720124997
2Rotation:43
2x:224.75
2y:-247.65
HIT
-93.14495746469802 ; -0.054862303828868 : + -0.9984939296853982
0XMove:NaN
0YMove:NaN
0Rotation:NaN
0x:0
0y:0
-93.14495746469802 ; -0.054862303828868 : + -0.9984939296853982
1XMove:NaN
1YMove:NaN
1Rotation:NaN
1x:0
1y:0
-93.14495746469802 ; -0.054862303828868 : + -0.9984939296853982
2XMove:NaN
2YMove:NaN
2Rotation:NaN
2x:0
2y:0
It seems the first 2 objects in the array have the NaNs right from the start :/
pegasus1820 is offline   Reply With Quote
Old 08-22-2010, 12:06 AM   #4
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

one thing that helps in debugging these issues is cleaning up your code... so lets sort this up so that it's more readable
ActionScript Code:
for(var t = 0;t<smallEnemys.length;t++) {     var ix:Number = smallEnemys[t].mc.x;     var iy:Number = smallEnemys[t].mc.y;     var ir:Number = smallEnemys[t].mc.rotaiton;     var irad:Number =  ir  * Math.PI / 180;//lets also store a radians version     if(thisplayer.x < (ix+200) && thisplayer.x > (ix - 200) && thisplayer.y < (iy + 200) && thisplayer.y > (iy - 200))     {         trace("ATTACK ATTACK!!!");     } else {         var nx:Number = ix + Math.cos(irad) * 2;         var ny:Number = iy + Math.sin(irad) * -2;         smallEnemysHit[t].x = smallEnemys[t].mc.x = nx;         smallEnemysHit[t].y = smallEnemys[t].mc.y = ny;         for(var g:int=0;g<fixedObjects.length;g++)         {             if(smallEnemysHit[t].hitTestObject(fixedObjects[g].mc))             {                 //smallEnemysHit[t] is redundant, was set 7 lines ago                 smallEnemysHit[t].x = smallEnemys[t].mc.x = nx;                 smallEnemysHit[t].y = smallEnemys[t].mc.y = ny;                 //ALERT!!!!                 //look here, see you don't say smallEnemys[t].mc.rotaiton...                 //you say smallEnemys[t].rotation...                 //smallEnemys[t].mc.rotation = smallEnemys[t].rotation + 135 + randomRange(0,90);                 //that's probably your problem                 //an undefined property coerced as a Number will be NaN                 //so lets change it to use our already stored rotation from above                 var nr:Number = ir + randomRange(135,225); //this was a random RANGE function, so make it a range                 nr = wrap(nr, 360, 0);                 smallEnemys[t].mc.rotation = nr;             }         }     } } /** * wrap a value around a range, similar to modulus with a floating minimum */ function wrap(val:Number, max:Number, min:Number = 0):Number {     val -= min;     max -= min;     if(max == 0) return min;     val %= max;     val += min;     while (val < min)         val += max;                             return val; }


ok, it's a little more readable now... and check out my comments where it say "ALERT"

that's probably your problem
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!

Last edited by lordofduct; 08-22-2010 at 12:09 AM.
lordofduct is offline   Reply With Quote
Old 08-22-2010, 01:05 AM   #5
pegasus1820
Registered User
 
Join Date: Nov 2006
Posts: 10
Default

Thank you sooo much for your help m8, that fixed the major error. I am just having smaller ones with the enemies buggin out once in a while but I think I can get them working. Here is a link to the build:

jvspad.com/spaceShooter8_20.html


Once again thanks!
pegasus1820 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:17 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.