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

Legacy GM NEED HELP Placing a gun in four directional game

D

dawson2223

Guest
Hi everyone!

I'm coding a shooter with 4-directional sprites. I have programmed the top and right sides to place my gun in the place I want it to be. For the bottom and left animations of the character, I want the gun to be in a different place so it matches the character movement, it has to be in the other hand. I can't find a way of doing it. I will attach my code.

Here's a link of what I'm trying to explain.



obj_player:
Create:

/// Variables
move = 0 // 0 for stop and 1 for running
// Initiate Weapon
x_offset = 13;
y_offset = -3;
my_gun = instance_create(x+x_offset, y+y_offset, obj_pistol);

// Initiate Weapon
x_offset = 13;
y_offset = -3;
my_gun = instance_create(x+x_offset, y+y_offset, obj_pistol);
my_gunLeft = instance_create(x+x_offset, y+y_offset, obj_pistol);

Step:
// Movement
depth = -y;

if (keyboard_check(ord('W')) && place_free (x,y-3)) {
y -= 3
sprite_index = sprPlayerRunUp
move = 1
}
if (keyboard_check(ord('S')) && place_free (x,y+3)) {
y += 3
sprite_index = sprPlayerRunDown
move = 1
}
if (keyboard_check(ord('D')) && place_free (x+3,y)) {
x += 3
sprite_index = sprPlayerRunRight
move = 1
}
if (keyboard_check(ord('A')) && place_free (x-3,y)) {
x -= 3
sprite_index = sprPlayerRunLeft
move = 1
}

if (x=xprevious && y=yprevious)
move = 0

if move = 0 {
image_speed = 0
if sprite_index = sprPlayerRunUp
image_index = 0
if sprite_index = sprPlayerRunDown
image_index = 1
if sprite_index = sprPlayerRunLeft
image_index = 3
if sprite_index = sprPlayerRunRight
image_index = 2
sprite_index = sprPlayerStand
}


End Step:
my_gun.x = x + x_offset;
my_gun.y = y + y_offset;





I tried using an if statement in the end step but it didn't work out right. I could just be using a bad code for it, not very good at coding. Please help me out!!! Thank you!!!!!
 
Last edited by a moderator:

Mick

Member
I guess you already have four sprites for the four directions of your gun. In the step event where you check for keyboard input you should set the wanted offsets and the sprite of the gun, like:

Code:
if (keyboard_check(ord('W')) && place_free (x,y-3)) {
  y -= 3
  sprite_index = sprPlayerRunUp
  move = 1
  x_offset = 0
  y_offset = -3
  my_gun.sprite_index = sprGunUp
}
Do the same for all directions. You might also want to set the depth variable of the gun depending on direction, for example when facing up, the gun should be behind the player object if the gun and player sprites overlap.

P.S. Remove the code from the create event after the second "// Initiate Weapon" comment.
 
D

dawson2223

Guest
I guess you already have four sprites for the four directions of your gun. In the step event where you check for keyboard input you should set the wanted offsets and the sprite of the gun, like:

Code:
if (keyboard_check(ord('W')) && place_free (x,y-3)) {
  y -= 3
  sprite_index = sprPlayerRunUp
  move = 1
  x_offset = 0
  y_offset = -3
  my_gun.sprite_index = sprGunUp
}
Do the same for all directions. You might also want to set the depth variable of the gun depending on direction, for example when facing up, the gun should be behind the player object if the gun and player sprites overlap.

P.S. Remove the code from the create event after the second "// Initiate Weapon" comment.

I only had one gun sprite (sorry I accidentally included code I was trying to use to fix the problem) but what you said worked thank you SO MUCH!!!!!!! :D
 
Top