Animation Aim with Arrow keys

W

WizardBones

Guest
Hey guys, I'm an artist trying to get some controls worked out so I can work on game animations.
I am wondering how I could change animations with the up or down arrow buttons. A type of index perhaps? when you press up his aim animation will angled, press up again even more angled, and 3rd press he is aiming his max. And the obvious down would make him cycle back through animation index. until you reach final down aim. If you hold either up or down it will cycle through them all depending on the key pressed. I have NO idea how to even begin, remember I'm just an artist, not making a game yet persay, but I wanna see how my sprites look in action : )
I assume splitting the body in two sections, so walking will be separate, just want the character to arm as i press button, I see lots for follow mouse, but I am after a more old school approach, it suits my drawings. Contra or Ghost busters II for the NES are basically the same concept. Thanks for reading.

EDIT: Maybe not needed but I will add the code from the tutorial I been following, maybe familiar to you. but help make things work right if not

/// Platform Physics

var rkey = keyboard_check(vk_right);
var lkey = keyboard_check(vk_left);
var jkey = keyboard_check(vk_up);

// Check for ground
if (place_meeting(x, y+1, obj_solid)) {
vspd = 0;

// Jumping
if (jkey) {
vspd = -jspd;
sprite_index = spr_player_stand;
}
} else {
// Gravity
if (vspd < 10) {
vspd += grav;
}
}

// Moving Right
if (rkey) {
hspd = spd;
}

// Moving Left
if (lkey) {
hspd = -spd;
}

// Check for not moving
if ((!rkey && !lkey)|| (rkey && lkey)) {
hspd = 0;
}

// Horizotal collisions
if (place_meeting(x+hspd, y, obj_solid)) {
while (!place_meeting(x+sign(hspd), y, obj_solid)) {
x+= sign(hspd);
}
hspd = 0;
}

// Move Horrizontally
x += hspd;

// Vertical collisions
if (place_meeting(x, y+vspd, obj_solid)) {
while (!place_meeting(x, y+sign(vspd), obj_solid)) {
y+= sign(vspd);
}
vspd = 0;
}

// Move Vertically
y += vspd;

//Control The Sprites
/*if(vspd != 0)
{
sprite_index = spr_player_jump;
image_speed = 0;
//use the next line if you have a falling animation as well but the falling animation should be the second one
//image_index = y>yprevious;
}

else
{
*/
if(hspd != 0)
{
sprite_index = spr_player_walk;
image_speed = .3;
}
else if(hspd = 0)
{
sprite_index = spr_player_stand;
}
//}
if !(place_meeting(x,y+1, obj_solid)){sprite_index=spr_player_jump}

//Control the direction that the player is facing
if(hspd > 0)
{
image_xscale = 1;
}
else if (hspd < 0)
{
image_xscale = -1;
}
 
Last edited by a moderator:

obscene

Member
Generally, you have a variable that you control with your up and down keys...

if key_up { angle ++ }
else if key_down { angle -- }
angle=clamp(angle,min,max);

Where min and max are probably something like -3 and 3 with 0 being "centered."

If you want to be able to hold up or down to cycle through them, the problem is that it will move too fast. So you would introduce a delay when a button is pressed and then the delay causes the rest of the code to be skipped...

if delay { delay--; exit; }
if key_up { angle ++; delay=5; }
else if key_down { angle --; delay=5; }
angle=clamp(angle,min,max);

Then in your animation you take that angle value and decide which image_index to apply...

switch (delay)
{
case 0: sprite_index=spr_aim_straight; break;
case 1: sprite_index=spr_aim_up_slightly; break;
etc.
}

Should be something similar to that.
 
W

WizardBones

Guest
AWESOME, I just got home, sorry for the late reply. I will try that out asap and let you know how its working out for me : )
 
W

WizardBones

Guest
I am getting a couple errors saying "Cannot use function/script name for variable min (also the same error for max). This is what I put together from what you were explaining. Almost correct? or way off? lol

var upkey = keyboard_check(vk_up);
var downKey = keyboard_check(vk_down);
var min = -3;
var max = 3;

//Aiming

if delay { delay--; exit; }

if downKey{ angle ++; delay=5; }
else if upKey{ angle --; delay=5; }
angle=clamp(angle,min,max);

switch (delay)
{
case 0: sprite_index=spr_aim_straight; break;
case 1: sprite_index=spr_aim_up_slightly; break;
}
 

Attachments

obscene

Member
When I wrote min and max in there I meant for you to put your values like -3 and 3, depending on how many sprites you have going up and down.

The error is caused because min and max are reserved names in GM as they are functions ( min() and max() )
 
W

WizardBones

Guest
Sweet thanks for the info dude! appreciate the patience lol
I changed the min and max according to the sprites, wondering if "case: -1" would be correct way to find the -1 value?

Aded delay = 0; to the event

I get no errors, but dont see the up and down animations change with the full code , I think the part where he is "standing" is messing with it. I think I need to split the body at this point... hmm im gonna assume what we have put together is correct until i figure out how to have a two part sprite. Thanks man! I will get back to you here once I figured that out, if thats ok?
 
Last edited by a moderator:
W

WizardBones

Guest
I adjusted the delay values to suit how many sprites there were. if you press up and hold it, it will stay at the sprite, and flick back to 0 again (straight sprite). Which is cool, but i dont see a delay between 0 and 2, it goes directly from 0 to 2, so i guess delay not working right. ALso when you press down and release the key it stays as the looking down -1. So im not sure about any of it, here is the code as of now.

var upKey = keyboard_check(vk_up);
var downKey = keyboard_check(vk_down);

//Aiming

if delay {delay--; exit;}

if upKey{ angle ++; delay=2;}
else if downKey{ angle --; delay=-1;}
angle=clamp(angle,-1,2);

switch (delay)
{
case -1: sprite_index=spr_aim_down_slightly; break;
case 0: sprite_index=spr_aim_straight; break;
case 1: sprite_index=spr_aim_up_slightly; break;
case 2: sprite_index=spr_aim_up_final; break;
}
 
Last edited by a moderator:

obscene

Member
Your confusing delay and angle. Delay should be set to a positive value always so that it can prevent the block from running. with the first line.

Only angle should be decreased or increased, and the switch is supposed to determing the sprite based on the angle, not the delay.
 
W

WizardBones

Guest
Ahhh yes of course ! lol duh
Thanks for your time man, by the looks of the game trailer I seen from windy hill studios, you are rather busy! such a cool looking game man!
 
Top