How do i make game boundaries?


#1

Hi, i am trying to add a feature to my game. What im trying to make is, when my object touches a wall the wall doesnt allow the object to move past it. The wall acts as a wall.

How do I do this?
Help


#2

anyone know?


#3

NO ONE ANSWERED THIS ONE EITHER!!! GOSH I HATE THIS FORUM. I don’t know why these people have to sit around for days just to get an answer? whatever


#5

@Monaco1,

So I thought about this a bit. Here’s a project that has a moving character which hits walls. There is only code on the character. The one issue is that you need to name all of your walls individually, but for project borders You’ll only need 4 walls!

wallsThatPushBack-Dec7-2018-1.50PM.html (788.0 KB)


#6

My computer wont let me view your file. Can you put the code in the comments plz.


#7

and who is [alacazzableobeardinc)?


#8

Try clicking on it and then drag the download into a new tab. That should solve your problem.


#9

@Animator786, here’s the code I’ve added to the character in the example above. (It’s kind of nasty). The names of my walls are “wall” and “wall2”.

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

var walls = [wall, wall2]; // 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;
    }
}

// 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; 
} 

Here’s an image of the project, the ball is the character.


Falling off of objects in a game
Falling off of objects in a game
#11

Thank you so much. This REALLY helped. its exaclty what I was looking for.


#12

Happy to help! Sorry it took a few days to get back to you. Let me know if I can give you any additional support!


#13

Hello

Can you please send me the project I am trying to understand how to program!
Thanks


#14

Sure, heres a project that uses game boundaries.

New Project-Jan8-2019-10.54AM.wick (8.7 KB)

Hope this helps