Collision detection through array
javascript, jquery
Solution
First of all, let'stalk about some code errors
- `$ball.t` should be probably `$ball.top`
- you do not need to have `$` as a prefix, for your code, it's simply a variable and you are calling `$ball` instead of `ball` witch results in assumption errors!
for those assumption errors here is what you are doing wrong:
- `$ball` is a dom element, not a jQuery element
- the same as `$bricks`
- `$ball` is an array
with those concluded from some `console.log()` let's try to fix the code:
the `$ball` should be called, as there's only one, by it's array element, as `$ball[0]` and because you have variables pointing to DOM elements and not jQuery elements, you need to wrap it in Jquery as:
if ( $($ball[0]).top === $($bricks[i]).offset().top ) { ...
a good idea not to get confused is only use `$` in jQuery elements, prefixing it in a variable, does not make them a jQuery Element.
And everytime you see that you have an error such as "element x has no method y" always assume that you're calling a method from a DOM element, and not a jQuery element.
Problem
Hi everyone I started to write a little game with ball and bricks and have some problem with collision detection. Here is my code http://jsbin.com/ibufux/9 . I know that detection works though array, but I can't figure how I can apply it to my code. Here is what i have tried: ``` bricksCollision: function() { for (var i = 0; i < $bricks.length; i++) { if ($ball.t == $bricks[i].offset().top) { $bricks[i].splice(i, 1); } } ``` Every bricks in game are generated by for loop and then goes to $bricks array. Every brick after generating receive top and left position and have position absolute. I have tried to check if $ball.t (it's properties of my ball object which detects ball top position) reach the bricks and than remove bricks. Thanks for any help. I'm only start to learn JS that's why my code is knotty.