Pong | Handling the paddles leaving the screen.
There are two ways of handling collision events with Defold. We could check the positions of objects in code and then change their attributes to modify their behaviour. Alternatively we could create what are known as collision objects that send messages to an object when they are hit.
The collision object approach would require us to create some game objects for invisible walls at the edge of the screen (game objects with no sprites): one at the top, one at the bottom, one at the left goal and one at the right goal. When an object hits one of these walls a message would be sent to it which we can check in the on_message function.
With pure code we won't need to create any additional objects, and instead we write the new game logic in the update function where the objects move.
With the paddles and the movement of the ball around the arena we will use pure code, and to bounce the ball off the paddle we will use collision objects. It is entirely your choice how you approach this in your game. Generally collision objects will be easier as your game becomes more complex, but for simply checking the x and y position of an object it will be quicker to develop just using code. We'll use both methods in this game so that you can appreciate the different approaches.
- In the assets panel, double click the player1.script file. In the update function, change the code to include checking if the paddle is about to leave the screen before setting it's new position.
function update(self, dt)
local pos = go.get_position()
pos.y = pos.y + self.move_distance * dt
-- Prevent the paddle leaving the screen
if pos.y<50 then
pos.y = 50
end
if pos.y>590 then
pos.y = 590
end
--
go.set_position(pos)
self.move_distance = 0
end
Firstly, notice we are using comments to identify the new section of code. It makes it easier for you to see where the changes to this function are, and therefore it would make it easier for other people reading your code too!
The origin (0,0 position) of the screen is in the bottom left corner. The x and y position of an object has an origin at the centre of the object. The height of the screen is 640 pixels (set in the game.project file). The height of the paddle is 100 pixels (you can check this in the atlas).
Therefore, if the paddle's y position is less than 50 it will be off the bottom of the screen (0 + paddle height/2). If the paddle's y position is more than 590 it will be off the top of the screen (640 - paddle height/2).
Six simple lines of code is easier than using collision objects here.
- Now apply the same change to the player 2 script.
Note you can open scripts by double clicking them either in the assets panel or the outline panel. Either way it just opens the script file in the editor. - Save the changes by pressing CTRL-S or 'File', 'Save All'.
- Run your program. The paddles for both players should no longer leave the screen. However, the ball still flies away!
The next stage is to get the ball bouncing. Stage 6b >