Knowledgebase / Get over here! Setting an object’s position
We’re going to make objects move to a new random position. This tutorial follows on from Crash! Collision detection.
Step 1: Replace Destroy with Set position
Edit Newton’s script by right-clicking on him, and selecting Edit Script. Instead of destroying the objects he touches, we’ll change their position to a new random position. Drag the “Destroy instance toucher” block into the trash.
Open the Transform tab and grab “Set x position of myself”. Drag this block into the place that the destroy block was in.
Step 2: Set x position of the toucher
Instead of setting the x position of myself (Newton), we want to set the position of the toucher. Go to the Variables tab, and drag the “instance toucher” block into where the “myself” block is. You can then drag the “myself” block into the trash.
An object’s x position is its horizontal position in the game. An x position of 0 will mean the object is at the very start of the game on the left.
Step 3: Random numbers
We’re setting the x position to 0, but we want to move the toucher to a random position. Open the Operators tab, and grab the “Random integer from 1 to 100” block. Integer is another name for a whole number (no fractions or decimals).
The game resolution (size of the game screen) is 768 pixels wide, so we want the toucher to set its x position to anywhere between 1 and 750 (we’ve chosen 750 so that a little bit of the apple will still appear in the game if it goes all the way over to y=500). Click on 100 and change it to 750.
Now click Play to test it out. You’ll notice the apples only change their position horizontally, so we need to get them changing their y position as well.
Step 4: Duplicate for the y position
Right-click on the “Set x position” block, and select Duplicate. Then drag the duplicated block underneath the original one, and click on the x to change it to y.
An object’s y position is its vertical position in the game. A y position of 0 will mean the object is at the very top of the game, and as the y position number gets larger, the object is placed further down. (A negative y position will send the object up higher)
The game resolution is 512 pixels high, so we want the toucher to set its y position to anywhere between 1 and 500 (we’ve chosen 500 so that a little bit of the apple will still appear in the game if it goes all the way over to y=500). Click on 750 and change it to 500.
Now play your game. You’ll notice the apples start to move once they’ve been bumped into by Newton, so we’ll fix that up by giving the apples their own script which makes them immovable.
Step 5: Stopping the apples from being pushed
When an object collides with another object it gets pushed away. We’ll give the apples a script to prevent that. Right-click on one of the apples, and select Add Script.
From the Events tab, drag out the “When level starts” block. Then from the Physics tab, drag out the “Set immovable to true” block (it’s near the bottom), and drag it into the “When level starts” block. This will stop other objects from pushing the apple around.
Click Level Editor to go back to the level. It will ask you to save your script, so give it a name like Apple. We need the rest of the apples to have this script on them as well, so open the Scripts sidebar on the left, and click on the Apple script to select it. Then click on the other apples, and they will get the Apple script attached to them.
Now play your game!
Don’t forget to save your game.