Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-16-2008, 01:52 PM   #1
sami_rima
Member
 
Join Date: Sep 2005
Posts: 98
Default Variable returns NAN

Hi,

Please look at this code and let me Know why the variable dy return NAN



PHP Code:
startGame();
stop();
lives=3;
score=0;

function 
startGame() {
    
// establish constants
    
floor 350;
    
heroSpeed 10;
    
worldEnd 2400;
    
jumpPower 60;

    
// initialize hero
    
heroPos = {x:0,y:0};
    
fallSpeed 0;
    
    
    
falling false;
    
    
//hero.swapDepths(999);
    
    // movehero should be called every frame
    
_root.onEnterFrame movehero;
    
    
    
    
}






    
function 
movehero() {
    
// get movement bounds for hero
    
heroBounds determineBounds(heroPos);
    
    
// if no ground under hero, then start falling
    
if ((heroBounds.bottom 0) and (!falling)) falling true;
    
    
// fall
    
if (fallingcheckFall();
    
    
// left arrow, move left if no boundary hit
    
if (Key.isDown(Key.LEFT)) {
        
//if (heroSpeed < heroBounds.left) {
            //heroPos.x -= heroSpeed;
            
hero._x -= heroSpeed;
            
hero.gotoAndStop("run")
        
//}
        
if (heroPos.0heroPos.0;
        
hero._xscale = -55;
        
moving true;
        
    
// right arrow, move right if no boundary hit
    
} else if (Key.isDown(Key.RIGHT)) {
        
//if (heroSpeed < heroBounds.right) {
            //heroPos.x += heroSpeed;
            
hero._x += heroSpeed;
            
            
hero.gotoAndStop("run");
            
        
//}
        
if (heroPos.worldEndheroPos.worldEnd;
        
hero._xscale 55;
        
moving true;
        
    
// must not be moving
    
}
    
    
    
    
    else {
        
moving false;
        
    }
    
    
    
    
// if on the ground and spacebar hit, then jump
    
if (Key.isDown(Key.SPACE) and (!falling) ) {
        
fallSpeed jumpPower// jump = fall up
        
falling true;
        
        if (!
moving ) { // use jump animation only if not moving
            
hero.gotoAndPlay("jump");
        }
    }
    
    
// if moving and not falling, animate
    
if (moving and !falling ) {
        
hero.gotoAndStop("run");
        
    
// if not moving or falling, then show stand frame 
    
} else if (!moving and !falling ) {
        
hero.gotoAndStop("stand");
    }
    
    
    
    
// position hero movie clip vertically
    
hero._y floor heroPos.y;
    
    


function 
determineBounds(pos) {
    
// assume distance bounds, ground is bottom
    
var bounds = {left:1000,right:1000,top:1000,bottom:pos.y};
    
    
// loop through all objects
    
for(var i=0;i<5;i++) {
        
        
// only look at terrain
        
            
var dx _root["terrain"+i].pos.x;
            var 
dy _root["terrain"+i].pos.y;
trace(dy);
            
// if terrain is in same vertical space
            
if ((dy >= 0) and (dy <= 50)) {
                
                
// if terrain to the to left, see whether it is closest so far
                
if ((dx+50 <= 0) and (Math.abs(dx+50) < bounds.left)) {
                    
bounds.left Math.abs(dx+50);
                    
                
// if box is to the right, see whether it is closest so far
                
} else if ((dx >= 0) and (dx bounds.right)) {
                    
bounds.right dx-50;
                }
            }
        
            
// terrain is in same horizontal space
            
if ((dx >= -50) and (dx <= 50)) {
                
                
// if box is below, see whether it is closest so far
                
if ((dy+50 <= 0) and (Math.abs(dy+50) <= bounds.bottom)) {
                    
bounds.bottom Math.abs(dy+50);
                    
                
// if box is above, see whether it is closest so far
                //it was 50
                
} else if ((dy-150 >= 0) and (dy-150 bounds.top)) {
                    
bounds.top dy-50;
                }
            }
        }
    }
    
    return(
bounds);
}

function 
checkFall() {
    
    
// accelerate due to gravity
    
fallSpeed -= 10;
    
    
// room to fall at full speed
    
if (fallSpeed > -heroBounds.bottom) {
        
heroPos.+= fallSpeed;
        
    
// complete distance to ground and stop falling
    
} else {
        
heroPos.-= heroBounds.bottom;
        
fallSpeed 0;
        
falling false;
        
hero.gotoAndStop("stand"); // stand
    
}
    
    
// see whether hero hits head on box above
    
if (heroPos.heroBounds.top) {
        
heroPos.heroBounds.top;
        
fallSpeed 0;
    }
    
}


// utility function to determine actual distance between movie clips
function distance(mc1,mc2) {
    
Math.sqrt(Math.pow(mc1._x-mc2._x,2)+Math.pow(mc1._y-mc2._y,2));
    return 
d;


I know it is a lot of code My interest in
Code:
trace(dy)
thanks
sami_rima is offline   Reply With Quote
Old 01-16-2008, 02:10 PM   #2
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Put these two trace() lines right above that line and tell us what you get...
ActionScript Code:
trace('_root["terrain"+i] = ' = _root["terrain"+i] + " : " + "pos = " + pos); trace('_root["terrain"+i].x = ' = _root["terrain"+i].x + " : " + "pos.x = " + pos.x);

FYI, you can use AS tag instead of PHP tag to format the ActionScript in this forum...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 01-16-2008, 02:22 PM   #3
sami_rima
Member
 
Join Date: Sep 2005
Posts: 98
Default I get an error

Code:
**Error** Scene=Scene 1, layer=hero, frame=1:Line 122: Left side of assignment operator must be variable or property.
     			trace('_root["terrain"+i] = ' = _root["terrain"+i] + " : " + "pos = " + pos);

**Error** Scene=Scene 1, layer=hero, frame=1:Line 123: Left side of assignment operator must be variable or property.
                 trace('_root["terrain"+i].x = ' = _root["terrain"+i].x + " : " + "pos.x = " + pos.x);

Total ActionScript Errors: 2 	 Reported Errors: 2
sami_rima is offline   Reply With Quote
Old 01-16-2008, 02:31 PM   #4
atomic
Banned by AS.org Staff
 
Join Date: Jan 2007
Location: Montréal, Québec
Posts: 14,073
Default

trace("_root["terrain"+i] = "+ _root["terrain"+i] + " : " + "pos = " + pos);
trace("_root["terrain"+i].x = "+ _root["terrain"+i].x + " : " + "pos.x = " + pos.x);
atomic is offline   Reply With Quote
Old 01-16-2008, 02:32 PM   #5
grass
Senior Member
 
Join Date: Apr 2006
Location: estonia
Posts: 739
Default

ActionScript Code:
trace('_root["terrain"+i] = '_root["terrain"+i] + " : " + "pos = " + pos); trace('_root["terrain"+i].x = ' + _root["terrain"+i].x + " : " + "pos.x = " + pos.x);

be creative... but try this again...
__________________
I re-invent wheel to invent bike with 4 asses in the future!
grass is offline   Reply With Quote
Old 01-16-2008, 02:34 PM   #6
sami_rima
Member
 
Join Date: Sep 2005
Posts: 98
Default Here is flash

Please see attached flash
thanks
Attached Files
File Type: zip runningboy3.zip (63.1 KB, 50 views)
sami_rima is offline   Reply With Quote
Old 01-16-2008, 02:36 PM   #7
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

What's the output from those trace() lines???
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 01-16-2008, 02:38 PM   #8
sami_rima
Member
 
Join Date: Sep 2005
Posts: 98
Default The new trace

The result of the new trace:


Code:
_root["terrain"+i] = _level0.terrain1 : pos = [object Object]
_root["terrain"+i].x = undefined : pos.x = 0
why is it looking for Objects?

thanks
sami_rima is offline   Reply With Quote
Old 01-16-2008, 02:41 PM   #9
self
NaN
 
self's Avatar
 
Join Date: Aug 2004
Location: austria
Posts: 396
Default

looks like _root["terrain"+i] is a movieclip, so it's x will be
ActionScript Code:
_root["terrain"+i]._x
try changing that in your code
__________________
please note that i'm using FlashDevelop 4.2.x and the Flex SDK to code, compile and debug AS3. thus, my results might differ from yours.
self is offline   Reply With Quote
Old 01-16-2008, 02:54 PM   #10
sami_rima
Member
 
Join Date: Sep 2005
Posts: 98
Default changed the trace

I changed the trace :

to:

Code:
trace('_root["terrain"+i] = '+  _root["terrain"+i] + " : " + "pos = " + pos);
trace('_root["terrain"+i]._x = ' + _root["terrain"+i]._x + " : " + "pos.x = " + pos.x);
result=

Code:
_root["terrain"+i] = _level0.terrain1 : pos = [object Object]
_root["terrain"+i]._x = -4.9 : pos.x = 0
sami_rima 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
LP Colorpicker biosonik Components 0 03-25-2005 06:37 AM
strange variable behavior flashdudette ActionScript 1.0 (and below) 3 05-09-2003 06:49 PM
checking variable problem flashdudette Server-Side Scripting 0 04-29-2003 05:51 PM
Paths problems or variable problem... Enoch ActionScript 1.0 (and below) 4 04-24-2003 02:02 AM
accessing instance name of MC through a variable. Array? nishr ActionScript 1.0 (and below) 3 03-26-2003 06:42 AM


All times are GMT. The time now is 03:26 AM.

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