GameMaker Help with holding keys

S

Sahibjot Cheema

Guest
I want to hold x to hold out you shield but if you press shift then it will switch weapon to sword.

The problem is that holding x and pressing shift at the same time glitches the shield. The sprite is stuck on holding the shield but u have the effects of not holding it out. It fixes it self if u let go of x and press shift again.

I think the problem is that the code for x keeps running until its let go but i want it to update when u press shift.

kActionHold = keyboard_check(ord("X"))

if (weapon == SHIELD) {
if (kActionHold) {
if (!action) {
// Shield Wall Action
if ((cRight || cLeft) && !onGround) {
sprite_index = PlayerActionWallSlide_Shield;
action = true;
// Dash Shield Action
}else if (state == DASH) {
sprite_index = PlayerDashAction_Shield;
alarm[1] = 12;
action = true;
// Shield Block
} else {
sprite_index = PlayerAction_Shield;
action = true;
}
}
} else action = false;
}

if (kChange) {
if (weapon = SWORD) weapon = SHIELD;
else if (weapon = SHIELD) weapon = SWORD;
}
 
B

Bayesian

Guest
I would look into using a state machine for this functionality. If you want to keep the code you have add a flag for the shield up state. Set the flag to false when shift is pressed and have x key down reset the flag to true
 

TheouAegis

Member
Whay's your SWORD state code? Sounds like the issue is just that you don'treset action and the sprite when you switch weapons.
 
S

Sahibjot Cheema

Guest
I would look into using a state machine for this functionality. If you want to keep the code you have add a flag for the shield up state. Set the flag to false when shift is pressed and have x key down reset the flag to true
I had tried this before already, using var instead of keys and changing that var when shift is pressed but it didn't work. But I tried it again and after rearranging the code a bit and adding a bit more stuff fixed it. Now it works perfectly. Thx :D
 
S

Sahibjot Cheema

Guest
Whay's your SWORD state code? Sounds like the issue is just that you don'treset action and the sprite when you switch weapons.
Yes exactly. That was 1 part of the problem. After fixing and changing the order of the code everything works.
 
Top