NateWr Posted September 1, 2014 Share Posted September 1, 2014 I've been playing a bit with Phaser for the first time this weekend and really enjoying it. I've run into a really basic issue that is no doubt caused by a completely dumb mistake I've made, but I can't seem to figure it out. I have set up a small red ball. When the player controlled ball (blue) gets near the red ball, it will chase the player. I've set up a little grey block as well, which the player and the red ball should have collision with. This works for the player, but the red ball will pass right through it. I've been working off of the Tanks example. Below I've copied just the code I think is relevant, but I've uploaded the whole tiny thing for you to look at. WASD movement. Shift to move quickly./** * The red ball */var EnemyRed = function( index, game, player ) { this.game = game; this.player = player; this.enemy = game.add.sprite( 96, 96, 'enemy' ); this.enemy.anchor.setTo( 0.5, 0.5 ); game.physics.enable( this.enemy, Phaser.Physics.ARCADE ); this.enemy.body.collideWorldBounds = true; this.enemy.body.bounce.setTo( 1, 1 ); this.enemy.body.drag.set( 0.8 ); this.enemy.body.maxVelocity.setTo( 400, 400 ); this.order = 'bounce';};/** * The red ball following the player */EnemyRed.prototype.update = function() { if ( this.game.physics.arcade.distanceBetween( this.enemy, this.player ) < 100 && this.order !== 'attack' ) { this.attack( this.player ); } ... else if ( this.order === 'attack' ) { this.game.physics.arcade.velocityFromRotation( this.game.physics.arcade.angleBetween( this.enemy.body, this.player.body ), 100, this.enemy.body.velocity); }}/** * The red ball is created and added to the enemies group */function create() {... enemies = []; enemies.push( new EnemyRed( 'patroller', game, player ) );... }/** * Collision is registered and the red ball's update routine is run */function update() { game.physics.arcade.collide( player, buildings ); game.physics.arcade.collide( enemies, buildings ); game.physics.arcade.collide( player, enemies ); ... for ( var i = 0; i < enemies.length; i++ ) { enemies[i].update(); }}Anyone spot the issue? I've browsed through the examples but not found anything to illuminate this kind of collision (all involve gravity, not top-down movement). Many thanks for any help you can provide. Link to comment Share on other sites More sharing options...
lewster32 Posted September 1, 2014 Share Posted September 1, 2014 The EnemyRed instance doesn't have a body on it, since it's not extending a Sprite, so your enemies array is not going to have the collision detection work properly (collide and overlap both check for a group or array of objects with body properties). You may be able to fix this simply by doing the following, which will ensure the enemies array is filled with Sprites and not your custom objects:enemies.push( new EnemyRed( 'patroller', game, player ).enemy );I'd strongly urge you to look at the examples for extending Sprites though as that's a better way to do this sorta stuff. Doing it that way means you get the update function for free, rather than having to call it manually as you're doing. Link to comment Share on other sites More sharing options...
NateWr Posted September 1, 2014 Author Share Posted September 1, 2014 That did the trick, thanks! lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts