• 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!

Help with basic attacking code

A

Alex hubbard

Guest
So I'm am currently learning the basics of coding, and have so far completed walk left, right as well as jump. And now I am trying to learn how to program the attack animation, nothing more. I would kindly appreciate some knowledge on the subject.

P.s I am very new to programming, so please if possible, it would be very helpful if you followed with an explanation as well. thank you kindly. coding script below

// get player input(Movment)
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump =keyboard_check(vk_up);

//Caculate mopvment
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,objectW)) and (key_jump)
{
vsp = -7;
}
//horizontal collison
if (place_meeting(x+hsp,y,objectW))
{
while (!place_meeting(x+sign(hsp),y,objectW))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;


//Vertical Collision
if (place_meeting(x,y+vsp,objectW))
{
while (!place_meeting(x,y+sign(vsp),objectW))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

//Animation
if (!place_meeting(x,y+1,objectW))
{
sprite_index = sPlayerJ;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1;else image_index = 0;
}
else
{
image_speed = 1;
if (hsp == 0)
{
sprite_index = sPlayer;
}
else
{
sprite_index = sPlayerR;
}
 

woods

Member
there are thousands of ways to handle attacks..
this may not be the fastest or easiest but.. worked for me ;o)

when my player presses the spacebar the axe is drawn in relation to the players facing.
when the animation ends, the axe object is destroyed.



obj_player create event
Code:
dir = 0 // sets the facing of the player to keep track of for later
obj_player step event
Code:
// add this in the movement area of your code.. tells the direction that the player is facing so we can draw the axe proply
if keyboard_check(key_right) 
{ 
dir = 0;    // facing right
}

if keyboard_check(key_left) 
{ 
dir = 1;    // facing left
}


//in the attack area of your code
if keyboard_check_pressed(vk_space)
{
       
if dir = 0 // facing right
{
axe =instance_create(x,y,obj_axe);
axe.image_angle = dir-90;
}
else
    
if dir = 1 // facing left
{
axe =instance_create(x,y,obj_axe);
axe.image_angle = dir-180;
}
}
// you may have to adjust image_angle based on how your sprite is drawn
in my obj_axe other animation end event
Code:
instance_destroy();

next we do a simple collision with axe in the enemy step event to apply damage


hope this helps
 
A

Alex hubbard

Guest
there are thousands of ways to handle attacks..
this may not be the fastest or easiest but.. worked for me ;o)

when my player presses the spacebar the axe is drawn in relation to the players facing.
when the animation ends, the axe object is destroyed.



obj_player create event
Code:
dir = 0 // sets the facing of the player to keep track of for later
obj_player step event
Code:
// add this in the movement area of your code.. tells the direction that the player is facing so we can draw the axe proply
if keyboard_check(key_right)
{
dir = 0;    // facing right
}

if keyboard_check(key_left)
{
dir = 1;    // facing left
}


//in the attack area of your code
if keyboard_check_pressed(vk_space)
{
    
if dir = 0 // facing right
{
axe =instance_create(x,y,obj_axe);
axe.image_angle = dir-90;
}
else
 
if dir = 1 // facing left
{
axe =instance_create(x,y,obj_axe);
axe.image_angle = dir-180;
}
}
// you may have to adjust image_angle based on how your sprite is drawn
in my obj_axe other animation end event
Code:
instance_destroy();


hope this helps
Thank you for you response, the only thing is my charcter is kicking,so can i still use this code?
 

woods

Member
the code i used above is using another object for the "attack hitbox"
...instead of drawing the axe, draw whatever object you want there.. if you are changing the player sprite, to something like player kicking sprite... you would have to do it a little different.

maybe something like this..


psudo code
obj_player create event
Code:
is_kicking = false
obj_player step event
Code:
if keyboard_check(vk_space) //this is the attack button held down
{
is_kicking = true
}


if (is_kicking = true)
{
if (place_meeting(x,y,enemy))
    {
    // code for enemy taking damage. wether it is instance_destroy(other) or enemy.hp-=1 
    // not sure how you have your damage set up
    is_kicking = false
    )
}
 
A

Alex hubbard

Guest
the code i used above is using another object for the "attack hitbox"
...instead of drawing the axe, draw whatever object you want there.. if you are changing the player sprite, to something like player kicking sprite... you would have to do it a little different.

maybe something like this..


psudo code
obj_player create event
Code:
is_kicking = false
obj_player step event
Code:
if keyboard_check(vk_space) //this is the attack button held down
{
is_kicking = true
}


if (is_kicking = true)
{
if (place_meeting(x,y,enemy))
    {
    // code for enemy taking damage. wether it is instance_destroy(other) or enemy.hp-=1
    // not sure how you have your damage set up
    is_kicking = false
    )
}
so my sprite comes with a kicking animation on the sprites strip, and I just want to program that animation. My bad if i am confusing you like i said I'm new to all this lol and i dont have damge set up. all i have Is walking and jumping lol
 

woods

Member
i am assuming you made a sprite for kicking... going along with the naming of your movement code above...
sprite_index = sPlayerK;


in your step event where you want the player to use the kick attack..
Code:
if keyboard_check(vk_space) //this is the attack button held down
{
is_kicking = true; 
sprite_index = sPlayerK  // this is the kicking animation
}
 
A

Alex hubbard

Guest
i am assuming you made a sprite for kicking... going along with the naming of your movement code above...
sprite_index = sPlayerK;


in your step event where you want the player to use the kick attack..
Code:
if keyboard_check(vk_space) //this is the attack button held down
{
is_kicking = true;
sprite_index = sPlayerK  // this is the kicking animation
}
ok so it worked but it only plays the first frame, he just brings his leg up and holds it there and does not move on to the next frame. but if i hold down space, and move it does the full animation while its moving?
 

woods

Member
i might have made a mistake on that.. does this change it at all?
instead of
keyboard_check
use
if keyboard_check_pressed

edit:

also make sure your image_speed is not set to 0
 

Bentley

Member
so my sprite comes with a kicking animation on the sprites strip, and I just want to program that animation
it would be very helpful if you followed with an explanation as well
Simple idea:
Code:
// Step event
if (key_kick && sprite_index != s_kicking)
{
    sprite_index = s_kicking;
    image_index = 0;
    image_speed = 1;
}

// Animation end event
if (sprite_index == s_kicking)
{
    sprite_index = s_idle;
    image_index = 0;
    image_speed = 1;
}
You will run into problems though. What if you are kicking and you press key right? You wouldn't want your sprite to be s_run. So states come to mind as the easiest way to go about it.

There's lots of other things to consider. Should you set your hspd and vspd to 0 when you kick? Should you check if you are on the ground when you kick? etc.
 
Last edited:

woods

Member
@Bentley
thats why i use a second object for my attack.. it doesnt matter what the player is doing.. when i hit attack button.. attack object shows up based on player's direction ;o)
 
A

Alex hubbard

Guest
i might have made a mistake on that.. does this change it at all?
instead of
keyboard_check
use
if keyboard_check_pressed
so now I press space, and it quickly does the first animation, and that's it? I also don't think its registring "is_kicking = true;" because I originally had the code for the kick like this
"
if keyboard_check_pressed(vk_space)
{
sprite_index = sPlayerATK //that is my kick animation
}
"
and it was the same issue I just mentioned above
 
A

Alex hubbard

Guest
Pseudo code:


Simple idea:
Code:
// Step event
if (key_kick && sprite_index != s_kicking)
{
    sprite_index = s_kicking;
    image_index = 0;
    image_speed = 1;
}

// Animation end event
if (sprite_index == s_kicking)
{
    sprite_index = s_idle;
    image_index = 0;
    image_speed = 1;
}
You will run into problems though. What if you are kicking and you press key right? You wouldn't want your sprite to be s_run. So states come to mind as the easiest way to go about it.

There's lots of other things to consider. Should you set your hspd and vspd to 0 when you kick? Should you check if you are on the ground when you kick? etc.
What are you referring to? @woods
Pseudo code:


Simple idea:
Code:
// Step event
if (key_kick && sprite_index != s_kicking)
{
    sprite_index = s_kicking;
    image_index = 0;
    image_speed = 1;
}

// Animation end event
if (sprite_index == s_kicking)
{
    sprite_index = s_idle;
    image_index = 0;
    image_speed = 1;
}
You will run into problems though. What if you are kicking and you press key right? You wouldn't want your sprite to be s_run. So states come to mind as the easiest way to go about it.

There's lots of other things to consider. Should you set your hspd and vspd to 0 when you kick? Should you check if you are on the ground when you kick? etc.
so this is the closest I've got, but the only issue now is it doesn't do the full animation
 
A

Alex hubbard

Guest
Pseudo code:


Simple idea:
Code:
// Step event
if (key_kick && sprite_index != s_kicking)
{
    sprite_index = s_kicking;
    image_index = 0;
    image_speed = 1;
}

// Animation end event
if (sprite_index == s_kicking)
{
    sprite_index = s_idle;
    image_index = 0;
    image_speed = 1;
}
You will run into problems though. What if you are kicking and you press key right? You wouldn't want your sprite to be s_run. So states come to mind as the easiest way to go about it.

There's lots of other things to consider. Should you set your hspd and vspd to 0 when you kick? Should you check if you are on the ground when you kick? etc.
not only does it not play the full animation but, when i hold space bar and move it stays in the first frame of the animation while i move
 

Bentley

Member
not only does it not play the full animation but, when i hold space bar and move it stays in the first frame of the animation while i move
That's what I wrote in my first post. Other code will set your sprite after you set your sprite to kick. So you need to prevent that code from running. The easiest way is to use states.
 
A

Alex hubbard

Guest
That's what I wrote in my first post. Other code will set your sprite after you set your sprite to kick. So you need to prevent that code from running. The easiest way is to use states.
okay I thank you for the help brother man, but I am very new to this, and I really don't know how to do states, and I'm not sure on how to set all the things in place I might need. were can i go to learn these kind of details?
 

Bentley

Member
okay I thank you for the help brother man, but I am very new to this, and I really don't know how to do states, and I'm not sure on how to set all the things in place I might need. were can i go to learn these kind of details?
I think @samspade has a FSM tutorial on GM community. Just look under tutorials. You can also google Finite State Machines or look on YouTube.

But the idea is that you have a state. You have one state at a time to exclude other code from running.
Pseudo code:
Code:
// Create event
state = "run";

// Step event
switch (state)
{
    case "run":
        // Do stuff
    break;
  
    case "kick":
        // Do stuff
    break;
}
If you're in the kick state, code from the run state will not execute. So while you're kicking your sprite won't be set to spr_run, your x position won't be changed by the "move" variable, and so forth. This is just a simple example. People use scripts to run states, user events, etc.
Ex:
Code:
// Create event
enum PLAYER_STATES
{
    RUN, // equals 0
    KICK, // equals 1
}

state = PLAYER_STATES.RUN;

// Step event
event_user(state);

// User event 0
    // Code for running
   
// User event 1
    // Code for kicking
Edit: if I didn't explain it well enough just let me know
 
A

Alex hubbard

Guest
I think @samspade has a FSM tutorial on GM community. Just look under tutorials. You can also google Finite State Machines or look on YouTube.

But the idea is that you have a state. You have one state at a time to exclude other code from running.
Pseudo code:
Code:
// Create event
state = "run";

// Step event
switch (state)
{
    case "run":
        // Do stuff
    break;
 
    case "kick":
        // Do stuff
    break;
}
If you're in the kick state, code from the run state will not execute. So while you're kicking your sprite won't be set to spr_run, your x position won't be changed by the "move" variable, and so forth. This is just a simple example. People use scripts to run states, user events, etc.
Ex:
Code:
// Create event
enum PLAYER_STATES
{
    RUN, // equals 0
    KICK, // equals 1
}

state = PLAYER_STATES.RUN;

// Step event
event_user(state);

// User event 0
    // Code for running
  
// User event 1
    // Code for kicking
Edit: if I didn't explain it well enough just let me know
soi was able to add states and i was able to get my kick animation working but now its stuck in a damn loop of the animation. and while it is preforming this loop i cant do anything
 

Bentley

Member
soi was able to add states and i was able to get my kick animation working but now its stuck in a damn loop of the animation. and while it is preforming this loop i cant do anything
Go to the animation end event and add something like this:
Code:
if (sprite_index == s_kick)
{
    state = "idle";     
}
You have to have a state transition. Otherwise, you'll be in the kick state forever.
 
A

Alex hubbard

Guest
Go to the animation end event and add something like this:
Code:
if (sprite_index == s_kick)
{
    state = "idle";    
}
You have to have a state transition. Otherwise, you'll be in the kick state forever.
so I added an animation end event and added the code, but nothing happened. do I need to add a separate state for the animation end event aswell?
 
A

Alex hubbard

Guest
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check(vk_up);
key_attack = keyboard_check(vk_space);

switch (state)
{
case PLAYERSTATE.FREE: PlayerState_Free(); break;
case PLAYERSTATE.ATTACK_KICK: PlayerState_Attack_Kick(); break;
}
No.
Hmm. Let me see the code you have in your step event and animation end event
I added all the code I have

//animation end
if (sprite_index == sPlayerATK)
{
state = "idle";
}

//step event
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check(vk_up);
key_attack = keyboard_check(vk_space);

switch (state)
{
case PLAYERSTATE.FREE: PlayerState_Free(); break;
case PLAYERSTATE.ATTACK_KICK: PlayerState_Attack_Kick(); break;
}

//PlayerState_free
this is all the code for moving and jumping

var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,objectW)) and (key_jump)
{
vsp = -7;
}
{
dir = 0; // facing right
}

if keyboard_check(key_left)
{
dir = 1; // facing left
}


//horizontal collison
if (place_meeting(x+hsp,y,objectW))
{
while (!place_meeting(x+sign(hsp),y,objectW))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;


//Vertical Collision
if (place_meeting(x,y+vsp,objectW))
{
while (!place_meeting(x,y+sign(vsp),objectW))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

//Animation
if (!place_meeting(x,y+1,objectW))
{
sprite_index = sPlayerJ;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1;else image_index = 0;
}
else
{
image_speed = 1;
if (hsp == 0)
{
sprite_index = sPlayer;
}
else
{
sprite_index = sPlayerR;
}


if (hsp != 0) image_xscale = sign(hsp);
}

if (key_attack) state = PLAYERSTATE.ATTACK_KICK;

//PlayerState_Attack_Kick
This is the code for the kick

hsp = 0
vsp = 0

//start of the attack
if (sprite_index != sPlayerATK)
{
sprite_index = sPlayerATK;
image_index = 0;

}
 

Bentley

Member
You have to adapt what I posted to your game.

So I wrote:

state = "idle";

You need to type:

state = PLAYERSTATE.FREE; // Or whatever the state is after kicking
 
A

Alex hubbard

Guest
You have to adapt what I posted to your game.

So I wrote:

state = "idle";

You need to type:

state = PLAYERSTATE.FREE; // Or whatever the state is after kicking
I went ahead and corrected it, but nothing changed it seems like its not reading the animation end event
 
Top