1

Possible Duplicate:
jQuery/Javascript collision detection

I am learning how to code in JavaScript, and im using the jQuery library. I have a player that moves around the screen with the WASD keys. I want him to stop when he comes to the boundaries of the game. I am using two divs, one is called playground (the area where the game takes place) and another called player. I have them set up like this:

<div id="playground">
    <div id="player"></div>
</div>

The player moves great but I want him to stop when he gets to the edge of the playground. Please explain your answer in depth because I'm new to JavaScript programming. Any help would be much appreciated.

3
  • Did you first set the boundaries to the id=playground. Commented Sep 22, 2012 at 7:06
  • What do you mean, the playground is 400px by 400px, but i dont know how to stop the player when he comes into contact of the edge Commented Sep 22, 2012 at 7:11
  • stackoverflow.com/questions/4230029/… Commented Sep 22, 2012 at 9:02

1 Answer 1

0

So you need to use something like this:

 if (this.__move.left) {
      if (!this.isPlaying("walk_left"))
        this.stop().animate("walk_left", 10);
    }

here is a really good example and a demo. Try it http://craftyjs.com/demos/tutorial/tutorial.html

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks man, ill give this a try, bit confusing atm but im sure ill work it out :) How would i implement this into my code? Sorry for being a pain :0
no worries .... when you find this is right please vote the answer... all the best
Cheers, I've already voted the answer, Thanks for the help, just wondering how i put this into the code, Where do i put my variables, Sorry to be a annoyance, Thanks in advance :)
Im Sorry but i cannot work this out, the website you linked is using a library called craft which i don't want to use, and it is to stop animation, is there any other way you know to stop the player on the boundary?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.