Every Object has a position (X and Y). The Stylus also has an X and Y of where it is touching. So all we have to do is set the X and Y of any Object to the Stylus X and Y in the Step event.
Use the action 'Set Object Position' in the 'Step' event, with these arguments:
Stylus.X - ([Width] / 2)
Stylus.Y - ([Height] / 2)
Move Objects by D-Pad
If the user presses Right, we must increase the X position. If they press Left, we must decrease it. If they press Down, increase the Y. Up, decrease the Y. So we are going to need 4 events for each (Button Held events).
For the Right event add the action 'Set Object Position' with these arguments:
[X] + 1
[Y]
This means use the previous X position + 1 (so move right) and the same Y position. We don't want to change the Y.
Collecting Objects
Once again, let us think of this in a programmatical way. The user must be able to move the Object with the D-Pad (or Stylus). We already have the actions to do that above. If the Object collides with another (collidable) Object, this object must be destroyed. The score should also increase.
Start with the .DSGM project to use the D-Pad. Add a new Object and call it 'Pickup'. Give it the sprite 'Ball_Sprite' so that it is visible. Add an event to this 'Pickup': Collision > Ball.
In this event, we need 2 actions: To delete itself and make the score bigger. So add 'Delete Object'. Add 'Set Score' (from the 'Score' tab) and put in the box: score + 1
Now go into Room_1 and plot some more Pickup objects. It works!
Things to remember:
- Think (on a low level) what you want to do. If you want to make the object move by the stylus or by the D-Pad, you are changing its position. So, the action, 'Set Object Position' is something to look at.
- X is the horizontal direction and Y is the vertical direction, beginning at 0,0 which represents the top left of the screen
- To increase the value of a variable (such as Score or an X Position), set it to itself plus 1
- When Events fire, the Actions within it happen.
