This site requires JavaScript, please enable it in your browser!
Greenfoot back
Minion1
Minion1 wrote ...

2012/10/29

Causing a process to execute just one time.

Minion1 Minion1

2012/10/29

#
The last time I begged for help here I got it, fast. Real fast in fact. So ta da! Like a trained monkey here I am again to beg for help. I'm working on the asteroids project from the greenfoot book scenarios. I've given my ship a modifier called "Structure" that essentially functions as life. I've made it so that each time the ship hits an asteroid, it's structure is reduced in a blaze of sparks (new images) sound effects (Mega Man X be blessed) and a physical counter on the screen that shows how much structure is left. My problem is this: I want the animation for the damage to execute just one time. Right now, it only executes as long as an asteroid is detected on the rocket. What I want it to do is literally execute the code for about 2 seconds and then revert back to the original image. I can't seem to do it. Anyone every try something like this?
Minion1 Minion1

2012/10/29

#
To be more specific: I have an array with a series of "damaged rocket" picture. When the rocket is struck by an asteroid, I want to see the images of the array to be selected in random order (got that part done just fine) for about 50 cycles of the act command, then stop, regardless of whether or not an asteroid is on the ship.
davmac davmac

2012/10/30

#
What you're talking about is state, and that means variables. The ship can be in one of three main states: - normal (state 0) - animating (state 1) - animation finished (state 2) You could use an 'int' field variable to track which state (0, 1, or 2). Call it 'hitState' or something similar.
int hitState = 0;
State 1 also needs a counter to count how many cycles the animation has run for.
int animationCounter = 0;
When the ship gets hit by an asteroid, you check the hitState. If it is currently 0, you set it to 1, and set animationCounter to 0. During the act cycle, you also check the hitState and, if it is 1 (animating), you select a random image and increment the animationCounter. If the animationCounter reaches 50, you set the image back to the normal image and then set the hitState to 2 (animation finished). Now, if the asteroid is still on the ship, it will have no effect, because hitState isn't 0.
You need to login to post a reply.