GameMaker Sprites and level restart

S

Steve Potts

Guest
Hi guys I have 2 problems I`m stuck with.

1) I have a cannon moving across the screen which fires at random.
The sprite is made up of 9 frames and I need frame 0 when not firing and to run the animation when it fires once. All I can do is have the cannon on frame 0 all the time or run the animation all the time.

2) When a missile hits the player the player, the player and the missile are destroyed and an explosion animation is played , I want to lose a life and restart the level if you have any lives left.
If I run the following code:

instance_destroy() ;
with other instance_destroy () ;
instance_create_layer(x-60,y,"Instances",obj_big_explosion) ;

lives = lives -1 ;
if lives > 0 then room_restart() ;

All is destroyed and the player is respawned but the explosion animation does not play.

If I rem out: if lives > 0 the room_restart() ;
then the explosion plays but obviously the level does not restart.

Any help most appreciated.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
1) Not sure what you mean here, but I'm assuming you only want the canon to fire when the animation has finished. For that you can use an alarm and the animation end event. So, something like this:

Code:
// CREATE
image_speed = 0; // No animation
alarm[0] = room_speed + random(room_speed); // Random shoot interval

// ALARM[0]
image_speed = 1;; Set animation running

// ANIMATION END EVENT
alarm[0] = room_speed + random(room_speed); // Set the canon to fire again
image_speed = 0; // Stop the animation
image_index = 0;
I hope that's what you are wanting, but if not then please elaborate! :)

2) You need to move the code into the explosion object, again use the animation end event, and simply add:

Code:
with (PLAYER OBJECT)
{
lives = lives -1 ;
if lives > 0 then room_restart() else instance_destroy();
}
instance_destroy();
Hope that helps!
 
S

Steve Potts

Guest
Hi again, what I`m doing with the cannon is :
CREATE EVENT : cannonfire = 0 ;
STEP EVENT :
x=x - 2
if x< -15 instance_destroy() ;



cannonfire = cannonfire +1
if irandom(20) = 1 and cannonfire > 100 {
image_speed = 1;
instance_create_layer(x,y,"Instances",obj_shell) ;



cannonfire = 0 ;
image_speed = 0;
image_index = 0 ;
}

The cannon now plays animation once and then fires the shell at random intervals with no animation.
 

TsukaYuriko

☄️
Forum Staff
Moderator
With that code, it should not be possible for the animation to play once. It should start playing as soon as the instance is created, loop until a shell is shot once and then freeze.

Anyway, there is no time allocated for the animation to play once the shell has been shot. Between the line where you set image_speed to 1 and then a few lines later back to 0, not even a single frame passes since code execution doesn't stop in between for any reason. Those two things happen back to back. This code is in a Step event, which means it happens multiple times per second - every step or frame, so it depends on how many frames you've configured your game to run at.

The end result is that after the if block runs, image_speed will be 0 because that's the last value you've set it to. Remove the line that does that. You can then use an Animation End event to set it to 0 in that event. It will trigger once the animation has fully played.
 
Top