Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Making the player sprite walk in different directions

What would be the syntax I need to write in Rubyscript in order to make the player face in different directions? Apparently this doesn't seem to work:

Code:
#Making the player face and move right when the right arrow key is pressed

 if Input.dir4 == 6 #if right arrow key is pressed

      self.direction = 6 #player faces right
      self.x += 2 #move player right
end

I get an error saying that self.direction is an invalid syntax. So what would be the right syntax to use to make the player face in the direction?
 
Hasn't RPG Maker already got a function for this? If your trying to do more than 8 movement sprites then I'd say theres something wrong with the 6 value. I'm not much of a scripter though so I can't be too certain.
 
Yes, for general direction, it does. But I'm making a new class that has its own movement. It's just the standard up,right,left,down movement. No diagonals. Do I need to include a superclass in order to use it? or can I not create that on my own?
The 6 value is the 6 on the NumPad which is the right arrow key. It's explained in the help file. But it doesn't say how to actually move or turn a sprite in a direction using RGSS script.


The code above was from a class I created called Hero_Sprite.
 
Here is the entire class for the Hero_sprite/player that I want to do:

Code:
class Hero_Sprite < RPG::Sprite 
  
  def initialize
    super()
    self.bitmap = RPG::Cache.character("001-Fighter01", 0)
    self.ox = self.bitmap.width/8
    self.src_rect.set(0,0,32,48) 
    self.x = 300
    self.y = 300 
  end
  

  def update

 #Move the player Right
  if Input.dir4 == 6 
    self.x += 2

    #walk animation code goes in here somewhere

  end
 
    #Move the player Left
  if Input.dir4 == 4 
        self.x -= 2
 
 #walk animation code goes in here somewhere

  end

end

end

I've looked all over google trying to find the code in RGSS that would make a character do a walk animation when you press a button, but I can't find it anywhere. I saw the class for Sprite_Animation and such, and I tried to use it in the code, but it wouldn't work even when I defined the superclass at the top of the code... I'm not sure what I'm doing wrong.
 
It's because using self. is telling ruby to use @direction from Hero_Sprite, not from the Game_Character class.
Try putting it under the Game_Player class, and defining it as the update method... or, you could even just define move_up/left/right/down in Game_Player and set them to move a shorter distance, like...
def move_up(turn_enabled = true, dist = 1)
Then you could call it with move_up(true, 2) in the update section of game_player, but it would still use the normal amount when being called in a move route >.>

Are you trying to make the hero move 2 spaces, or 2 pixels between 2 grid spaces? Because you'd have to change the map to not use 32x32 grids for the latter. @x += 1 would move the player one full grid to the right >.>
 
How do I include Game_Player in the Hero_sprite class? I get an error when I write:

Code:
class Hero_Sprite < Game_Character < Sprite

or:


Code:
class Hero_sprite < Sprite < Game_Player

If I don't include the Sprite class, then I get an error due to me using bitmaps in the class. If I don't include Game_Player, then I get an error because I'm trying to call that function...
 

khmp

Sponsor

[edit]Correction

You need a sprite object which contains within it, a bitmap. My apology and again good luck! :thumb:

@hero = Sprite.new
@hero.bitmap = RPG::Cache.character("001-Fighter01", 0)

And don't forget to dispose of it when you're done.

@hero.bitmap.dispose
@hero.dispose
 
okay, I think I just screwed something up. Now Im getting more errors... What if I just start over from scratch? All I want to do is just make a sprite walk and do the walk animation when it walks. How can I do this from scratch? or is it impossible?
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top