sromeroi Posted February 11, 2017 Share Posted February 11, 2017 Dear All, I'm creating a small / simple game that uses the accelerometer (or cursor keys in the browser), for an online course. It's almost finished but for the collisions: I can't get them to work. The "game" is a sample about using the accelerometer to move a ship that avoids colliding with asteroids. Code and screenshot here: Link to MOOCsteroids code in github About the problem: I created a sprite (named "ship") and a asteroids group (named "group_asteroids") and in the asteroids update function I'm doing: // From file js/asteroids.js function playerCollided( ship, asteroid ) { console.log("Crashed!"); } function update(app) { for( i=0; i<num_asteroids; i++ ) { asteroids[i].angle += rotation_angle[i]; } game.physics.arcade.overlap( this.ship.getShip(), this.group_asteroids, playerCollided(), null, this); } What happens next is that I get thousands of "Crashed!" in the console, not when the ship collides with one of the asteroids but continously. I've also tested avoiding groups and using overlap directly between the ship and every asteroid (individually) with the same results. Any idea of what I'm doing wrong? Thanks! Link to comment Share on other sites More sharing options...
Arcanorum Posted February 13, 2017 Share Posted February 13, 2017 The problem is that you are actually running the playerCollided function when you give it as a parameter to overlap. You don't need the () on the end to pass a reference to the function, just the function name. This is how it should look: game.physics.arcade.overlap( this.ship.getShip(), this.group_asteroids, playerCollided, null, this); Also, if getShip is intended to be a getter, then you should avoid that. Getters and setters are not commonly used in JavaScript. Just use the ship object directly. Link to comment Share on other sites More sharing options...
samme Posted February 13, 2017 Share Posted February 13, 2017 You may prefer game.physics.arcade.collide for this. Link to comment Share on other sites More sharing options...
sromeroi Posted February 14, 2017 Author Share Posted February 14, 2017 On 2/13/2017 at 10:45 AM, Arcanorum said: The problem is that you are actually running the playerCollided function when you give it as a parameter to overlap. You don't need the () on the end to pass a reference to the function, just the function name. Ohhhhh, yes, it was a typo, and I was looking everywhere but in that place xD I was invoking the function and passing "undefined" (as the function does not return anything) as callback instead of the function object... Thanks for pointing it out! Link to comment Share on other sites More sharing options...
Recommended Posts