BananaPandaDemographic
Member
pretty new to programming but i'm gradually figuring it out. found out how to get my attack animation to work through states but i cant quite figure out how to attack and move at the same time and when i jump the object freezes in air
create
state
animation end
create
Code:
hsp = 0;
vsp = 0;
grv = 0.3;
walksp = 4;
hascontrol = true;
canjump = 0;
hitfrom = 0;
enum st
{
normal,
attack
}
state = st.normal
Code:
switch(state)
{
case st.normal:
{
//Player imput
if (hascontrol)
{
key_left = keyboard_check(vk_left) || keyboard_check(ord("A"));
key_right = keyboard_check(vk_right) || keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);
key_enter = keyboard_check_pressed(vk_enter);
}
else
{
key_right = 0;
key_left = 0;
key_jump = 0;
key_enter = 0;
}
//Calculate movement
var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;
//Jumping
canjump -= 1;
if (canjump > 0) && (key_jump)
{
vsp = -7;
canjump = 0;
}
//Attacking
if (key_enter = 1)
{
state = st.attack;
image_index = 0;
sprite_index = sPYattack;
break;
}
//Horizontal collision
if (place_meeting(x+hsp,y,obj_wall))
{
while (!place_meeting(x+sign(hsp),y,obj_wall))
{
x = x + sign(hsp);
}
hsp = 0;
}
if (place_meeting(x+hsp,y,oDoor2))
{
while (!place_meeting(x+sign(hsp),y,oDoor2))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;
//Vertical collision
if (place_meeting(x,y+vsp,obj_wall))
{
while (!place_meeting(x,y+sign(vsp),obj_wall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;
//Animation
if (!place_meeting(x,y+1,obj_wall))
{
sprite_index = sPYjump;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
if (sprite_index == sPYjump)
{
audio_sound_pitch(snLand1,random_range(1,1.3));
audio_play_sound(snLand1,4,false);
}
canjump = 10;
image_speed = 1;
if (hsp == 0)
{
sprite_index = sPYand;
}
else
{
sprite_index = sPYmove;
}
}
if (hsp != 0) image_xscale = sign(hsp);
break;
}
case st.attack:
{
{
//Animate
sprite_index = sPYattack;
if (image_index >= 1) && (image_index <= 3)
{
with (instance_create_layer(x,y,layer,oHitbox))
{
image_xscale = other.image_xscale;
with (instance_place(x,y,parEnemy))
{
if (hit == 0)
{
hit = 1;
vsp = -3;
hsp = sign(x - other.x) * 4;
image_xscale = sign(hsp);
}
}
}
}
}
}
}
Code:
if (sprite_index == sPYattack) state = st.normal;