Making a Moving Tile

Note: this tutorial is out of date. You can use the concepts from this tutorial, combined with this tutorial http://gamefroot.com/knowledgebase/move-it-physics-movement-and-the-constantly-loop/ to recreate this.

 

moving

An easy thing you can do to make your games more tricky to navigate is to add moving tiles. Static platforms can become boring and predictable, so why not keep the player on their toes? Here’s how we can make a moving tile.

First, make sure you’ve got your level drawn out with a nice big gap between some terrain. Remember that terrain tiles need to be drawn onto the tilemap layer.

moving-tile-1

Once you’ve got your terrain drawn, place your player character next to the gap. Make sure the player character is placed on the layer above the tilemap.

moving-tile-2

Then attach the Platform Player Controls script to the player character – if you don’t do this, you won’t be able to play!

Now for the moving tile. Grab an object from the Game Objects tab in the sidebar, and place it in the gap.

moving-tile-3

This object needs a script to tell it to move, so let’s make one. Click Script Edit Mode to create a new script (if you’re new to scripts, check this out). Here’s the script we need to make:

moving-tile-4

 

(If you want a close look, click on the image to zoom in.) This script is giving these instructions to the object:

  • When created become immovable (so the player can’t knock me around), and give me a velocity x (horizontal speed) of 50.
  • Constantly, if my x position is further than 1000, set my velocity x to -50 (so I will start travelling to the left if I’ve gone too far to the right).
    • or else, if my x position is before 530, set my velocity x to 50 (so I will start travelling to the right if I’ve gone too far to the left)

And this makes the object move back and forth! To create this, we’ll need to start with 2 Event blocks called “When created” and “Constantly”, which we can find under Game Mechanics, Events.

moving-tile-5

Next we’ll need “Set velocity x” and “Set reacts to gravity” from the Arcade Physics tab. Drag these blocks into the “When created” block. Then click on “reacts to gravity” and change it to “immovable”.

moving-tile-6

Now we need to set up the logic for the movement, and we’ll do this with an “if, do” block from the Logic tab. Drag it into the “Constantly” block.

moving-tile-7

 

To turn the “if do” block into an “if do, else do” block, you’ll need to click on the star button in it’s top-left corner, and drag the “else if” block across. You can click on the star again to make that window go away.

moving-tile-8

Next we need the “less than” and “greater than” blocks from the Logic tab. Drag them into the “if” slots, and change the top = to , and the bottom = to .

moving-tile-9

Now grab two “x position of myself” blocks from the Instances tab, and drag them in the “less than” and “greater than” slots.

moving-tile-10

To put numbers into the empty “less than” and “greater than” slots, go to the Math tab and drag out the “0” number block.

moving-tile-11

The last two blocks are “set velocity x” from the Arcade Physics tab. Drag these into the “if do” and “else if do” openings. Change the numbers to -50 for the top one, and 50 for the bottom one.

moving-tile-12

Lastly, we need to figure out what the “greater than” and “less than” numbers need to be. We can determine these numbers by going back to our level and finding the positions on either side of the gap – but don’t forget to Save this script first (click the Save button in the top-right corner).

moving-tile-13

Once you’ve saved the script, click Close to return to your level. By hovering your mouse cursor over any part of your level, you can see what those x and y positions are inside your level – these numbers are displayed above the layers panel to the right (x position, y position). I can see that my gap starts at 530 x, and then by hovering over the other side of the gap, I can see that it ends at 1000 x.

Now that we know these numbers, we can go and add them to our script. Open the Scripts tab in the sidebar, and click Edit on your moving tile script.

moving-tile-14

 

Once your script has opened, change these numbers from 0 to the positions you determined just before. The top number should be the position of right side of the gap, and the bottom number should be the position of the left side of the gap. Once you’ve changed those numbers, save the script and close the script editor.

moving-tile-15

 

Now attach the script to the moving tile object by clicking on the script then clicking on the object.

moving-tile-16

All done! Now you can preview your game. Click the image below to see the game run:

moving-tile-17

8 Replies to “Making a Moving Tile”

    1. okkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

Leave a Reply