Sunday, 15 February 2015

HTML5/Javascript game dev - If I have more than one boundary, only the most recent one works -



HTML5/Javascript game dev - If I have more than one boundary, only the most recent one works -

so have function called createboxboundary, which, when player position within limits, sets boundary variables true. pretty straightforward. however, when phone call function more 1 time in main game loop, 1 called works. below sampling of code

//it should noted player deminsions 40x80 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 set fiddle total game code. , if has advice on improve way collisions/boundaries in javascript, i'm open well. help appreciated!

the problem not checking if boundaryright set true before checking value sec boundary.

something should work:

boundaryright = ( boundaryright || ( playerxpos + 40 == x && playerypos + 80 >= y && playerypos < y + height );

i have done other tweaks in jsfiddle add together more flexibility of defining boundaries , other things. adds ability move in more 1 direction.

http://jsfiddle.net/petersendidit/qdcmg/7/

javascript html5 collision-detection

No comments:

Post a Comment