GML Cant Code attack

C

Claudia

Guest
Hi there, I am very new to Game Maker and I have a module assignment due in a couple of months where I have to design a basic platformer. I'm basically coding mario but its shrek kind of. So far I have made a new room and have coded Shrek (the player) walking left, right and jumping (with animation). However I can't for the life of me figure out how to code the players attack button. I've been looking at a lot of tutorials but I can't get any of them to work with the code I already have.

I want to make my player attack animation play when you press space bar. I have 2 different sprites for attack. One facing left and the other facing right. If I could get the controls and animation down I could probably work out the whole hitbox thing.

Here's what I've got in my player object step event. It probably makes no sense:

///Platform Physics

MOVELEFT = keyboard_check(ord('A'));
MOVERIGHT = keyboard_check(ord('D'));
MOVEUP = keyboard_check(ord('W'));

//check ground
if place_meeting (x, y+1, obj_grass) {
vspd = 0

//jump
if MOVEUP {
sound_play(snd_jump)
vspd = -jspd
}
} else {
//gravity
if vspd < 10 {
vspd += grav
}
}

//right
if MOVERIGHT {
hspd = spd;
}

//left
if MOVELEFT {
hspd = -spd;
}

//attack
if (attack_key) {
image_index = 0;
state = scr_attack_state;
}

//check
if ((!MOVERIGHT && !MOVELEFT) || (MOVERIGHT && MOVELEFT)) {
hspd = 0
}

//horizontal collisions
if place_meeting(x+hspd, y, obj_grass) {
while !place_meeting (x+sign(hspd), y, obj_grass) {
x+=sign(hspd)
}
hspd = 0
}
//move x
x+=hspd

//vertical collisions
if place_meeting(x, y+vspd, obj_grass) {
while !place_meeting (x, y+sign(vspd), obj_grass) {
y+=sign(vspd)
}
vspd = 0
}
//move y
y+=vspd



///Animation

if (x > xprevious)
{
if (sprite_index != spr_shrek_walking_right)
sprite_index = spr_shrek_walking_right;
image_speed = 0.15;
}
else if (x < xprevious)
{
if (sprite_index != spr_shrek_walking_left)
sprite_index = spr_shrek_walking_left;
image_speed = 0.15;
}

else
{
sprite_index = spr_shrek_standing

}



Then I have a script activation for this script:


///Scr_attack_state

image_speed = .5;

switch (sprite_index) {
case spr_shrek_walking_right:
sprite_index = spr_shrek_hit;
break;

case spr_shrek_walking_left:
sprite_index = spr_shrek_hit_left;
break;

}

Any help is appreciated :)
 
O

Odlaw

Guest
I think your code is working, but as soon as it switches to the hit animation the very next frame it switches back during the Animation portion of the code. My suggestion would be rework the animation portion of the code, it should ignore the current code if it's in an attack state and when the attack animation ends switch back to the code you have now. I hope this makes sense.
 

MaxLos

Member
Basically what Odlaw said; set the animation for movement and idle stuff when your in your normal state and attacking stuff for when your in your attacking state. Also not sure if it's intentional but looking at your scr_attack_state script, it doesn't have a case statement for when your sprite is "spr_shrek_standing" so I don't think you'll be able to attack unless your moving..
 
C

Claudia

Guest
Basically what Odlaw said; set the animation for movement and idle stuff when your in your normal state and attacking stuff for when your in your attacking state. Also not sure if it's intentional but looking at your scr_attack_state script, it doesn't have a case statement for when your sprite is "spr_shrek_standing" so I don't think you'll be able to attack unless your moving..
I think your code is working, but as soon as it switches to the hit animation the very next frame it switches back during the Animation portion of the code. My suggestion would be rework the animation portion of the code, it should ignore the current code if it's in an attack state and when the attack animation ends switch back to the code you have now. I hope this makes sense.
Thanks for the replies but i literally have no idea what any of that means lol. I dont even understand my own code. Its all copied and adapted from tutorials. I dont fully understand states either. Like where do you put the code for states? How do you change states? I'm a complete noob.
 

MaxLos

Member
Thanks for the replies but i literally have no idea what any of that means lol. I dont even understand my own code. Its all copied and adapted from tutorials. I dont fully understand states either. Like where do you put the code for states? How do you change states? I'm a complete noob.
Well thats not good lol, but its fine we all have to start from somewhere, i guess. I used to c/p code to but I would at least HEAVILY comment it so I could make heads or tails of what it was later. You've seemed to have some comments on it so thats good. Anyway your code is kinda similar to the platformer game I'm working on so I think I can help.

In the create event of your player make a variable called 'state' and set it "Normal".
Code:
 state = "Normal"
Typically when the player is in their normal state, their able to move, jump, attack, etc.
Wrap your animation code in a Normal state check since that's when your able to perform those actions
Code:
///Animation

if state = "Normal"
{
if (x > xprevious)
{
if (sprite_index != spr_shrek_walking_right)
sprite_index = spr_shrek_walking_right;
image_speed = 0.15;
}
else if (x < xprevious)
{
if (sprite_index != spr_shrek_walking_left)
sprite_index = spr_shrek_walking_left;
image_speed = 0.15;
}

else
{
sprite_index = spr_shrek_standing
}
}
Next your gonna want your player to change to their "Attack" state when the attack button is pressed:

Code:
if (attack_key)
{
    image_index = 0;
    script_execute(scr_attack_state);
    state = "Attack";
}
I changed a few things here. It seemed that you already had a state variable but you were setting it to that script? Anyway I think that should work... Just add an animation end event for your attacks and set yourself back to your Normal state
 
Last edited:

MaxLos

Member
Ok, I made an empty project to test everything out and I can say that it should work now. I had to add a lot of things your code cus it seems like its missing a lot and whatever tutorial you copied it from is probs older than me. This isn't super optimized at all because I didn't want to change your code too much to the point you wouldn't understand anything.
Anyways, heres the code
Code:
Shrek Create Event
spd = 5;
hspd = 0;
vspd = 0;
jspd = 4;
grav = 0.2;

state = "Normal";
Your speed, jspd, and grav can be whatever, I just used random numbers for testing.

Code:
Shrek Step Event

///Platform Physics

MOVELEFT = keyboard_check(ord('A'));
MOVERIGHT = keyboard_check(ord('D'));
MOVEUP = keyboard_check(ord('W'));
attack_key = keyboard_check(ord("J"));

//check ground
if place_meeting(x,y+1,obj_grass)
{
    vspd = 0
}
else if (vspd < 10) vspd += grav;

//jump
if state = "Normal"
{
    if MOVEUP
    {
        sound_play(snd_jump)
        vspd = -jspd
    }

    //right
    if MOVERIGHT hspd = spd;

    //left
    if (MOVELEFT) hspd = -spd;

    //attack
    if (attack_key)
    {
        state = "Attack"
        image_index = 0;
        script_execute(scr_attack_state);
        hspd = 0;
        vspd = 0;
    }
}
//check
if ((!MOVERIGHT && !MOVELEFT) || (MOVERIGHT && MOVELEFT))
{
hspd = 0
}

//horizontal collisions
if place_meeting(x+hspd, y, obj_grass)
{
    while !place_meeting (x+sign(hspd), y, obj_grass)
    {
        x+=sign(hspd);
    }
    hspd = 0;
}

//move x
x+=hspd

//vertical collisions
if place_meeting(x, y+vspd, obj_grass)
{
    while !place_meeting (x, y+sign(vspd), obj_grass)
    {
        y+=sign(vspd)
    }
    vspd = 0
}

//move y
y+=vspd



///Animation
if state = "Normal"
{
    if (x > xprevious)
    {
        if (sprite_index != spr_shrek_walking_right)
        sprite_index = spr_shrek_walking_right;
        image_speed = 0.15;
    }
    else if (x < xprevious)
    {
        if (sprite_index != spr_shrek_walking_left)
        sprite_index = spr_shrek_walking_left;
        image_speed = 0.15;
    }
    else
    {
       if (hspd = 0)
       {
         if (sprite_index = spr_shrek_walking_right) or (sprite_index = spr_shrek_hit_right) sprite_index = spr_shrek_standing_right;
         else if (sprite_index = spr_shrek_walking_left) or (sprite_index = spr_shrek_hit_left) sprite_index = spr_shrek_standing_left;
       }
    }
}
So the first thing I added was attack_key = keyboard_check_ord("J"). In the code you posted, you checked for the attack key but hadn't initalized it anywhere. Code in the attack key check is the same as my last post except I set hspd and vspd to 0 so you don't slide across the ground if you attacked while moving. I also wrapped your movement key checks in the Normal State check so you can't move while attacking.

For the animation part, I added a check for when we're not moving and had to add two sprites for standing still while facing the left and right, since you aren't using image_xscale to flip your sprites.


Scr_attack_state script:
Code:
image_speed = .5;
state = "Attack";

switch (sprite_index)
{
    case spr_shrek_walking_right: sprite_index = spr_shrek_hit_right; break;
    case spr_shrek_walking_left: sprite_index = spr_shrek_hit_left; break;
    case spr_shrek_standing_right: sprite_index = spr_shrek_hit_right; break;
    case spr_shrek_standing_left:sprite_index = spr_shrek_hit_left; break;
}
All we're doing here is setting us to the correct attacking sprite for whatever direction we're facing

And finally, the animation end event:

Code:
//If we finished our attack, go to our Normal state
switch (sprite_index)
{
    case spr_shrek_hit_right:
    case spr_shrek_hit_left:
    state = "Normal"; break;
}
If it still doesn't work, PM me and I can send you the project I tested it on but it should work lol. I even have an old tutorial I made but never uploaded if your still having trouble/wanna see a more optimized way
 
Top