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

Animation for 8 driection shooting

J

JohnSebek

Guest
Hello, so recently I want to fix my game shooting system by using the old school shooting like in Metroid. I have all programmed but I have no idea how to make the sprite to change when shooting in different directions.My player shoots in 8 directions: up,down,left,right,left,upright,down left,downright.I have the basic like when key pressed left up and down, but I have a problem to make animation while shooting in up-down and down directions and while moving. For more info, there is a shooting code.

Information about object: obj_player
Sprite: spr_iddleR
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

// weapon variables:
heat = 0
cooldown = 20

Step Event:
COMMENT: shooting
execute code:

var dx, dy, db;
// shooting direction determination:
dx = (keyboard_check(vk_right) - keyboard_check(vk_left))
dy = (keyboard_check(vk_down) - keyboard_check(vk_up))
// is shooting in some direction:
if (dx != 0 || dy != 0) if (heat <= 0) {
(instance_create(x, y, obj_bullet)).direction = point_direction(0, 0, dx, dy)
heat = cooldown
}
heat = max(0, heat - 1) // cool down

COMMENT: walking
execute code:

if keyboard_check(ord("A")) = true
{
hspeed = -3 //must be negative to go left
sprite_index = spr_walkL
image_speed = 0.5
}

if keyboard_check(ord("D")) = true
{
hspeed = 3
sprite_index = spr_walkR
image_speed = 0.5
}

if keyboard_check(ord("A")) = false and keyboard_check(ord("D")) = false
{
hspeed = 0
sprite_index = spr_iddleR
}

Collision Event with object parent_block:
execute code:

//if the collided object is solid perform actions
if(other.solid == true) {
//make the player contact solid
if(vspeed < 0) move_contact_solid(90,5);
else move_contact_solid(270,5);

//set the vertical speed to 0, important!
vspeed=0

//if he has the jump sprite, we want him to show other sprites
if(sprite_index == spr_iddleL) {
if(wspeed = 0) sprite_index = spr_iddleL
else sprite_index = spr_walkL
} else if(sprite_index == spr_iddleR) {
if(wspeed = 0) sprite_index = spr_iddleR
else sprite_index = spr_walkR
}
}

NOTE
I am still learning in GMC language, so some stuff might still be unknown for me.:( I tried to look also on the internet but found only one tutorial/example but the code was so complex and vague that I lost myself in it.:bash: For more info feel free to ask me :)
 

TheouAegis

Member
You are setting your sprite simply based on if he's moving, so his sprite will always be his walking sprite. You need to determine which sprite to use based on if he's moving AND based on dx and dy. So right now you just go to spr_walkR or spr_walkL or spr_idleR or spr_idleR based on if your left or right keys are pressed. You need to also check if you're pressing up or down, and maybe also check if you're shooting (if this is a simple Metroid clone with no shooting animation, then just check what keys are pressed). You can convert dx and dy to a simple variable and use that value to determine what sprites to use.

var aim = (dx > 0) + (dy < 0) * 2 + (dx < 0) * 4 + (dy > 0) * 8;

The value of aim will be as follows:

no keys pressed: 0
right key pressed: 1
up key pressed: 2
left key pressed: 4
down key pressed: 8
right and up both: 3
left and up both: 6
right and down both: 9
left and down both: 12

Any other values should be rendered illegal since on a normal controller you wouldn't be able to do that anyway. Just check the value of aim before you set a sprite to determine which sprite to use.
 
J

JohnSebek

Guest
You are setting your sprite simply based on if he's moving, so his sprite will always be his walking sprite. You need to determine which sprite to use based on if he's moving AND based on dx and day. So right now you just go to spr_walkR or spr_walkL or spr_idleR or spr_idleR based on if your left or right keys are pressed. You need to also check if you're pressing up or down, and maybe also check if you're shooting (if this is a simple Metroid clone with no shooting animation, then just check what keys are pressed). You can convert dx and dy to a simple variable and use that value to determine what sprites to use.

var aim = (dx > 0) + (dy < 0) * 2 + (dx < 0) * 4 + (dy > 0) * 8;

The value of aim will be as follows:

no keys pressed: 0
right key pressed: 1
up key pressed: 2
left key pressed: 4
down key pressed: 8
right and up both: 3
left and up both: 6
right and down both: 9
left and down both: 12

Any other values should be rendered illegal since on a normal controller you wouldn't be able to do that anyway. Just check the value of aim before you set a sprite to determine which sprite to use.
Sounds reasonable. But I still kinda don´t know how to transfer this into code. So I will define the var aim and then it will be like 0=sprite_index = spr_Iddle?
 

TheouAegis

Member
You can use a switch or an array or even a ds_map.

Code:
Sprites = ds_map_create();
Sprites[? "spr_IdleR1"] = spr_IdleR;
Sprites[? "spr_IdleR2"] =spr_IdleU;
Sprites[? "spr_IdleR3"] = spr_IdleRU;
Sprites[? "spr_IdleR4"] = spr_IdleLb
And so on and so on. Each number corresponds to a value of aim. That could be how you set up a ds_map to hold all the player's sprites. So then when you need to actually change the sprite, you set the map index to whatever the normal right sprite is (as a string) plus the string() of aim.

Code:
sprite_index =Sprites[? "spr_WalkR"+string(aim)];
Thatwould set it to, for example, spr_WalkL if you are moving and aim is 4.
 

CMAllen

Member
There are some questions you're going to want to answer before moving forward:
How do you want to do aiming input?
What method of input are you leaning towards? Keyboard and mouse? Gamepad?
Do you want to limit the directions a player can shoot while moving or when stationary?

There are different ways to tackle this that depend on how you want the player to aim and what methods you intend to allow for player input. If you're thinking more keyboard and mouse, you could let the player aim using the mouse (within reason, depending on sprite assets). If you're leaning towards gamepad input, for example, you may or may not want to use 2 thumbsticks, one that only aims, and the other that aims and moves. If you're thinking more classic console gamepad approach, then the shooting angles are going to be more conditional -- only shooting straight up when stationary, only shooting down when falling, and so forth.
 
J

JohnSebek

Guest
There are some questions you're going to want to answer before moving forward:
How do you want to do aiming input?
What method of input are you leaning towards? Keyboard and mouse? Gamepad?
Do you want to limit the directions a player can shoot while moving or when stationary?

There are different ways to tackle this that depend on how you want the player to aim and what methods you intend to allow for player input. If you're thinking more keyboard and mouse, you could let the player aim using the mouse (within reason, depending on sprite assets). If you're leaning towards gamepad input, for example, you may or may not want to use 2 thumbsticks, one that only aims, and the other that aims and moves. If you're thinking more classic console gamepad approach, then the shooting angles are going to be more conditional -- only shooting straight up when stationary, only shooting down when falling, and so forth.
Well, the aiming input should be on arrows and movement on WSAD. And the game controlled on keyboard and mouse (but the mouse is not used for shooting)
And by the last thing I am not sure, the game would be harder when you could only shoot while standing still, but the game would be more longe and challenging. So if I could try both things I will then decide what to use.
 

CMAllen

Member
If you're aiming with one set of controls and moving with another, then you can check the shooting axis similar to the same way top-down games typically check for the character's facing direction:

Code:
var axis_h = keyboard_key(vk_right)-keyboard_key(vk_left);
var axis_v = keyboard_key(vk_down)-keyboard_key(vk_up)

shoot_angle = round(point_direction(0,0,axis_h, axis_y)/45);
switch(shoot_angle)
{
    case 1: spr_UpRight; break;
    case 2: spr_Up; break;
    case 3: spr_Upleftt; break;
    case 4: spr_Leftt; break;
    case 5: spr_DownLeft; break;
    case 6: spr_Down; break;
    case 7: spr_DownRight; break;
    default: spr_Right; break;
}
(fixed copy/paste fail)
 
Last edited:
J

JohnSebek

Guest
If you're aiming with one set of controls and moving with another, then you can check the shooting axis similar to the same way top-down games typically check for the character's facing direction:

Code:
var axis_h = keyboard_key(vk_right)-keyboard_key(vk_left);
var axis_v = keyboard_key(vk_down)-keyboard_key(vk_up)

shoot_angle = round(point_direction(0,0,axis_h, axis_y)/45);
switch(shoot_angle)
{
    case 1: spr_UpRight; break;
    case 2: spr_Up; break;
    case 1: spr_Upleftt; break;
    case 1: spr_Leftt; break;
    case 1: spr_DownLeft; break;
    case 1: spr_Down; break;
    case 1: spr_DownRight; break;
    default: spr_Right; break;
}
OOO i see! Now i know what to do! thank you very much :)
 
J

JohnSebek

Guest
Just be sure to look closer at the switch/case and don't do the copy/paste fail that I did (but corrected).
Also, just a quick ask, when the player will move it will set the walking sprite and when player starts shooting player will stop and show the animation something like:
if keyboard check pressed W movement speed = 0 image index = image the pressed key
 
Last edited by a moderator:
Top