1

I'm building a web browser game.
Using Jquery 1.4 as framework
But i need to delete the trees that are in the building area.
This is what happens now:
What happens now

I would like to delete the trees if they are in the image area.
Made some sample code for show: http://jsfiddle.net/coolking/evU9D/3/
I hope i've given enough info.

Image height: 73px image width: 107px

The code incorporating the accepted answer is here: http://jsfiddle.net/coolking/evU9D/22/

casperOne
  • 73,706
  • 19
  • 184
  • 253

1 Answers1

3

Quick aside: it's better practice to apply all of your event listeners in JavaScript via element.AddEventListener() than to add HTML attributes like onload="..." or onmousedown="...".

jQuery has collision detection APIs to determine exactly what you're asking. Here's a good reference: jQuery/JavaScript collision detection

When you detect a collision, use something along the lines of $(this).remove() to remove the tree from the DOM.

I love strategy games, so I hope your game turns out well! =D

Community
  • 1
  • 1
Eric
  • 230
  • 3
  • 9
  • I will give you a screenshot ;) xD http://dl.dropbox.com/u/8015538/example/screenie.png But the problem is i cant get it working! :/ The onmousedown event was fast ;) in my real code it isnt ;) – Jeffrey Barneveld Jan 02 '12 at 21:41
  • 1
    then I'm not sure what you're having trouble with. be specific and ill do my best to help. – Eric Jan 02 '12 at 21:44
  • The collision if i do like tree1 <= obj ? obj ? tree1 it would take alot of time to do that again for my over 100's of trees you can see a demo at http://jsfiddle.net/coolking/evU9D/3/ – Jeffrey Barneveld Jan 02 '12 at 21:48
  • @Jeffrey, how many trees do you have here? There are only five in your fiddle, but your comment suggests you might be working with several hundreds. Did you try Eric's suggestion? – Frédéric Hamidi Jan 02 '12 at 22:13
  • I'm heading off to the gym and will take a look when i get back. – Eric Jan 02 '12 at 22:22
  • 1
    Looks like you're on your way – Eric Jan 03 '12 at 00:44