Change object depth based on mouse position

Discussion in 'Programming' started by Arthur_echoes, Dec 5, 2018.

  1. Arthur_echoes

    Arthur_echoes Member

    Joined:
    Dec 5, 2018
    Posts:
    4
    Im making a game project (still learning) and im trying to make an obj "sword" change depth when the mouse is in north of the screen.

    i did it as a similar thing when i made the obj "player" change xscale on mouse position



    //xscale on cursor//
    var mouse_dir;

    mouse_dir = point_direction(o_player.x, o_player.y, mouse_x, mouse_y);
    if mouse_dir > 90 and mouse_dir < 270 {
    o_player.image_xscale = -1;
    } else {
    o_player.image_xscale = 1;
    }



    with the obj "sword" i tried a similar thing but it didn't worked



    // depth on mouse position
    var mouse_dir;

    mouse_dir = point_direction(o_player.x, o_player.y, mouse_x, mouse_y);
    if mouse_dir < 0 and mouse_dir > 0 and mouse_dir <90 and mouse_dir >270{
    object_set_depth (o_sword,1)
    } else {
    object_set_depth (o_sword,0)
    }
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    2,923
    You're asking
    if mouse_dir is less than 0 and more than 0 at the same time.
    and
    if mouse_dir is less than 90 and more than 270 at the same time.

    You might notice that this is physically impossible to achieve unless you had some sort of quantum computer.
    This causes your statement to always return false, setting the depth to 0.

    Also, where exactly is your north? From the code you supplied, you'd rather check for east.

    Is there a reason you don't want to just check if the mouse is above the player, then it is in the north technically speaking.
     
  3. Arthur_echoes

    Arthur_echoes Member

    Joined:
    Dec 5, 2018
    Posts:
    4
    i know, i tried every single number and it didn't worked, disconsider that line
     
  4. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    2,923
    Consider this
    [​IMG]

    You want north. North begins at 0 and ends at 180.
    Code:
    var mouse_dir;
    
    mouse_dir = point_direction(o_player.x, o_player.y, mouse_x, mouse_y);
    if (mouse_dir > 0 and mouse_dir < 180)
       object_set_depth (o_sword,1);
    } else {
       object_set_depth (o_sword,0);
    }
    
     
    Arthur_echoes likes this.
  5. Arthur_echoes

    Arthur_echoes Member

    Joined:
    Dec 5, 2018
    Posts:
    4
    oooh i tought the 0 degree was at the north...
    i tried to use this line"" if (mouse_dir > 0 and mouse_dir < 180)""
    but with 270º and 90º
     
  6. Arthur_echoes

    Arthur_echoes Member

    Joined:
    Dec 5, 2018
    Posts:
    4
    it didn't worked, but with that information i can make it work some way p_playernotwork.PNG splayernotwork.PNG
     

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