Be a Supporter!

Script Crashing Flash?

  • 276 Views
  • 8 Replies
New Topic Respond to this Topic
dylan-double-c
dylan-double-c
  • Member since: Mar. 5, 2008
  • Offline.
Forum Stats
Member
Level 17
Musician
Script Crashing Flash? 2012-05-23 10:59:06 Reply

onClipEvent(enterFrame){
	if(Key.isDown(Key.LEFT)){
		this._x -= 3;
	}
	if(Key.isDown(Key.RIGHT)){
		this._x += 3;
	}
	while(_root.spike.hitTest(_x,_y,true)){
		_root.health.nextFrame();
	}
}

My three MovieClips are a moving character, a stationary circle (instance name: spike) and a rectangle with 100 frames of decreasing horizontal size (instanced: health).
The plan is to have "health" move closer to the hundredth frame when the first two clips touch.
However, when my first two clips touch, Flash freezes, and later has a window telling me the script is screwing things up.

So I have two questions. Most importantly, what is wrong in my script? It's not doing the intended job.
Secondarily, why does this crash Flash?

AS2
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Script Crashing Flash? 2012-05-23 11:11:27 Reply

At 5/23/12 10:59 AM, dylan-double-c wrote: onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
this._x -= 3;
}
if(Key.isDown(Key.RIGHT)){
this._x += 3;
}
while(_root.spike.hitTest(_x,_y,true)){
_root.health.nextFrame();
}
}

My three MovieClips are a moving character, a stationary circle (instance name: spike) and a rectangle with 100 frames of decreasing horizontal size (instanced: health).
The plan is to have "health" move closer to the hundredth frame when the first two clips touch.
However, when my first two clips touch, Flash freezes, and later has a window telling me the script is screwing things up.

So I have two questions. Most importantly, what is wrong in my script? It's not doing the intended job.
Secondarily, why does this crash Flash?

AS2

A while loop continues until it's broken, or if the condition is no longer met. When neither of these occur, you've caused an infinite loop which will eventually crash the player.

Imagine the scenario where your spike movieclip collides with the other movieclip, the while loop will continue forever until you stop the that condition. How is the condition stopped? The loop is occurring but there's nothing inside the loop to stop it from meeting the condition.

var i = 0;

while(i < 100)
{
    i++;
}

This loop will continue adding to i until it reaches 100, and then after that, the condition isn't true anymore and the while loop stops. However,

var i = 0;

while(i > 0)
{
    i++;
}

In this example, i will always be over 0, so the loop will continue indefinitely and therefore cause it to crash.

Long post short, you need to use an if statement, break your loop or stop meeting the loops condition for it to not crash.

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to Script Crashing Flash? 2012-05-23 11:14:05 Reply

Don't use while-loops if you don't know how they work. They are the easiest way to crash flash. The while-loop runs as long as the condition stated in it is true, that means you must always have an event inside the while loop that makes the condition eventually false so the loop breaks. You have failed to do so, meaning the while-loop will run forever and flash can't handle forever so it crashes. Read your own code and understand what you have written.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

dylan-double-c
dylan-double-c
  • Member since: Mar. 5, 2008
  • Offline.
Forum Stats
Member
Level 17
Musician
Response to Script Crashing Flash? 2012-05-23 11:16:28 Reply

Long post short, you need to use an if statement, break your loop or stop meeting the loops condition for it to not crash.

That's an awesome explanation, thanks!
Would an }else{ work?

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Script Crashing Flash? 2012-05-23 11:20:28 Reply

At 5/23/12 11:16 AM, dylan-double-c wrote:
Long post short, you need to use an if statement, break your loop or stop meeting the loops condition for it to not crash.
That's an awesome explanation, thanks!
Would an }else{ work?

I try to avoid hitTests - use math. Trust me, it's so much faster (and actually somewhat fun if you like puzzles)

in answer to your question: I would use an if/else if/else


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Script Crashing Flash? 2012-05-23 11:22:29 Reply

At 5/23/12 11:16 AM, dylan-double-c wrote:
Long post short, you need to use an if statement, break your loop or stop meeting the loops condition for it to not crash.
That's an awesome explanation, thanks!
Would an }else{ work?

Simply doing:

if(_root.spike.hitTest(_x,_y,true)){
		_root.health.nextFrame();
	}

Would make the code work how you need it to. You would use an else, if you wanted to something to happen when the spike isn't touching the movieclip. Because you're literally saying, if the spike is touching this, change the health frame, else if the spike ISN'T touching this, then do something else. As an example you could have it regenerate health when not touching the spike, in which case you could do:

if(_root.spike.hitTest(_x,_y,true)){
		_root.health.nextFrame();
	} else {
                _root.health.prevFrame();
}

Because this is saying, if anything else that isn't "spike hittesting this" is happening, then change the health frame to the previous frame.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Script Crashing Flash? 2012-05-23 11:23:02 Reply

ah, man. I hate double-posting :/

Sam, your second example won't work. That while loop only carries out if i is greater than 0. i is equal to 0, so it wouldn't start in the first place. use the >= symbols


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Script Crashing Flash? 2012-05-23 11:24:17 Reply

At 5/23/12 11:23 AM, egg82 wrote: ah, man. I hate double-posting :/

Sam, your second example won't work. That while loop only carries out if i is greater than 0. i is equal to 0, so it wouldn't start in the first place. use the >= symbols

Very true. I just copied and pasted and wasn't exactly thinking - my bad.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Script Crashing Flash? 2012-05-23 11:31:20 Reply

At 5/23/12 11:24 AM, Sam wrote:
At 5/23/12 11:23 AM, egg82 wrote: ah, man. I hate double-posting :/

Sam, your second example won't work. That while loop only carries out if i is greater than 0. i is equal to 0, so it wouldn't start in the first place. use the >= symbols
Very true. I just copied and pasted and wasn't exactly thinking - my bad.

haha, that's fine. We all make mistakes. If we didn't... Well, this world would be very boring o.o

a few tips to the OP:
for health and mana bars, don't just stick a scale animation in some frames and work like that. In short: don't trust frames to act like variables.
Use variables. Trust me, they're your best friend in almost any situation (even animation)
try not to put your code in movieclips. Use a new layer with a new - blank - frame. I put code in buttons, but that's because I really don't have many buttons and they're all easily visible and accessible.
Use functions! They're right up there on the friends list next to variables


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature