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

GML Noob attack code | need help!

B

Bengs

Guest
Hello everyone my name is Ben,

I just started using gamemaker and i am usingit for a school project.
This is my code so far except i have 2 problems

1) My attack animations doesnt seem to play al the wat
2) I dont know the code to make my character attack when he is stationary

Note: changing mouse_check_button(mb_left) to mouse_check_button_pressed(mb_left) only makes things worse.

Code:
//Variables
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_space = keyboard_check_pressed(vk_space);
key_w = keyboard_check_
pressed(ord("W"));
key_lmb = mouse_check_button(mb_left);
jump = 0;

//Attack Status
if key_lmb {
attack= 1;
} else {
attack = 0;
}

//Hp
if Chp <= 0
{
    instance_destroy();
    game_restart();
}

//calculating movement
var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;

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

if (place_meeting(x+hsp,y,Obj_GrassBlock))
{
    while (!place_meeting(x+sign(hsp),y,Obj_GrassBlock))
    {
        x=x+sign(hsp);
    }
    hsp = 0;
}

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

//moving controls
if (keyboard_check(vk_nokey))
{
    image_speed = 0;
    image_index = 0;
    attack = 0;
}

if (key_right) and (vsp=0){
    if attack = 0
    {
        x = x + hsp;
        Cdmg= 0;
        image_speed = hsp/1.5;
        sprite_index = Spr_CharacterWalkRight;
    } else
        {
            attack = 1;
            image_speed = 2;
            sprite_index = Spr_CharacterAttack01Right;
            if (place_meeting(x+1,y,par_enemy))
            {
                    Cdmg=100;
            }
        }
}


if (key_left) and (vsp=0) {
    if attack = 0
    {
        x = x + hsp;
        Cdmg= 0;
        image_speed = hsp/1.5;
        sprite_index = Spr_CharacterWalkLeft;
    } else
        {
            attack = 1;
            image_speed = 2;
            sprite_index = Spr_CharacterAttack01Left;
            if (place_meeting(x+1,y,par_enemy))
            {
                    Cdmg=100;
            }
        }
}

y = y + vsp;

if (place_meeting(x,y+1,Obj_GrassBlock)) and (key_space or key_w)
{
    jump = 1;
    vsp = -3.5;
}

if (key_left) and (vsp != 0) {
    if attack = 0{
        x = x + hsp;
        Cdmg= 0;
        image_speed = 0;
        image_index = 0;
        sprite_index = Spr_CharacterWalkLeft;
        }   
        else {
            image_speed = 2;
            sprite_index = Spr_CharacterAttack01Left;
            if (place_meeting(x-1,y,par_enemy))
            {
                Cdmg=100;
            }
}
}
if (key_right) and (vsp !=0) {
    if attack = 0{
        x = x + hsp;
        Cdmg= 0;
        image_speed = 0;
        image_index = 0;
        sprite_index = Spr_CharacterWalkRight;
    } else {
            image_speed = 2;
            sprite_index = Spr_CharacterAttack01Right;
            if (place_meeting(x+1,y,par_enemy))
            {
                Cdmg=100;
            }
}
}

//Stationary Attacks
 
Last edited by a moderator:

rytan451

Member
Hi, Bengs,
For the sake of readability, you can enclose your code in [ code][/code] tags (remove the space inside the square brackets), so it appears formatted as code. It is much easier to read it if it is formatted as such.
 
B

Bengs

Guest
Hi, Bengs,
For the sake of readability, you can enclose your code in [ code][/code] tags (remove the space inside the square brackets), so it appears formatted as code. It is much easier to read it if it is formatted as such.
Okey Ty I’ve changed it
 

rytan451

Member
Hi, Ben,
For your animation problems, you want to ask yourself this: what affects the animations? Read through those areas carefully. If, after considering them carefully, you conclude they work exactly as intended (check corner cases, such as division by zero), then you can move on to consider other pieces of code. This process is called debugging, and it is typically very time-consuming.
To solve your stationary attacks, ask yourself: how did I make the moving attacks work as I wanted? Can I remove just the moving parts? Can I check if the player is moving before I start the attack?

I am not willing to fix your code directly. I hope to make you learn how to fix these things yourself, so that you are better able to resolve all these problems yourself.
 

samspade

Member
In the spirit of the above I've hidden my suggestions in a spoiler in case you want to try debugging it yourself. Learning debugging is one of the most important things you can do when learning to code. If you wanted to try yourself, I would suggest walking through the logic of your code in English.

That said, I think I know what is wrong; however, my suggestion to fix it might be even more work - ultimately better though (in my opinion at least) as solving the issue without significantly changing your code will eventually make your code a mess of conditions and a problem to add to or change.

You might want to consider looking up state machines tutorials on YouTube. That would help a lot with this problem. Your code does a lot of things right, but to make it do what you want, you should probably re-write a significant portion of it. Right now your problem is that you animations, or rather your sprite indexes, are tied to whether you've pushed the attack button (among other things).

Following your code's logic you are saying:

If I'm pushing right and my vertical speed is equal to zero. Then if I have pushed attack, switch my sprite to attack. If I have not pushed attack switch my sprite to something else.

But attack is only true when you are holding the mouse button. So the above could be changed to read:

If I'm pushing right and my vertical speed is equal to zero. Then if I am pushing the left mouse button, switch my sprite to attack. If I am not pushing the left mouse button switch my sprite to something else.

This logic runs each step (so at 60 frames per second, it runs sixty times each second) meaning that as soon as you aren't attacking (pushing left mouse button) your sprite will switch.

If you have a specific attack animation and you want that animation to finish before the attack stops, then here are the changes I would suggest:
  • Switch to some type of state machine
  • Have an attack state.
  • Use the animation end event (I believe it is under other events) to switch out of the attack state.
Once you have state machines set up, adding a stationary attack would be more straightforward.
 
B

Bengs

Guest
ty both I’ve fixed the animation problem. There is one thing I would like to as. Is there a way to see what way your character is facing because if so I can make the code for my stationary attack
 

samspade

Member
ty both I’ve fixed the animation problem. There is one thing I would like to as. Is there a way to see what way your character is facing because if so I can make the code for my stationary attack
Several ways come to mind:
  • Use the sprite.
  • Save the sign of hsp to a variable.
You're already using different sprites for each direction so you could just check what sprite is assigned to the image index. Alternatively:

Code:
///create
facing = 1; //1 will equal facing left, -1 will equal facing right

///step
if (hsp != 0) {
    facing = sign(hsp);
}
You should look up sign() in the manual but it returns -1, 0, 1 depending upon whether a number is negative, 0, or positive. So you initialize the variable facing to some direction (I chose 1). Then since a positive hsp directly translates into moving left and a negative hsp directly translates into moving right, saving whether the player is moving left or right will save the direction the player is facing. So if the character is moving left or right (e.g. hsp != 0) you save whether hsp is positive or negative to facing. And 1 will mean the player was moving left (e.g. hsp > 0) and -1 will mean the player was moving right (e.g. hsp < 0).
 
Top