0
\$\begingroup\$

I'm making a 2d side-scrolling fighting game in game maker:studio and I want to make a code that when the player attacks he stops moving. I've tried some options:

keyboard_check(ord("D"));

both this and

keyboard_check_pressed(ord("D"));
x += 0

I also tried

hspeed = 0

I also tried adding these to my left and right key events:

if (sprite_index = spr_atk_right)
 {
 x -=4 for right key event or x += 4 for left key event
 }

I have a right key and left key event with the simple code x += 4 for the right key and x -= 4 for the left key. I was hoping the second set of codes would cancel it out but neither worked.

\$\endgroup\$
1
  • \$\begingroup\$ The other (advanced) answers are definitely the way to go, in particular the state machine. If you are new and just simply want to get it going you could perhaps wrap it up in a boolean and reset it in the part where a key is checked (hope that makes sense, hard to tell from what you are actually doing) \$\endgroup\$ – lozzajp Nov 7 '16 at 10:27
2
\$\begingroup\$

Might I suggest looking into Finite State Machines? I came across it the other day and I thought it interesting because for every action you add, more bugs require squashing. However, finite state machines work in a way where when you're in a "state" only on pressing of certain keys, does your avatar/character perform an action. So while standing (stand state) you can only walk, jump and duck. While jumping (jump state) you can only move left and right, which removes the ability to jump and duck while in midair. In your case it'd probably look a little like:

Stand state (nothing pressed): Jump Attack Walk Duck(?)

Jump state: Attack

Walk state: Attack Jump Duck(?)

Duck state: (Perform duck animation only)

Attack state: (Perform Attack animation only)

In this way, you can't move while attacking, even if you tried. Though it would mean you can only move AFTER you're done attacking.

Read it here: http://gameprogrammingpatterns.com/state.html

This at least reduces the need for adding way more than you need to any if else statements and does away with having a billion flags.

\$\endgroup\$
1
  • \$\begingroup\$ Immediately thought of that book and chapter when I read the title, definitely the way to go. \$\endgroup\$ – lozzajp Nov 7 '16 at 10:25
0
\$\begingroup\$

How about this, create an alarm that has a timer for your players attack duration. start the timer when they press the attack button to count down, then release them when they are allowed to move again.

create event:

can_move = true;
attack_speed = room_speed*2; //2 seconds 

alarm 0:

can_move = true;

step event:

if(keyboard_check_pressed(ord("D"))) {
    can_move = false;
    alarm[0] = attack_speed;
}

if(can_move = true) {
    //put your move code here
    //so it only executes when you are allowed
}
\$\endgroup\$
0
\$\begingroup\$

You should not do x += 4 and x -= 4 directly inside the event. Instead, store the pressed keys in an array.

You can then process the array in the priority order you find appropriate, for example :

if (keyPressed["D"])
{
     // Attack
}
else if (keyPressed["RightArrow"])
{
     x += 4;
}
else if (keyPressed["LeftArrow"])
{
     x -= 4;
}

This will definitly make your life simpler.

\$\endgroup\$
0
\$\begingroup\$

if your using game maker studio you can use if statements to make sure that only 1 key is being pressed for the movement in a step event like this

Movement Event (Coded in GML[Game Maker Language])(Make sure this is in the step event of the player)

if keyboard_check_pressed(ord('W')) && string_length(keyboard_string) == 1{
     y -= 5;
}

Action Event (In GML)(In Step Event)

if keyboard_check_pressed(vk_control) && keyboard_check_pressed(ord('W')){
    <Action Event Code Here>
}
\$\endgroup\$
1
  • \$\begingroup\$ I seen all the other answers and I think mine would be the easiest way to go. \$\endgroup\$ – WolfHybrdi23 Jan 16 '17 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.