Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So basically what I have is a function called createBoxBoundary, which, when the player position is within certain limits, sets boundary variables to true. Pretty straightforward. However, when I call this function more than once in the main game loop, only the one most recently called works. Below is a sampling of my code

function createBoxBoundary(x,y,width,height){
    //right boundaries
    if(playerXPos + 40 == x && playerYPos + 80 >= y && playerYPos < y + height){
        boundaryRight = true;
    } else{boundaryRight = false;}

    //bottom boundaries
    if(playerYPos == y + height && playerXPos + 40 >= x && playerXPos <= x + width){
        boundaryTop = true;
    } else{boundaryTop = false;}

    //left boundaries
    if(playerXPos == x + width && playerYPos + 80 >= y && playerYPos <= y + height){
        boundaryLeft = true;
    } else{boundaryLeft = false;}

    //bottom boundaries
    if(playerYPos + 80 == y && playerXPos + 40 >= x && playerXPos < x + width){
        boundaryBottom = true;
    } else{boundaryBottom = false;}

I've also set up a fiddle with the full game code. And if anyone has advice on a better way to do collisions/boundaries in javascript, I'm open to that as well. Any help is appreciated!

share|improve this question

closed as too localized by Sean Middleditch, Byte56, Trevor Powell, bummzack, Tetrad Feb 18 '13 at 23:41

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

GDSE is not for debugging your code for you. Your debugger is for that. Step through the code and inspect what is going on. The Chrome debugger is very good; FireBug is good too. – Sean Middleditch Feb 17 '13 at 23:49
The FAQ allows for "game-specific programming issues" and unless I have misunderstood, this is one of them. This is not a "bug" of the kind that it throws an error. It's the kind of this doesn't work the way I intended, and I don't know why. – EpicPineapple Feb 17 '13 at 23:57
@TehEpicPineapple Your title suggests that you do have a bug. It implies that somewhere in your code is an error. Only the brave ( well no people with time on their hand ) will analyze your code. – Sidar Feb 18 '13 at 17:25
If you have to post your code and ask where the problem is, the problem is "too localized." This question is specific to your implementation, not the general technique. Answers to this aren't generally useful to anyone but you. – Sean Middleditch Feb 18 '13 at 18:08
up vote 0 down vote accepted

I made the collision variables reset on each animation loop and then only set to true if they are a collision. I also changed the section of the code you call the animation loop from, this should be done after the processing of the current loop. Also added in some fallback support for my browser whilst testing. Updated fiddle here:

share|improve this answer

You set everything to false on a failed check and it looks a lot like you share those variables between all players/boundaries. might make sense to store them in an extra object and set/test each object individually.

share|improve this answer
I do not. The variables are for the player only. I have separate ones for other AI/players/whatever – EpicPineapple Feb 18 '13 at 20:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.