Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums [AS3] NaN and my enemys
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

08-21-2010, 10:56 PM   #1
pegasus1820
Registered User

Join Date: Nov 2006
Posts: 10
[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 is the move code in question:

Quote:
 for(var t = 0;t(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 360){ smallEnemys[t].mc.rotation -= 360; } //trace(smallEnemys[t].mc.rotation); } } }

 08-21-2010, 11:38 PM #2 lordofduct Senior Member     Join Date: Feb 2008 Location: West Palm Beach, FL Posts: 3,872 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!
08-21-2010, 11:45 PM   #3
pegasus1820
Registered User

Join Date: Nov 2006
Posts: 10

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 :/

 08-22-2010, 12:06 AM #4 lordofduct Senior Member     Join Date: Feb 2008 Location: West Palm Beach, FL Posts: 3,872 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 (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
 08-22-2010, 01:05 AM #5 pegasus1820 Registered User   Join Date: Nov 2006 Posts: 10 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!

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 06:27 PM.

///