Pong | Creating the player 1 script for moving the paddle.
Now that player 1's paddle is moving we can repeat the process for player 2. We could use a single script to control both players. With all programs there are multiple ways to create a solution. Some are more elegant, some are more efficient, but sometimes they are just different. To introduce you to the concept of game objects we have decided to have a different script for each player even though it will pretty much contain the same code.
This time you have less support. Follow the steps below to get the paddle moving for player 2. Refer back to the previous step if you need more help.
- In the assets panel, right click the folder 'main' and choose 'New...', 'Script'. Name the script, 'player2'.
- Add code to the init function to initialise the player2 object when it spawns.
- Add code to the on_input function to set the distance to move the paddle if up or down cursor keys are pressed.
- Add code to the update function to move the paddle.
function init(self)
msg.post(".", "acquire_input_focus")
self.move_distance = 0
self.paddle_speed = 500
end
function update(self, dt)
local pos = go.get_position()
pos.y = pos.y + self.move_distance * dt
go.set_position(pos)
self.move_distance = 0
end
function on_input(self, action_id, action)
if action_id == hash("player2_up") then
self.move_distance = self.paddle_speed
end
if action_id == hash("player2_down") then
self.move_distance = -self.paddle_speed
end
end
- Save your project at this stage by pressing CTRL-S or choosing 'File', 'Save All'.
- In the assets panel, in the 'main' folder, double click 'main.collection'. You should see the game canvas in the editor panel in the middle of the screen.
- In the outline panel, right click the player2 game object and add this script as a new component file.
- Save the changes by pressing CTRL-S or 'File', 'Save All'.
- Run the program by pressing F5 or choosing, 'Debug', 'Start / Attach' from the menu bar.
If everything is working you should be able to use the cursor keys to move player 2's paddle up and down. It will leave the screen if you go too far because we haven't stopped this happening yet in the script. Now we can get the ball rolling! Stage 5c >
If your code doesn't work and you can't move the paddle, consider the following potential issues:
- Have you copied the code exactly without any spelling mistakes? The compiler won't always generate a syntax error if you mis-spelt a command. E.g. go.get_postion is missing an 'i' but it would cause the ball not to move.
- Did you set the key bindings described in stage 4 for player 2 and name the actions the same as the strings in the lines if action_id == hash("player2_up")?
- Did you copy and paste the code from player1 and forget to change the hash from player1 to player2 and player2 in the on_input function?
- Did you add the script to the player2 game object as a new component file?
The script must be attached to the game object to work. - Are you still running a previous version of the game without reloading?
Check the windows you have open.