Incoming | Scripting the player target.
When the player clicks the left mouse button the target should appear at that position on the screen and gradually fade out before being removed from the screen.
There are a few ways we could approach this. We could use a factory and spawn the player target when a key is pressed using a target spawner script. However, because we can only ever have one target on the screen at a time, an easier approach is simply to move the target off the viewable area of the screen when the fade animation ends and move it to a new position when the left mouse button is clicked.
Firstly we need to set a key binding for the mouse.
- In the assets panel, in the 'input' folder and double click the 'game.input_binding' file.
- Set the 'Mouse Triggers' so that input, 'Button left' has an action of, 'target'.
Now we need to write a script to handle the input.
- Create a new script called, 'player.script'. Try to do this without help, but there is a guide below if you need it. Step-by-step guide
- In the assets panel, right click, 'main.collection' and select, 'New', 'Script'.
- Name the script 'player' and click 'Create Script'.
- In the init function change the code to aquire the input focus.
function init(self)
msg.post(".", "acquire_input_focus")
end
- Above the init function, write a new user-defined function that will be called when the target has faded out. This will reset its position.
local function complete()
pos = vmath.vector3(-100, -100, 0)
go.set_position(pos)
end
You can make your own methods for game objects by declaring local functions in addition to the built-in ones.
pos is a vector3 position to a place that is off-screen so the player cannot see it.
- Change the on_input function to respond to the mouse being clicked.
function on_input(self, action_id, action)
if action_id == hash("target") and action.pressed then
go.set("#sprite", "tint.w", 1)
pos = vmath.vector3(action.x, action.y, 0)
go.set_position(pos)
go.animate("#sprite", "tint.w", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_INOUTSINE, 0.5,0, function() complete() end)
end
end
If the action_id is 'target' (that we set in the key bindings) and the action is that the mouse button has been pressed then...
The sprite's 'tint.w' property is set to 1. This means that it is fully visible. The 'tint.w' property can have a value beteen 0 and 1 that states how transparent it is.
The position is then set to match the position of the mouse. This is stored in action.x and action.y.
The animate command is then used to animate the sprite over half a second to fade out (go.EASING_INOUTSINE).
When the animation is complete, the following code is executed: function() complete() end. This essentially calls the 'complete' function we have just written. It's much neater to call a function rather than write all the code in this part of the command.
- Don't forget we now need to attach the script to the target game object. See if you can remember how to do this, and check with the guide below. Step-by-step guide
- In the assets panel in the 'main' folder, double click, 'main.collection'.
- In the outline panel, right click 'target' and select, 'Add Component File'.
- Select '/player.script' and click 'OK'.
- Save the changes by pressing CTRL-S or 'File', 'Save All'.
- Run your game at this point. It should now respond to the left mouse being clicked.
We can now finish the core mechanics of the game by adding the collision handling routine. Stage 6 >