Player direction??

W

William Turner

Guest
Something that has been bothering me for a while now is getting the players direction...honestly its bugging the hell out of me...

All I got for movement is this
Code:
//Movement
if keyboard_check(ord("D")){
    sprite_index = s_player_right
    x+=4
}
if keyboard_check(ord("A")){
    sprite_index = s_player_left
    x-=4
}
if keyboard_check(ord("S")){
    sprite_index = s_player_down
    y+=4
}
if keyboard_check(ord("W")){
    sprite_index = s_player_up
    y-=4
}
as for why I need the direction...
Code:
if keyboard_check_pressed(vk_space){
    create_hitbox(s_hitbox_test,x,y,Direction??,300)
}
I need the direction so that if the player turns then the hitbox will be created in the correct direction
Code:
///@arg sprite
///@arg x
///@arg y
///@arg direction
///@arg life

var SPRITE = argument0
var X = argument1
var Y = argument2
var Direction = argument3
var Life = argument4

var HITBOX = instance_create_layer(X,Y,"Instances",obj_hitbox)
HITBOX.sprite_index = SPRITE
HITBOX.image_angle = Direction
HITBOX.alarm[0] = Life

return HITBOX
ive tried a few things but nothings worked so far...

Any and all help is appreciated.
 
Last edited by a moderator:
K

Kanugane

Guest
There's a lot of things that can be done in a better way. Let's start first with your movement. We can optimize it by creating a new variables first, hspd and vspd which are horizontal speed and vertical speed. Both are hspd = 0; vspd = 0;
Now that we have new variables, let's tweak your movement code:

In create event, you should initialize these variables:
//Initialize horizontal and vertical speed
hspd = 0;
vspd = 0;
//Initialize our speed limit
maxhspd = 4;

//Initialize our acceleraction and deacceleration
acceleration = 2;
friction_ = 1;


In your step event, try this:
// Horizontal Movement
var hinput = keyboard_check(ord("D")) - keyboard_check(ord("A"));
//We check if we move or not. If we move, then we execute code below
if hinput != 0{
// We begin to move and accelerate
hspd += hinput*acceleration;
//This is our speedlimit. We won't become a sonic.
hspd = clamp(hspd, -maxhspd, maxhspd);
} else {
//If we no longer move, we will slow down.
hspd = lerp(hspd, 0, friction_);
}


//Apply our movement changes to player's x and y coordinates.
x += hspd;
y += vspd;
and finally, you want to attack in a right direction! Here's the deal: (also in step event)

//Create a temp.variable which will determine which side player is moving and facing
var atkdir = 1*sign(hspd);
if (keyboard_check_pressed(vk_space)){
instance_create_depth(x+atkdir,y,100,obj_hitbox);
}
Now, there's a thing you need to know. If you want to create a hitbox, you also want it to vanish as soon as its done. So in obj_hitbox, on create event make instance_destroy(), alternatively you may use alarms instead for this.
Also, with this code, you'll have to modify your sprites/animations. I suggest to put it in a separate script and after x+= hspd and y+= vspd variables.

if (hspd > 0){
index_sprite = s_player_right;
}
if (hspd < 0){
index_sprite = s_player_left;
}
if (hspd == 0){
index_sprite = s_player_idle;
}
Now, you may notice that I did not include vertical movement. Here is the joy of coding. You should try and do that yourself by using the same code logic I've given to you. I hope this helps you!
 
W

William Turner

Guest
With the code you provided I got the movement working just fine both vertically and horizontally the only problem is the attack portion does not seem to work...it does not give any errors its just nothing appears even with the proper object in the project...I hate having to continue to push my questions on others but at the moment its all I can do with my knowledge.
 
K

Kanugane

Guest
Hmm. Let's see.. It seems that I've made a small mistake myself and you should have placed instance_destroy(); in step event of your hitbox object. Alternatively, you could use a post-draw event and put instance_destroy(); there. What it does, it executes a comand after its sprite has been drawn on screen. I personally am using step event and unfortunately, I do not remember why I stopped using post_draw. Probably there was some issue.
 
Top