Falling off of objects in a game


#1

Hi, I am making a platformer game. I am tying to mak my character fall off of a platform but I dont know how. Please help’

THANKS
Animator786

EDIT: If anyone knows the answer to this plz tell me the code. (my laptop doesnt let me view files on the forum.) sorry for the trouble


#2

Hey @Animator786,

I’ve added the code and an example image the last post you were a part of! Let me know if it helps with this. How do i make game boundaries?

You may need to add an update function that always pushed the character down on the character object, like this.

function update() {
  this.y += 5; // Speed of gravity.
}

#3

Hi, I implemented the code for “Game Boundaries” into my game and I tried to combine it with the “falling off of objects” code but it would not work. With the gravity code, the character just falls straight through the boundaries before I get a chance to move the character. What I am trying to do is have the game start with the character resting on the wall/platform. then, if i move the charcter over the edge of the wall/platform than the gravity code will kick in and allow for the character to fall. If you know how to do this please tell me.

Sorry if this question was too complicated,
regards,
Animator786


#4

Maybe try putting the game start way above a platform so the game has time to realize the platform is real


#5

I tried that but it still doesnt recognize the platform. I dont know, the code is probably wrong. Creators, if you are reading this, plz help.


#6

Hey @Animator786,

We’ll actually need to make a few adjustments.

First, we’ll need to add our floor object to the “walls” list.

var walls = [wall, wall2, floor1];

Then, we’ll need to change our update function a bit to use the same logic as the keyDown function.

function update() {
  var oldX = this.x; 
  var oldY = this.y; 

  this.y += 5; // Move our character the speed of gravity.

  // reset ourselves if we hit a wall.
  if (hitWalls(this)) {
      this.x = oldX; 
      this.y = oldY;
  }
}

Try these out!


#7

Hi, it keeps giving this error. I don’t know how to fix it.


#8

Oh! These were adjustments to the code from the last thread. Make sure that you add all of that code too!

// How much did we move last turn?
var moveSpeed = 5; 

var walls = [wall, wall2, floor1]; // List the names of ALL the walls in my project.

function keyDown(key) {
    // Save our old positon 
    var oldX = this.x; 
    var oldY = this.y; 
    
    // Try to move the character
    if (key === 'RIGHT') {
        this.x = this.x + moveSpeed; // Move 5 pixels to the right
    } else if (key === 'UP') {
        this.y = this.y - 5; // Move 5 pixels to the right
    } else if (key === 'LEFT') {
        this.x = this.x - moveSpeed; // Move 5 pixels to the right
    } else if (key === 'DOWN') {
        this.y = this.y + moveSpeed; // Move 5 pixels to the right
    }
    
    // If we hit a wall, move us back.
    if (hitWalls(this)) {
        this.x = oldX; 
        this.y = oldY;
    }
}

function update() {
  var oldX = this.x; 
  var oldY = this.y; 

  this.y += 5; // Move our character the speed of gravity.

  // reset ourselves if we hit a wall.
  if (hitWalls(this)) {
      this.x = oldX; 
      this.y = oldY;
  }
}

// Pass in an object to see if it hit the wall.
function hitWalls(objectToCheck) {
    var hitWall = false; 
    
    // Go through all of the walls I have listed. 
    for (var i=0; i<walls.length; i++) {
        var wallToCheck = walls[i]; 
        // If I hit the wall, let the person who called the function know.
        if (objectToCheck.hitTest(wallToCheck)) {
            hitWall = true; 
        }
    }
    return hitWall; 
} 

Also, make sure you name your floor “floor1”.


#9

Oh okay, I got confused. sorry. And yeah, I named my floor floor1.
Thanks