8 Directional Movement: Sprites | GMS2 [2]

Поділитися
Вставка
  • Опубліковано 8 лют 2025
  • We continue on from our previous tutorial and hook up our sprites to our movement system.
    Sprites download:
    1drv.ms/f/s!Am...
    Sprites credit:
    Adapted from TheNess "RPG sprite - 8 direction human male 16x16": opengameart.or...
    Here I am in other places:
    FriendlyCosmonautDev channel: / @friendlycosmonautdev8040
    Twitter: / friendly_cosmo
    Patreon: / friendlycosmonaut

КОМЕНТАРІ • 111

  • @FriendlyCosmonaut
    @FriendlyCosmonaut  6 років тому +18

    I'm going to be a bit busy in January, but did plan to do another follow-up to this, showing how to use a single sprite sheet (similar to the system used in the Farming RPG). This will be a full video with explanations when I have the time, but for those interested now, here is the code for it:
    pastebin.com/RRUP4Yzv
    And Happy Holidays, everyone!

    • @rpgtogether7022
      @rpgtogether7022 5 років тому +1

      It's March now please upload a video.... you're my only support in my game development...
      PLEASE... 😟

  • @bunjoe09
    @bunjoe09 5 років тому +29

    This question has just been floating around in my head for a long time now. You know so much about GML, what is your story with GML? How did you learn how to code with it? Because after such a long time looking, you are still the only reliable source and teacher for learning this stuff.

    • @bunjoe09
      @bunjoe09 3 роки тому +2

      @Douglas Kian bruh this is a year old comment...

    • @simaogoncalves07
      @simaogoncalves07 3 роки тому +3

      @@bunjoe09 2 years ;)

    • @bunjoe09
      @bunjoe09 3 роки тому +2

      @@simaogoncalves07 Even better.

    • @inqurity
      @inqurity 2 роки тому

      There's not much stuff to learn, GML is just your basic programming language like GDScript

    • @georgehennen
      @georgehennen Рік тому

      4 Years

  • @thomasredmane
    @thomasredmane 5 місяців тому +4

    For anyone watching this 5+ years later:
    To get your Idle sprites working, I used "sprite_index" in my second switch statement instead of "dir" and it works just fine. Here's an example:
    (Please note: I used "image_xscale" to swap sprite directions)
    //Idle Animation
    switch(sprite_index)
    {
    case(spr_player_walk_down): sprite_index = spr_player_idle_down; break;
    case(spr_player_walk_side): sprite_index = spr_player_idle_side; break;
    case(spr_player_walk_side):
    {
    if (image_xscale = -1) //This check if the player is facing the other direction (180 degrees)
    {
    sprite_index = spr_player_idle_down;
    image_xscale = -1;
    }
    break;
    }
    case(spr_player_walk_up): sprite_index = spr_player_idle_up; break;
    }

    • @DaniRevOfficial
      @DaniRevOfficial 5 місяців тому +2

      I'm watching this 5 years later, and you helped me 3 weeks after writing that out so thank you.

    • @thomasredmane
      @thomasredmane 5 місяців тому +1

      ​@@DaniRevOfficial of course!

    • @djinni2005
      @djinni2005 2 місяці тому

      You are such a life saver!!

    • @dryopteris2996
      @dryopteris2996 27 днів тому

      Omg thanks... But I still have an issue : it is working for me, but randomly, and much better below 20 FPS... At 60 FPS, when my player stops, it automaticaly turns in the nearest direction between one of thoses directions : 0, 90, 180, 270. So I can never see my spr_player_idle_up_right etc... :/ Does somebody know why ?

  • @giannymo7027
    @giannymo7027 3 роки тому +2

    thank you so, SO MUCH! I learned more from this than the other 500 videos I watched on GMS2.

  • @Pixelarkid
    @Pixelarkid 7 місяців тому

    Your tutorials are awesome, you explain each step with visual examples!! Thank you!!

  • @lordfrz9339
    @lordfrz9339 11 місяців тому

    Spent all day tryingto figure this out, finaly found your video, ist perfect, thank you.

  • @GemuNeko
    @GemuNeko 6 років тому +1

    I have been struggling with this all weekend with no answers. I'm more of a visual learner, you are a life saver ;-;

  • @phoenixproject298
    @phoenixproject298 6 років тому +23

    Will you do a collision tutorial for this type of movement?
    I have already made a type of collision but it is not quite good, since sometimes you get stuck in the corners of the walls, I would like to know if you have planned to do it. You do very good tutorials by the way :P thanks for sharing your work with all of us!

    • @emanu1674
      @emanu1674 Рік тому

      It's a little late, but you can try this:
      Remove the "x += moveX" and "x += moveY" from the code and put this on its place:
      // Colision for X axis
      if(!place_meeting(x+spd, y, obj_Collision) && hInput == 1){
      x += moveX;
      }
      if(!place_meeting(x-spd, y, obj_Collision) && hInput == -1){
      x += moveX;
      }

      // Colision for Y axis
      if(!place_meeting(x, y+spd, obj_Collision) && vInput == 1){
      y += moveY;
      }
      if(!place_meeting(x, y-spd, obj_Collision) && vInput == -1){
      y += moveY;
      }
      If you need to check more than one collision object, create a single object to check for collision and then parent it to all the other collision objects you want to have.

  • @sternchen5988
    @sternchen5988 6 років тому +2

    Thanks for the tutorial! I really like your video, keep it up!

  • @porkwoofles3909
    @porkwoofles3909 5 років тому

    Tight tutorial. Well edited/voiced. Thanks a lot!

  • @Gifisha
    @Gifisha 5 років тому +1

    Every video I watch of yours... So good O.o

  • @jakeq3530
    @jakeq3530 3 роки тому +4

    A collision tutorial for this video would be AMAZING.

    • @emanu1674
      @emanu1674 Рік тому

      It's a little late, but you can try this:
      Remove the "x += moveX" and "x += moveY" from the code and put this on its place:
      // Colision for X axis
      if(!place_meeting(x+spd, y, obj_Collision) && hInput == 1){
      x += moveX;
      }
      if(!place_meeting(x-spd, y, obj_Collision) && hInput == -1){
      x += moveX;
      }

      // Colision for Y axis
      if(!place_meeting(x, y+spd, obj_Collision) && vInput == 1){
      y += moveY;
      }
      if(!place_meeting(x, y-spd, obj_Collision) && vInput == -1){
      y += moveY;
      }
      If you need to check more than one collision object, create a single object to check for collision and then parent it to all the other collision objects you want to have. It also works for corners if you're in a pinch, but the character won't slide on it.

  • @erickdalarivadeabreu
    @erickdalarivadeabreu 3 роки тому

    Tysm, FriendlyCosmonaut

  • @DeitiesDogma
    @DeitiesDogma 5 років тому +4

    Great video thank you! Can we get a video with collisions and this type of movement?

  • @Mystical-TEDDY_
    @Mystical-TEDDY_ 3 роки тому +2

    Im SOOO confused with the idle sprite part its not working when i put the same switch thing but then make it "idle" instead of right

  • @ereasai9217
    @ereasai9217 6 років тому

    using the point_direction was very cool!

  • @josiahbrosnan973
    @josiahbrosnan973 4 роки тому

    Creat teaching! Really helps! Thank you!

  • @shubhanshukumar9675
    @shubhanshukumar9675 5 років тому

    WOW so much awesome this helped me a lot

  • @PlaskiJakDeska
    @PlaskiJakDeska 4 роки тому +1

    Aaaaand I'm subscribed.

  • @laptopgamer9357
    @laptopgamer9357 3 роки тому +4

    Thanks, if i ever able to publish my game i mention you in my credits,your coding is easy,so i can do coding and im just 11 years old

  • @meganoob9281
    @meganoob9281 6 років тому +1

    yes, brilliant!

  • @nycto3488
    @nycto3488 3 роки тому +2

    Does anyone know why my sprite stopped it's idle animation when it's not moving? And could anyone help me fix it?

  • @satuhogosha
    @satuhogosha 6 років тому +5

    What exactly do you mean (3:46) by "Note: if you have idle sprites , you would put another switch statement here instead, in the same format as before!" . I'm struggling to get the idle animation working with your tutorial.

    • @JohanKarlemo
      @JohanKarlemo 6 років тому +2

      if (hinput == 0) and (yinput == 0){
      sprite_index = Idle;
      }
      else
      {
      THE SWITCH STATEMENT;
      }

    • @satuhogosha
      @satuhogosha 6 років тому

      @@JohanKarlemo thank you

    • @anthonypismarov
      @anthonypismarov 5 років тому

      @@JohanKarlemo I'm having the same problem. This doesn't work for me. My character continues plays his run animation or spins in circles playing the idle animation depending on how I do it. Why does she add the idle code after the switch statement and you do it before? Would you mind explaining further?

    • @JohanKarlemo
      @JohanKarlemo 5 років тому

      @@anthonypismarov Have you set image_speed to 0 when you change into the idlesprite?

    • @grizzlymiller8694
      @grizzlymiller8694 5 років тому

      I have idle animations for all 8 directions and this doesnt work for me. i tried all sort of combinations to get it to idle... none are working. Would it be better to have each instance of movement have the walk sprite activate, like
      if (keyboardcheck_pressed(vk_right)) {Sprite_Index = player_walk_r}
      if (keyboardcheck_released(vk_right)) {Sprite_Index = player_idle_r}
      ??

  • @Kepler2-b
    @Kepler2-b 3 роки тому

    thanks your are genius

  • @KyleMortPowers
    @KyleMortPowers 3 роки тому +3

    I'm having difficulties with the code. I am trying to use some sprites I made for when you are idle, but the game won't run. How exactly would it look to make it with other idle sprites? (Just so I can fix my problem)

    • @runabyt3
      @runabyt3 3 роки тому +1

      Would love to know an answer to this too

    • @jakeq3530
      @jakeq3530 3 роки тому

      3:47

    • @jakeq3530
      @jakeq3530 3 роки тому

      @@runabyt3 3:47

  • @bryankelly3300
    @bryankelly3300 5 років тому +3

    my left dir didn't work, it would take whatever sprite I was just on like if I was going right and would just in essence moonwalk and I can't fix it

    • @TheGrunto
      @TheGrunto 5 років тому

      Same happened to me. Any idea how to fix it?

    • @roscianyt
      @roscianyt 5 років тому

      @@TheGrunto Works here. Re-check your code.

    • @stu5632
      @stu5632 5 років тому +12

      Add to the switch statement 'switch round(dir){.......'
      The 180 direction is 180.00 for some reason. Adding the 'round(dir)' allows it to round to 180.

    • @TheGrunto
      @TheGrunto 5 років тому

      @@stu5632 Thanks this did the trick! I had a janky keyboard_check right before to patch the problem, but that seemed like poor code.

  • @duhhhmick9532
    @duhhhmick9532 2 роки тому +1

    is there a way to make the direction follows the mouse coordinates?

  • @chumorgan443
    @chumorgan443 Рік тому

    I love the results from this code but desperately needs a collision counter part.

  • @adriantenorio9036
    @adriantenorio9036 4 роки тому +1

    My character in case 180, wont move to the left using the spr_l, insted it will move to the left with the spr_r. Any help?

  • @LifeAfterBreakfast
    @LifeAfterBreakfast 6 років тому +1

    great video.

  • @danimations80
    @danimations80 4 роки тому +2

    Hot tip and nicely explained. Now how do I assign a different idle animation to every direction once my character is stationary?

    • @MierzejX
      @MierzejX 2 роки тому +2

      @tape stuff
      I'm just a beginner but here's how I've done it. In the else statement of if hInput != 0 || vInput != 0 I've put sitch statement that's looking for sprite_index. So based on her video, the example code would look like this:
      switch (sprite_index) {
      case spr_r:
      sprite_index = spr_idle_r;
      break;
      case spr_ur:
      sprite_index = spr_ur_idle;
      break;
      (...) And so on :)

    • @asddsadsad-b8b
      @asddsadsad-b8b 2 роки тому

      @@MierzejX I tried that but for some reason it's not working for me... Can you help me out?

    • @DylpickleA
      @DylpickleA 11 місяців тому +1

      Here's what I did... it's working so far. No idea if this method will have issues down the line. It seems to work though.
      //Set Sprite
      switch round(dir)
      {
      case 0: sprite_index = spr_player_right; break;
      case 45: sprite_index = spr_player_right_up; break;
      case 90: sprite_index = spr_player_up; break;
      case 135: sprite_index = spr_player_left_up; break;
      case 180: sprite_index = spr_player_left; break
      case 225: sprite_index = spr_player_left_down; break;
      case 270: sprite_index = spr_player_down; break;
      case 315: sprite_index = spr_player_right_down; break;
      }
      }
      //Idle Sprites
      if(hInput = 0 and vInput = 0)
      {
      switch round(sprite_index)
      {
      case spr_player_right: sprite_index = spr_player_idle; break;
      case spr_player_right_up: sprite_index = spr_player_idle; break;
      case spr_player_up: sprite_index = spr_player_idle; break;
      case spr_player_left_up: sprite_index = spr_player_idle_left; break;
      case spr_player_left: sprite_index = spr_player_idle_left; break
      case spr_player_left_down: sprite_index = spr_player_idle; break;
      case spr_player_down: sprite_index = spr_player_idle; break;
      case spr_player_right_down: sprite_index = spr_player_idle; break;
      }
      }

    • @orange5423
      @orange5423 6 місяців тому

      @@DylpickleA I was wondering about the same thing. Works for me, thank you!!!

  • @x8squish8x16
    @x8squish8x16 6 років тому

    Cousmonaut Lady is back wahoo

  • @martink5477
    @martink5477 6 років тому +1

    Could you add mouse click and movement? Then add shooting with right mouse button?

  • @purplesquirrel8803
    @purplesquirrel8803 2 роки тому

    Hey guys I need some help so I followed along and I have everything down verbatim but my sprites are coming up as blue instead of red like hers during the switch(dir) function and I was wondering what am I doing wrong ? It says error ( not set before reading it) I’m new to GML so idk what this means.

  • @aorn8437
    @aorn8437 5 років тому

    you are seriously a fucking legend, thank you

  • @lytningthief
    @lytningthief 4 роки тому +1

    Using this code for four directional movement but for some reason if I'm moving diagonally and switch to the opposite diagonal direction the sprite wont change and it looks like the player object is moonwalking. Any idea why this might be happening?

    • @scotsparaman
      @scotsparaman 4 роки тому

      post your code so we can see what is happening

    • @ploushie
      @ploushie Рік тому

      im having the same issue did you ever fix it

  • @MrKingJavo
    @MrKingJavo 6 років тому +3

    One thing I’m curious about is your switch statement. It reads to me the cases only hit if the direction is exactly at those degree values. Wouldn’t that mean it only changes sprites if you hit those exact values? I’m sure I’m missing something though.

    • @mousseauchocolatdupatron3125
      @mousseauchocolatdupatron3125 6 років тому +2

      I guess it will change sprites with those exacts values, but in that case the previous code just makes it possible to have those values. If you got a system that makes it possible, like mouse controled direction, i guess you can arrange this code with "0 degrees < dir value < 45 degrees" kind of stuff

  • @voidling2632
    @voidling2632 5 років тому

    I use unity, I know how to program, I'm just here because the sprites looked nice even though I don't like pixelart at all but really good and nice video.... and oh my gosh, gml looks so damn easy cO and I thought c# is easy...

  • @goodbyebadbye
    @goodbyebadbye 5 років тому

    Thank you😭

  • @kallishizuku7497
    @kallishizuku7497 3 роки тому

    how can i do this with the mouse in game maker 1.4? I'm looking to make a top down shooter so that the player looks in 8 directions according to the position of the mouse

  • @jakeq3530
    @jakeq3530 3 роки тому +1

    I used WASD instead using this code:
    hInput = (keyboard_check(ord("D"))) - (keyboard_check(ord("A")));
    vInput = (keyboard_check(ord("S"))) - (keyboard_check(ord("W")));
    SPRINTING:
    I then added an extra variable called
    toSprint = (keyboard_check(vk_shift));
    Then inside the Movement If statement I added an If/Else statement to check if the Shift key is being pressed and changed the spd and image_speed to a larger number, or setting it back to lower values if not being pressed.
    if (toSprint) {
    spd = 1.4;
    image_speed = 0.50;
    } else {
    spd = 0.75;
    image_speed = 0.25;
    }

    • @jakeq3530
      @jakeq3530 3 роки тому +1

      COMPLETE CODE for copypasta:
      hInput = (keyboard_check(ord("D"))) - (keyboard_check(ord("A")));
      vInput = (keyboard_check(ord("S"))) - (keyboard_check(ord("W")));
      toSprint = (keyboard_check(vk_shift));
      if (hInput != 0) || (vInput != 0) {
      dir = point_direction(0, 0, hInput, vInput);
      moveX = lengthdir_x(spd, dir);
      moveY = lengthdir_y(spd, dir);
      x += moveX;
      y += moveY;

      //Sprinting
      if (toSprint) {
      spd = 1.4;
      image_speed = 0.50;
      } else {
      spd = 0.75;
      image_speed = 0.25;
      }

      //Set Sprite
      switch(dir) {

      case 0: sprite_index = spr_r; break;
      case 45: sprite_index = spr_ur; break;
      case 90: sprite_index = spr_u; break;
      case 135: sprite_index = spr_ul; break;
      case 180: sprite_index = spr_l; break;
      case 225: sprite_index = spr_dl; break;
      case 270: sprite_index = spr_d; break;
      case 315: sprite_index = spr_dr; break;
      }
      } else {
      image_index = 0;
      }

    • @gameboy_colored
      @gameboy_colored 2 роки тому +1

      thanks for this!

    • @AdamBarkerThe
      @AdamBarkerThe 6 місяців тому

      @@jakeq3530 not sure if you will reply after this long but how would I add in my idle animations? I was trying to add them in like it says in the video but it keeps giving me errors.

  • @AlessioValentino
    @AlessioValentino 6 років тому +1

    Hi! First of all thanks a lot for your amazing job, you're great!
    Second: I was trying to implement this code in the same code we used for the farming RPG tutorial, but I the collisions just don't work, how can I fix it?

  • @Troylglover
    @Troylglover 4 роки тому

    how do I shoot in the direction I'm facing (without a mouse)

  • @Chupie77777
    @Chupie77777 6 років тому +2

    How do we add collisions with a wall object with this code? every tutorial I see adds movement and uses different variables and makes it a bit confusing with this coding.

    • @raymond4445
      @raymond4445 6 років тому

      ​@@KennedySilva06 He has a good question because her code for the collisions in her RPG video doesn't work with this code.

    • @yugurealba5009
      @yugurealba5009 6 років тому +2

      Kennedy is right, the code will looks something like this. Although this probably doesnt work since i havent test it. but i hope you get the gist out of it.
      if ! place_meeting(moveX,moveY,YourSolidObjectName) {
      x += moveX;
      y += moveY;
      }

    • @raymond4445
      @raymond4445 6 років тому

      Yeah I have something like this, but the player gets stuck in corners and I'm not sure why 😐🤔

    • @Chupie77777
      @Chupie77777 6 років тому +1

      Thanks for all the comments! I think I kinda got it, but now my enemies collide with walls just fine, but when they collide with each other, they get stuck and it looks stupid lol

    • @charliestreet7104
      @charliestreet7104 6 років тому +1

      @@raymond4445 it could be your player's hitbox getting moved between sprites, make sure your hitbox is consistent throughout all your 8 directional srites

  • @SuperCatbread
    @SuperCatbread 6 років тому +1

    Why use this dir variable with point_direction instead of the built-in direction variable?
    I'm having trouble getting these scripts to work with a following object, and that might be part of my problem.

    • @BlackBloodPoem
      @BlackBloodPoem 5 років тому +1

      Hey. It happened so that I'm now reading manual and... Well, probably that's why: "Direction is one of those properties and can be used to set the direction of movement of the instance when the instance has a speed other than 0." And we don't use speed here, but instead, manipulate x and y directly.
      And I suppose we do this because it's more
      accurate, thus more reliable way to get what we want here.
      Since you wrote this comment 2 months ago, I assume, you most likely already figured out, what's wrong.

  • @AlexTheKidd-Alex_Garlin
    @AlexTheKidd-Alex_Garlin 6 років тому

    How to set up 8 directional AI? I ain't sure what coding its on? even though I am still gml

  • @lytningthief
    @lytningthief 4 роки тому

    Great tutorial! Can this system work with a top down zelda type game? And if so, what's the logic you would use to code the attack animations? I'm having a little trouble with that part.

  • @MiguelAngelValdepenaDelgado
    @MiguelAngelValdepenaDelgado 2 роки тому

    😮

  • @Mystical-TEDDY_
    @Mystical-TEDDY_ 3 роки тому

    Is there a way to make it WASD instead of the arrow keys

    • @mightbejao5707
      @mightbejao5707 3 роки тому

      yes, just replace (vk_right/left/up/down)

    • @jakeq3530
      @jakeq3530 3 роки тому

      hInput = (keyboard_check(ord("D"))) - (keyboard_check(ord("A")));
      vInput = (keyboard_check(ord("S"))) - (keyboard_check(ord("W")));

  • @twizzle6530
    @twizzle6530 3 роки тому +1

    you are like Jesus basically

  • @КириллКирилл-я1щ
    @КириллКирилл-я1щ 5 років тому

    Hi my name is Cyril! I am your subscriber from Ukraine on UA-cam channel.
    I am writing you with help. When writing a game Rpg ran into a problem.
    My hero has a lot of weapons,
    I don’t know how to transfer the damage value of a weapon to a character
    (when he picks it up at the click of a button)
    I’ve already done pressing a button and a weapon appears in my hand.
    Have you encountered such a code or do you know any source that could help me ????
    I translated this text using google, do not laugh)))))))

  • @ducked-head5797
    @ducked-head5797 5 років тому

    ım sliding help