all groups > flash actionscript > october 2005 >
You're in the

flash actionscript

group:

Program Flow / Variable Assignment Gone Crazy


Program Flow / Variable Assignment Gone Crazy cjreynolds
10/11/2005 10:52:08 PM
flash actionscript: I'm writing a game - a space ship dodging asteroids. The ship and asteroid are
movie clips. I randomly scale and then randomly move four instances of asteroid
across the screen toward the ship, and use a hit test in the asteroid clip to
detect collision with the ship.

I have a variable called livesLeft, that decrements each time the number of
hits exceeds 10. livesLeft starts out as "5". When it is below 1, it should
direct the main timeline to frame 2, which is a "Game Over" screen.

The code is all contained in the "asteroid" movie clip, in four frames.

FRAME 1 CODE (object - motionSphere_mc):

livesLeft = 5;
_root.lifeScore = 5; // temporary variable for troubleshooting -
displayed in the main timeline to monitor "livesLeft" variable
numHits = 0;

FRAME 2 CODE (object - motionSphere_mc, frame label - "initialize"):

setProperty("", _y, random(300));
setProperty("", _x, -30);
xStep = Number(random(50))+20;
setProperty("", _width, random(50)+50);
setProperty("", _height, random(70)+20);

FRAME 3 CODE (object - motionSphere_mc, frame label - "anime"):

setProperty("", _x, Number(getProperty("", _x))+Number(xStep));
if (_root.ship.hitTest(this)) {
if(!collision) {
_root.lifeScore = livesLeft;
if (numHits<10) {
numHits = numHits+1;
with (_root.ship) {
gotoAndPlay(2);
}
} else {
with (_root.ship) {
gotoAndPlay(5);
}
livesLeft = livesLeft-1;
if (livesLeft < 5) {
setProperty("_root.life1", _visible, false);
}
if (livesLeft < 4) {
setProperty("_root.life2", _visible, false);
}
if (livesLeft < 3) {
setProperty("_root.life3", _visible, false);
}
if (livesLeft < 2) {
setProperty("_root.life4", _visible, false);
}
if (livesLeft<1) {
gotoAndPlay("Scene 1", 2);
}
numHits = 0;
}
collision = true;
} else {
collision = false;
}
}

FRAME 4 CODE (motionSphere_mc):

if (Number(getProperty("", _x))>550) { // this returns the asteroid
to the left side of the stage when it exits the right side
gotoAndPlay("initialize");
} else {
gotoAndPlay("anime");
}

"initialize" and "anime" are frames one and two of the clip.

The only time I manipulate the "livesLeft" variable is in frame 3, where I
decrement the value by 1 every time hit count > 10. Yet the value goes from 5
to 4 to 3 to 4 to 5 to 2 to 3, etc.. and also, even though the value falls
below 1 (0, -1...), it never sends the main timeline to frame 2 (Game Over
screen).

Why are seemingly straightforward commands being ignored?

joe
Re: Program Flow / Variable Assignment Gone Crazy cjreynolds
10/12/2005 12:00:00 AM
The more I work on this, the more baffled I become - I've been programming for
20+ years, and I'm not sure I like actionscript at all. It seems to ignore "if"
statements at will, and as best I can tell, program flow doesn't neccessarily
go to the exact frame it is sent to! It even seems that it arbitrarily adds
numbers when told to subtract!! Am I missing something really basic here, or is
this an extremely flakey language!!??

Here is the latest code in frame 3 of the motionSphere_mc object:

setProperty("", _x, Number(getProperty("", _x))+Number(xStep));
if (_root.ship.hitTest(this)) {
if (!collision) {
if (numHits<10) {
numHits = numHits+1;
with (_root.ship) {
gotoAndPlay(2);
}
} else {
with (_root.ship) {
gotoAndPlay(5);
}
livesLeft = livesLeft-1;
_root.lifeScore = livesLeft;
if (livesLeft<5) {
setProperty("_root.life1", _visible, false);
}
if (livesLeft<4) {
setProperty("_root.life2", _visible, false);
}
if (livesLeft<3) {
setProperty("_root.life3", _visible, false);
}
if (livesLeft<2) {
setProperty("_root.life4", _visible, false);
}
if (livesLeft==0) {
gotoAndPlay("gameOver");
}
numHits = 0;
}
collision = true;
} else {
collision = false;
}
}

END OF CODE

Given this code, and the code from frames 1, 2, and 4 (see previous post
above) why in the world would the livesLeft variable turn around halfway
through play and start adding to the variable instead of subtracting, then turn
around again and decrement as it should? Then, when it DOES finally reach a
value of "0", the "if" statement :

if (livesLeft==0)

seems to be flatly ignored.

I've uploded the game to my website
-http://www.cjreynolds.com/robertfogt/roids.fla to download the source.

Thanks,

joe
Re: Program Flow / Variable Assignment Gone Crazy kglad
10/12/2005 12:00:00 AM
i suspect your logic is faulty. why don't you try one asteroid instnace on
stage and see if things work the way you want. if it doesn't then you know you
have some problem like "gameOver" not being a frame label on the asteroid
movieclip.

if it does work the way you want when you have one asteroid instance then you
know that your code from one instance is conflicting with other asteroid
instances.
Re: Program Flow / Variable Assignment Gone Crazy mandingo
10/12/2005 12:00:00 AM
I haven't been able to tes this... but try:

setProperty("", _x, Number(getProperty("", _x))+Number(xStep));
if (_root.ship.hitTest(this)) {
if (!collision) {
if (numHits<10) {
numHits ++;
_root.ship.gotoAndPlay(2);
} else {
_root.ship.gotoAndPlay(5);
}
_root.livesLeft --;

switch(_root.livesLeft){
case 4:
_root.life1._visible = false;
break;
case 3:
_root.life2._visible = false;
break;
case 2:
_root.life3._visible = false;
break;
case 1:
_root.life4._visible = false;
break;
case 0:
_root.gotoAndPlay("gameOver");
break;
}
numHits = 0;
}
collision = true;
} else {
collision = false;
}
}


cheers
Re: Program Flow / Variable Assignment Gone Crazy mandingo
10/12/2005 1:02:30 AM
I haven't looked at all your logic...

BUT... don't use scene labels in your gotoAndPlay() actions... they don't
work.


Scenes actually don't exist after the compile of the SWF so references to
these can cause issues were they are not continuous to the normal timeline...
give your frame a good frameLabel and change your action to

gotoAndPlay("gameOVERFrame");

cheers
AddThis Social Bookmark Button