Game Development Reference
In-Depth Information
Finally, we need to fix the image so that it can turn in the direction of our choice, instead of just
moving backward as moonwalking. Here's how we can do that:
local _W, _H = love.graphics.getWidth(), love.graphics.getHeight()
local dirX, speed = 1, 10
local theImage, x, y
function love.load()
theImage = love.graphics.newImage("myImage3.png")
x = 50
y = 50
love.graphics.setColor(0,0,0,255)
love.graphics.setBackgroundColor(255,255,255,255)
love.graphics.draw(theImage, x, y, 0, dirX, 1)
local key = love.keyboard.isDown
if key("up") then
y = y - dirY
elseif key("down") then
y = y + dirY
elseif key("left") then
x = x - dirX
dirX = −1
elseif key("right") then
x = x + dirX
dirX = 1
end
end
This allows us to achieve the effect we want without creating a new sprite. We've done this by
manipulating the scale factor. The draw function can take quite a few parameters:
love.graphics.draw(imgObject, x, y, angle, sx, sy, ox, oy, kx, ky)
Here, sx and sy are what we are interested in. By keeping them at 1, we are not changing the size
of the image, but by setting it as 1 or −1, we can flip the images, so −1 is flipping the image on the
horizontal axis and −1 on the vertical. Figure 11-6 shows how this mirrors our image on the x-axis.
Search WWH ::




Custom Search