Discord
Login
Community
DARK THEME

How to change sprite animation depending on direction

Hey. This is my first time using this. Or making any game and code in general. I was just wondering how to change the animation or sprite whenever the character moves. I already have the animations set out for the directions, I just can't figure out how to make it so that they play whenever the corresponding button is pressed.

https://microstudio.dev/i/gilles/spriteanimation/

If your player is a class it is best to replace the sprite that is to be drawn.

Player = class
  constructor = function( )
     left = sprites["player_walk_left"]
     right = sprites["player_walk_right"]
     ....
     draw_sprite = left
     direct_x = -1
  end

  update = function()
    if keyboard.LEFT then 
       draw_sprite = left
       dirext_x = -1
    end
    if keyboard.RIGHT then 
       draw_sprite = right
       dirext_x = 1
    end
  .....
  end
  
  draw = function()
    screen.drawSprite( draw_sprite, x, y , ..... )
  end
end

What do you mean by "class"? I'm sorry, I'm still new to all this stuff.

I just used if direction=1 then screen.draw(sprite left) end if direction=2 then screen.draw(sprite right) end

@Oliver TheWise

A class is a container for data (fields) and methods (functions).

In addition, a class is created dynamically (memory is allocated to this structure) so you can create several instances of the same class and assign these instances to variables.

e.g.

Actor = class
   constructor = function( pos_x, pos_y )
       this.x = pos_x
       this.y = pos_y
   end
end

enemy1 = new Actor( 10, 20 )
enemy2 = new Actor( 321, 432 )

print( enemy1 ) 

As loginus said, it's most definitely best to replace the character when you want it to move in a certain direction.

  1. Make seperate characters in the drawing panel, each facing a different direction.

  2. Name them differently depending on which direction they face, it doesn't really matter what you name them as long as they are different.
    (e.g. "player_left", "player_right").

  3. This is an example of what it may look like:

player = class
   constructor = function()
      sprite = "" 

      // there would be x, y variables here.
   end
  
   update = function()
      if keyboard.RIGHT then 
         sprite = "player_right"
      end

      if keyboard.LEFT then 
         sprite = "player_left"
      end

      if keyboard.UP then 
         sprite = "player_up"
      end

      if keyboard.DOWN then 
         sprite = "player_down"
      end
   end

   draw = function()
      screen.drawSprite(sprite, x, y...)
   end
end

init = function()
   player = new player()
end

update = function()
   player.update()
end

draw = function()
   player.draw()
end

This method is targeted towards beginners which i am assuming you are. This method assumes that you already have just a little knowledge of what classes are, if not then i highly suggest looking at the documentation or using a tutorial in the tutorial panel, they are very useful and are very crucial for developing things on this website.

You also do need to make the sprites yourself if you didn't already know.

You can also use objects to achieve pretty much the same thing, i dont feel like explaining them here but if you are going to use them a good heads up is that objects are not too different from classes, which is something i wish i knew earlier on lol.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community