1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Player direction??

Discussion in 'Programming' started by William Turner, Sep 21, 2018.

  1. William Turner

    William Turner Member

    Joined:
    Sep 2, 2018
    Posts:
    11
    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: Sep 21, 2018
  2. Kanugane

    Kanugane Member

    Joined:
    Sep 20, 2018
    Posts:
    30
    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:


    In your step event, try this:
    and finally, you want to attack in a right direction! Here's the deal: (also in step event)

    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.

    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!
     
  3. William Turner

    William Turner Member

    Joined:
    Sep 2, 2018
    Posts:
    11
    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.
     
  4. Kanugane

    Kanugane Member

    Joined:
    Sep 20, 2018
    Posts:
    30
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice