I could use a hand changing my sprite

H

Hajbiz

Guest
I'm creating a top down dungeon crawler game. I have 4 way directional movement, with up, down, left, and right. Each direction has it's own animation for walking. In this game you shoot enemies. I am trying to set up a shoot animation so that if the sprite is the left sprite and space is pressed it plays the left shoot animation, if the up walk sprite is active and I press space the up shoot animation, and so on. However I can't get my code working. I'm rather new to GML I tried this on a step event.

if !(keyboard_check_pressed(ord("vk_space"))) && !(keyboard_check_pressed(ord("vk_left"))) sprite_index = sp_shoot_l
if !(keyboard_check_pressed(ord("vk_space"))) && !(keyboard_check_pressed(ord("vk_right"))) sprite_index = sp_shoot_r
if !(keyboard_check_pressed(ord("vk_space"))) && !(keyboard_check_pressed(ord("vk_up"))) sprite_index = sp_shoot_u
if !(keyboard_check_pressed(ord("vk_space"))) && !(keyboard_check_pressed(ord("vk_down"))) sprite_index = sp_shoot_d

Thank you to those who help.
 

FrostyCat

Redemption Seeker
The constants starting with vk_ aren't meant to be used with ord(), only letters and numbers are.
The Manual said:
The following is a small example of how to use the vk_ constants:

Code:
if keyboard_check_pressed(vk_tab)
   {
   instance_create(x,y,obj_Menu);
   }
 
H

Hajbiz

Guest
What would you say would be a better way to change my sprites when 2 buttons are pressed, or if a current animation is active?
 
Your current method is alright, but you could encase it all in a single Space check where you then check for each direction, instead of four separate conditions with the Space.

The problem is that your method requires both Space and an arrow to be pressed in a single frame, which isn't always going to be the case (especially as it gets worse the faster the room speed is). You could simply check which the last direction set was when firing.
 
H

Hajbiz

Guest
if !(keyboard_check_pressed(ord("vk_space"))) && if (phy_position_x > phy_position_xprevious) {
sprite_index = spr_player_right;
}

Would something along these lines be good?
 
T

TheRBZ

Guest
do something like
if !(keyboard_check_pressed(ord("vk_space"))){
sprite_index = spr_player_ + direction +;}
Direction would be a variable, but you would need to change that code because I forgot how to use the + +.
 

LunaticEdit

Member
Are you using direction at all? 0 is right, 90 is up, 180 is left, and 270 is down.
Also, use keyboard_check(vk_space) to determine if the key is pressed at that very moment.
For some reason you are saying "if keyboard left key is NOT pressed and space is NOT pressed then set to shoot, this seems to be the opposite of what you want...

Code:
if (keyboard_check(vk_space) {
   // We are shooting something
   if (keyboard_check(vk_left)) {
      sprite_index = spr_shoot_l;
   } else if (keyboard_check(vk_right)) {
      sprite_index = spr_shoot_r;
   } else if (keyboard_check(vk_up)) {
      sprite_index = spr_shoot_u;
   } else if (keyboard_check(vk_down)) {
      sprite_index = spr_shoot_d;
   }
Note that keyboard_check_pressed is only triggered ONCE until the key is released and pressed again, you most likely DON'T want this.
 
Top