[Solved]Making a character switch images when pressing a button to move

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
TurtlyDz
Prole
Posts: 2
Joined: Thu Sep 24, 2020 8:04 pm

[Solved]Making a character switch images when pressing a button to move

Post by TurtlyDz »

In the game I am trying to make, I have two character images one of the character looking left and one looking right. I already have the image that looks right walking around but I want to be able to switch the image when I press 'a' to go left. If you need me to attach the game files just tell me. I am really new to Lua and LOVE and I would appreciate your help. Thank you.
Last edited by TurtlyDz on Thu Oct 01, 2020 12:09 am, edited 1 time in total.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Making a character switch images when pressing a button to move

Post by sphyrth »

TurtlyDz wrote: Wed Sep 30, 2020 8:34 pm If you need me to attach the game files just tell me.
Please do. There are several ways of solving your problem.
Xugro
Party member
Posts: 114
Joined: Wed Sep 29, 2010 8:14 pm

Re: Making a character switch images when pressing a button to move

Post by Xugro »

I attached a small example. But as sphyrth sad: This is just one of many solutions.

The basic idea is mostly the same: Save the direction the character is looking in some variable and read it when you draw the image.
Attachments
left_and_right.love
(82.28 KiB) Downloaded 144 times
TurtlyDz
Prole
Posts: 2
Joined: Thu Sep 24, 2020 8:04 pm

Re: Making a character switch images when pressing a button to move

Post by TurtlyDz »

I have just realized how to do it. I was thinking that I had to change images when I pressed a button but I found a function that just flips a character over the y axis:
if self.direction == 'right' then
scaleX = 1
else
scaleX = -1
end
love.graphics.draw(player_right, math.floor(player_rightX), math.floor(player_rightY), 0, scaleX, 1, self.width / 2, self.height / 2)

Thank you for trying to help! Honestly I thought it would;d take a while for someone to respond. Thank you again.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests