Game Development Reference
In-Depth Information
function love.update(dt)
x = x + dirX
y = y + dirY
if x < 0 or x > _W then dirX = − dirX end
if y < 0 or y > _H then dirY = − dirY end
end
At first we get the width and height of the window. This could be different on different systems, or it
could be set via the conf.lua file. We get this using the getWidth and getHeight functions, so that
we know what the size is at this instance. We save the width and height in the _W and _H variables.
Next we set the speed of movement; in this case, we're setting the dirX and dirY variables to 10
and 10. This shall be used to change the position of the image at each update call.
In the function love.load , which is called only once, when the app starts, we load the image and
set it to the variable theImage , and set the variables x and y to 50 and 50, which will be the starting
position for this image.
In the love.draw function, we position the image at x , y .
In our love.update function, we increment the x and y by the dirX and dirY , and then we check if
the position of x or y is outside of the bounds, 0,0 or width , height of the window. If the x or the y
coordinates are outside of the window's bounds, we invert the dirX or the dirY accordingly, which
results in creating a bounce effect and places the image back within the bounds of the window.
The Active Window
The love.focus function can be used to determine if the window is active and has focus or not. This
could be used, for example, to stop processing when the current window loses focus and continue
accordingly when it regains focus again.
We add a line at the top of our existing code from above:
local current = true
And then we simply add a function at the end of the code:
function love.focus(f)
current = f
end
Finally, we redefine the update function as follows:
function love.update(dt)
if current then
x = x + dirX
y = y + dirY
 
Search WWH ::




Custom Search