-1
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 17, 2013 at 23:49
  • \$\begingroup\$ 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. \$\endgroup\$
    – starscape
    Commented Feb 17, 2013 at 23:57
  • \$\begingroup\$ @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. \$\endgroup\$
    – Sidar
    Commented Feb 18, 2013 at 17:25
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 18, 2013 at 18:08

2 Answers 2

0
\$\begingroup\$

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:

http://jsfiddle.net/totovste/QdCMG/8/

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ I do not. The variables are for the player only. I have separate ones for other AI/players/whatever \$\endgroup\$
    – starscape
    Commented Feb 18, 2013 at 20:55

Not the answer you're looking for? Browse other questions tagged .