01-16-2008, 01:52 PM
|
#1
|
|
Member
Join Date: Sep 2005
Posts: 98
|
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 (falling) checkFall();
// 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.x < 0) heroPos.x = 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.x > worldEnd) heroPos.x = 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].x - pos.x;
var dy = _root["terrain"+i].y - 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.y += fallSpeed;
// complete distance to ground and stop falling
} else {
heroPos.y -= heroBounds.bottom;
fallSpeed = 0;
falling = false;
hero.gotoAndStop("stand"); // stand
}
// see whether hero hits head on box above
if (heroPos.y > heroBounds.top) {
heroPos.y = heroBounds.top;
fallSpeed = 0;
}
}
// utility function to determine actual distance between movie clips
function distance(mc1,mc2) {
d = 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 thanks
|
|
|
01-16-2008, 02:10 PM
|
#2
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
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...
|
|
|
01-16-2008, 02:22 PM
|
#3
|
|
Member
Join Date: Sep 2005
Posts: 98
|
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
|
|
|
01-16-2008, 02:31 PM
|
#4
|
|
Banned by AS.org Staff
Join Date: Jan 2007
Location: Montréal, Québec
Posts: 14,073
|
trace("_root["terrain"+i] = "+ _root["terrain"+i] + " : " + "pos = " + pos);
trace("_root["terrain"+i].x = "+ _root["terrain"+i].x + " : " + "pos.x = " + pos.x);
|
|
|
01-16-2008, 02:32 PM
|
#5
|
|
Senior Member
Join Date: Apr 2006
Location: estonia
Posts: 739
|
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!
|
|
|
01-16-2008, 02:34 PM
|
#6
|
|
Member
Join Date: Sep 2005
Posts: 98
|
Here is flash
Please see attached flash
thanks
|
|
|
01-16-2008, 02:36 PM
|
#7
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
What's the output from those trace() lines???
|
|
|
01-16-2008, 02:38 PM
|
#8
|
|
Member
Join Date: Sep 2005
Posts: 98
|
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
|
|
|
01-16-2008, 02:41 PM
|
#9
|
|
NaN
Join Date: Aug 2004
Location: austria
Posts: 396
|
looks like _root["terrain"+i] is a movieclip, so it's x will be
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.
|
|
|
01-16-2008, 02:54 PM
|
#10
|
|
Member
Join Date: Sep 2005
Posts: 98
|
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
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 10:03 AM.
///
|
|