1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 4-Direction Sprite Face Mouse?

Discussion in 'Programming' started by Inamortus, Jun 25, 2016.

  1. Inamortus

    Inamortus Guest

    Hey all,

    I've been working on a project, and have added the sprites I've made into the game, but I can't figure a way to change the sprite based on the mouse postion, ie. changing the sprite index to be spr_playerUp if the mouse's position is above the player, to spr_playerRight if it is to the right of the player. Anything I've found while searching for a solution is purely for rotating the character to follow the mouse, which obviously isn't what I want with a 4-Directional sprite.

    Would anyone be able to help me out with this?

    Thanks in advance.
     
  2. I guess it would be something like this
    Code:
    mx = mouse_x
    my = mouse_y
    
    //Right left
    if  mx > oPlayer.x
    sprite_index = spr_right
    else if mx < oPlayer.x
    sprite_index = spr_left
    
    //Up down
    if my > oPlayer.y
    sprite_index = spr_up
    else if my < oPlayer.y
    sprite_index = spr_down
    EDIT: removed the equals in >=, does not make any sense, sorry :p
    Also, consider that this works best if the player sprite origin is centered, but I think that was obvious!
     
    Inamortus likes this.
  3. Inamortus

    Inamortus Guest

    Thanks for the help; it half works. It will only change to Right/Left if the mouse is in a very specific postion, anyhwere else and it's either Up or Down. Any idea why this is?
     
  4. Surgeon_

    Surgeon_ Symbian Curator

    Joined:
    Jun 22, 2016
    Posts:
    248
    Try this:

    Code:
    //This code goes in the player object:
    
    mx=mouse_x;
    my=mouse_y;
    
    var s;
    
    switch ((point_direction(x,y,mx,my)+45) div 90) {
    
    case 1: s=spr_player_up; break;
    case 2: s=spr_player_right; break;
    case 3: s=spr_player_down; break;
    
    default: s=spr_player_left; break;
    
    }
    
    sprite_index=s;
    
    Also, if you don't understand how this works, feel free to ask.

    -Surgeon_
     
    Inamortus likes this.
  5. Snail Man

    Snail Man Member

    Joined:
    Jun 20, 2016
    Posts:
    282
    It's just a flaw in the code; try this:
    Code:
    var sprite_list;
    sprite_list[0] = spr_playerRight;
    sprite_list[1] = spr_playerUp;
    sprite_list[2] = spr_playerLeft;
    sprite_list[3] = spr_playerDown;
    dir = point_direction(x,y,mouse_x,mouse_y);
    sprite_index = round((dir)/90);
    
    
     
    Inamortus likes this.
  6. Well, because (stupid me :mad:) I forgot to specify every mouse_x and mouse_y combination. Basicly, it behaves like that because you either have a mouse_x > x and a mouse_y > y or viceversa, thing I did forget to add in the code. Try Surgeon_'s or Snail Man's solution; more efficent and less sloppy less mine. :D
     
    Inamortus likes this.
  7. Inamortus

    Inamortus Guest

    That's perfect! Thank you so much. I think I understand the basics of it. It sets a variable to different values based on which area of the screen (split into 45 degree sections) the mouse is in?

    EDIT: Also, quickly, would it later be possible to put several of these into if statements to change sprites based on a character selection variable?
     
  8. Surgeon_

    Surgeon_ Symbian Curator

    Joined:
    Jun 22, 2016
    Posts:
    248
    Yes, exactly. And if you acquire more directional sprites, you can replace "div 90" with "div 45", add some more cases and then you got turning in 8 directions. And so on... Anyway, I'm glad this worked out for you :)

    -Surgeon_
     
    Inamortus likes this.

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