1. Home
  2. Docs
  3. Game Factory Documentatio...
  4. Coding
  5. Gate: first try

Gate: first try

Let’s use our new skills to make a gate that opens when you bump into it. Place a gate between some terrain blocks like this:

Now, use the Logic tool on the gate and remove its built-in IF-THEN panel, because it will interfere with our example. We just want a plain gate!

Now let’s try to naively implement a script that opens the gate when you bump into it. Make a new block (or reuse the block you already had on the slime) and put it on the gate. Write this code on the block:

  export function onCollision() {
    // Open the gate by moving the the left a bit
    moveLeft(1);
  }
  

It says “if I collide with something, then move left”. Sounds right, doesn’t it? Well… almost. Look at what happens:

As you can see, the gate moves left with a speed of 1 meter/second only while the player is colliding with it. If you stop colliding with the gate, it stops.

This happens because the onCollision message gets delivered every frame only while the gate is colliding. After it stops colliding, it is no longer delivered, so your moveLeft call doesn’t execute, hence the gate stops moving.

How could we implement a gate that opens smoothly all the way, without needing the player to continually bump against it?

Well, for that, the gate needs to remember that it’s currently in the “opening” state. For that, we need memory!

How can we help?

Search this website Type then hit enter to search