• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker SOLVED : Sprites animations and bullets

L

Larukuchi

Guest
Hi guys,

I facing an issue since yesterday and I can't figure out how to solve it ...
I hope someone will be able to help me :)
I would like to say that I'm a a beginner with Game maker.

This is my problem :

My player have multiple states set by enum in its create event.
One of my state is player.bomb allowing the player to shoot a fire ball like a bullet, and this the code inside :

Code:
/// @description Bomb state
  
var _bullet = instance_create_layer(x, y-10, "Instances", o_bomb);
audio_play_sound(a_set_bomb, 5,false);

image_speed = 1;

if direction_facing_ == dir.up {
    _bullet.image_angle = 90;
    _bullet.direction = 90;
             
}

if direction_facing_ == dir.right {
    _bullet.image_angle = 360;
    _bullet.direction = 360;
       
}
if direction_facing_ == dir.down {
    _bullet.image_angle = 270;
    _bullet.direction = 270;
       
}

if direction_facing_ == dir.left {
    _bullet.image_angle = 180;
    _bullet.direction = 180;
       
}

state_ = player.move;

The behavior is ok, but the sprites showing the animation of the player shooting isn't ok, I mean we just can see one frame on six normally set in the sprite lookup table corresponding to the player.bomb state.

If I ad this code before "state_ = player.move", the animations will be fine but the bullet are now multiplied by x and the player get stuck in the animation.

Code:
if animation_hit_frame(-1){

state_ = player.move;

}
Have some ideas ?

Issue bullet orion.png
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
You do currently change the state yourself as soon as player enters it, as seen at
Code:
state_ = player.move;
Assuming that you are using HeartBeast's scripts for animation,
Code:
///animation_hit_frame(frame)
var frame = argument0; // The frame to check for
return (image_index >= frame+1 - image_speed) && (image_index < frame+1);
Code:
///animation_end()
return animation_hit_frame(image_number - 1);
You could add a "bombend" state, which would have
Code:
if (animation_end()) {

    state_ = player.move;

}
inside, and have the player change state to that instead of player.move from bomb state.

This way upon entering the bomb state the player would fire a projectile and enter the post-state for displaying the animation correctly.
 
L

Larukuchi

Guest
Hi

Thank you for your reply !
Actually I followed the heartbeats tutorial for top down rpg like Zelda.
I will try what you advise tomorrow ! :)
I’ll get back to you when it’s done to keep you update.

Thx again


You do currently change the state yourself as soon as player enters it, as seen at
Code:
state_ = player.move;
Assuming that you are using HeartBeast's scripts for animation,
Code:
///animation_hit_frame(frame)
var frame = argument0; // The frame to check for
return (image_index >= frame+1 - image_speed) && (image_index < frame+1);
Code:
///animation_end()
return animation_hit_frame(image_number - 1);
You could add a "bombend" state, which would have
Code:
if (animation_end()) {

    state_ = player.move;

}
inside, and have the player change state to that instead of player.move from bomb state.

This way upon entering the bomb state the player would fire a projectile and enter the post-state for displaying the animation correctly.
 
L

Larukuchi

Guest
Hello there,

So I tried to ad a bombend state but it seems it didn't work correctly. I had other issues crashing the game do this way but maybe I missed something...
Anyway I came back to the basics editing once again the code inside the bomb state, applying

///animation_end()
return animation_hit_frame(image_number - 1);

before

state_ = player.move

So now the character's animation is fine and he doesn't get stuck anymore in this infinite state.
The problem is about the bullet once again... during one frame of animation (6 frames for all the spell animation) there are 5 or 6 bullets created...( see picture attached). I need only one bullet actually.

To summarize, this code allow me to get everything well except too many bullets are created :

/// @description Bomb state

var _bullet = instance_create_layer(x, y-10, "Instances", o_bomb);
audio_play_sound(a_set_bomb, 5,false);

image_speed = 1;

if direction_facing_ == dir.up {
_bullet.image_angle = 90;
_bullet.direction = 90;

}

if direction_facing_ == dir.right {
_bullet.image_angle = 360;
_bullet.direction = 360;

}
if direction_facing_ == dir.down {
_bullet.image_angle = 270;
_bullet.direction = 270;

}

if direction_facing_ == dir.left {
_bullet.image_angle = 180;
_bullet.direction = 180;

}

if animation_hit_frame(image_number-1){
state_ = player.move;
}


And this code allow me to get only one bullet as I want, but we can't see the player's spell animation :

/// @description Bomb state

var _bullet = instance_create_layer(x, y-10, "Instances", o_bomb);
audio_play_sound(a_set_bomb, 5,false);

image_speed = 1;

if direction_facing_ == dir.up {
_bullet.image_angle = 90;
_bullet.direction = 90;

}

if direction_facing_ == dir.right {
_bullet.image_angle = 360;
_bullet.direction = 360;

}
if direction_facing_ == dir.down {
_bullet.image_angle = 270;
_bullet.direction = 270;

}

if direction_facing_ == dir.left {
_bullet.image_angle = 180;
_bullet.direction = 180;

}

state_ = player.move;


I don't get it... :-(

If you have ideas let me know !

Thx

You do currently change the state yourself as soon as player enters it, as seen at
Code:
state_ = player.move;
Assuming that you are using HeartBeast's scripts for animation,
Code:
///animation_hit_frame(frame)
var frame = argument0; // The frame to check for
return (image_index >= frame+1 - image_speed) && (image_index < frame+1);
Code:
///animation_end()
return animation_hit_frame(image_number - 1);
You could add a "bombend" state, which would have
Code:
if (animation_end()) {

    state_ = player.move;

}
inside, and have the player change state to that instead of player.move from bomb state.

This way upon entering the bomb state the player would fire a projectile and enter the post-state for displaying the animation correctly.
 

Attachments

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
To summarize, this code allow me to get everything well except too many bullets are created :
Assuming that you reset image_index to 0 before changing the state to bomb state, could you only spawn the bullet if image_index == 0?
 
L

Larukuchi

Guest
Assuming that you reset image_index to 0 before changing the state to bomb state, could you only spawn the bullet if image_index == 0?
Ahah :D, with condition image_index == 0 to shoot the bullet, I have now the animation well displayed and only one bullet, so it's pretty cool thank you ! The only remaining problem is that now, if i pressed in the same time, a direction plus the bullet button, only the spell animation is displayed but the bullet doesn't spawn...
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Any ideas ? :)
It is not possible to make any further guesses without knowing how you manipulate image_index when switching between states. You can definitely try setting image_index to 0 right before you switch to bomb state though.
 
Last edited:
L

Larukuchi

Guest
It is not possible to make any further guesses without knowing how you manipulate image_index when switching between states. You can definitely try setting image_index to 0 right before you switch to bomb state though.
Ok thx,
Actually like you said I'm using scriptes by Heartbeast.

For exemple at the end of my sword state, to go back to the move state I have this line of code :

///
If animation_hit_frame (image_number - 1) {
state_ = player.move;
}

Same to switch between dash state and move state.

So I thought it will be ok for the bomb state to do the same, but I guess as I have another sprite to manipulate inside this state (the bullet sprite) it cause me some issues that I don't have in the other states.
In the bomb state I have a sprite assign to the state (the spell animation) and when I'm entering in this state, I create my bullet object which has another sprite assigned to it.
 
L

Larukuchi

Guest
Ok thx,
Actually like you said I'm using scriptes by Heartbeast.

For exemple at the end of my sword state, to go back to the move state I have this line of code :

///
If animation_hit_frame (image_number - 1) {
state_ = player.move;
}

Same to switch between dash state and move state.

So I thought it will be ok for the bomb state to do the same, but I guess as I have another sprite to manipulate inside this state (the bullet sprite) it cause me some issues that I don't have in the other states.
In the bomb state I have a sprite assign to the state (the spell animation) and when I'm entering in this state, I create my bullet object which has another sprite assigned to it.
I precise that this is the code inside the script :

///
var _frame = argument0;
var _speed = global.one_second/sprite_get_speed(sprite_index);

return (image_index >= _frame+1 - image_speed/_speed) and (image_index < _frame+1);
 
L

Larukuchi

Guest
It is not possible to make any further guesses without knowing how you manipulate image_index when switching between states. You can definitely try setting image_index to 0 right before you switch to bomb state though.
I know now for sure that the problem is : Still I'm in the spell animation I shoot bullets. So if I set the following condition : if image_index == 0 { shoot bullet }, the problem is solved in part, BUT it's working only if I don't press movement key, which is quite weird. I have to move, wait, then shoot, then move, then wait, then shoot...I would like the bullet spawns even if I'm pressing a direction key. But I don't see how to do it. :-(
 
L

Larukuchi

Guest
It is not possible to make any further guesses without knowing how you manipulate image_index when switching between states. You can definitely try setting image_index to 0 right before you switch to bomb state though.
If I set this : if image_index >= 0 && image_index <= 1 { shoot bullets} I spawn 2 or 3 bullets...because there is the time in this range of frame to spawn 2 or 3 bullets...I'm getting tired... :-/
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Ok thx,
Actually like you said I'm using scriptes by Heartbeast.

For exemple at the end of my sword state, to go back to the move state I have this line of code :

///
If animation_hit_frame (image_number - 1) {
state_ = player.move;
}

Same to switch between dash state and move state.

So I thought it will be ok for the bomb state to do the same, but I guess as I have another sprite to manipulate inside this state (the bullet sprite) it cause me some issues that I don't have in the other states.
In the bomb state I have a sprite assign to the state (the spell animation) and when I'm entering in this state, I create my bullet object which has another sprite assigned to it.
As I have said, try doing
Code:
image_index = 0;
prior to changing to bomb state via
Code:
state_ = player.bomb;
or similar.
 
L

Larukuchi

Guest
As I have said, try doing
Code:
image_index = 0;
prior to changing to bomb state via
Code:
state_ = player.bomb;
or similar.
Hey thank you for your reply.
Actually I already tried that but same results.

In my move state which is the "central state", I check for an object and a keyboard pressed to change my move state to bomb.state, I aded inside it, "image index = 0"; without any results.

Here is my move state code :

Code:
/// @description Move State


image_speed = 0;

var _x_input = o_input.right_ - o_input.left_;
var _y_input = o_input.down_ - o_input.up_;
var _input_direction = point_direction( 0, 0,_x_input, _y_input);
var _create_kardia_ = keyboard_check_pressed(vk_control);
var _sword = keyboard_check_pressed(vk_alt);
var _dash = keyboard_check_pressed (vk_shift);
var _switch_to_magic = keyboard_check_pressed (vk_control);
var _magic = keyboard_check(vk_space);


// Movement code

roll_direction_ = direction_facing_*90;


if _x_input == 0 && _y_input == 0 {
    image_speed = 0;
    image_index = 0;
    apply_friction_to_movement_entity ();
    
 
    
}else{
    image_speed = 0.6;

    get_direction_facing (_input_direction);
    add_movement_maxspeed(_input_direction, acceleration_, max_speed_);
    roll_direction_ = direction_facing_*90;
    
        
}


// Create kardia object and trigger animation to switch sword to magic

if _switch_to_magic == true {
    state_ = player.switch_sword;
    instance_create_layer(x,y, "Instances",o_kardia_invisible);
with (instance_create_layer(o_player.x,o_player.y,"Instances",o_kardia_player)) {
    follow = other.id; // Kardia floating around the player
    }
    image_index = 0;
    image_speed = .02;
}
  
// Switch to sword state and destroy Kardia object

if _sword == true {
    state_ = player.switch_kardia;
    instance_destroy (o_kardia_invisible);
    
}

// Switch to magic state (bomb.state)

if !instance_exists(o_kardia_invisible) {
    _magic = false;
    if _magic == true {
    state_ = player.bomb;
}
}
    
// Use item

inventory_use_item(_sword, global.item[0]);
inventory_use_item(_dash, global.item[2]);
inventory_use_item_dash_magic(_dash, global.item[2]);
inventory_use_item_magic(_magic, global.item[3]);


move_movement_entity(false);
 
L

Larukuchi

Guest
As I have said, try doing
Code:
image_index = 0;
prior to changing to bomb state via
Code:
state_ = player.bomb;
or similar.
Hi there !

So finally here is a solution that is working.

I moved the bomb state code which spawns the bullet inside an alarm event.
I set this alarm to 2, inside the bomb state.
I inverted my sprite animation to fit exactly to the bullet timing.

Well... I would like to thank you because you tried to help me ! I wasn't alone and in a sense it help me to go further ;-)

See Ya
 
Top