0penS0urce Posted June 17, 2015 Share Posted June 17, 2015 The distanceBetween() function in Phaser doesn't seem to include support for Objects. Do I have to make a predefined group, or do something else? Thanks in Advance! Enemy = function (game, x, y) { Phaser.Sprite.call(this, game, x, y, 'enemy'); game.physics.enable(this, Phaser.Physics.ARCADE); var tween = game.add.tween(this).to({ x:400, y: 200}, 5000, Phaser.Easing.Linear.None).to({ x:400, y: 400}, 5000, Phaser.Easing.Linear.None).to({ x:0, y: 400}, 5000, Phaser.Easing.Linear.None).start(); };Enemy.prototype = Object.create(Phaser.Sprite.prototype);Enemy.prototype.constructor = Enemy;Enemy.prototype.update = function() {};Tower = function (game, x, y) { Phaser.Sprite.call(this, game, x, y, 'tower'); game.physics.enable(this, Phaser.Physics.ARCADE);};Tower.prototype = Object.create(Phaser.Sprite.prototype);Tower.prototype.constructor = Tower;Tower.prototype.update = function() { if(game.physics.arcade.distanceBetween(this, Enemy)<=100){ console.log("shoot"); }};var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });function preload() { game.load.image('enemy', 'assets/player.png'); game.load.image('tower', 'assets/player.png');}function create() { game.physics.startSystem(Phaser.Physics.ARCADE); var en = new Enemy(game, 800, 200); game.add.existing(en); var tow = new Tower(game, 500, 400); game.add.existing(tow);}function update(){} Link to comment Share on other sites More sharing options...
rich Posted June 17, 2015 Share Posted June 17, 2015 distanceBetween will work happily with objects as long as they've got public x and y properties. Your code looks wrong though - you're not comparing instances, you're comparing the actual base objects. "Enemy" is wrong, it should be a reference to an Enemy object, not the prototype itself. Link to comment Share on other sites More sharing options...
0penS0urce Posted June 17, 2015 Author Share Posted June 17, 2015 I've done distanceBetween(this, Enemy.prototype), but it gives a Phaser.js error on line 831, saying that x is undefined. I am guessing this is because the variables are not public, so how do I make them public? Link to comment Share on other sites More sharing options...
rich Posted June 17, 2015 Share Posted June 17, 2015 That's still not an instance of the object, it's the object prototype itself. Here:Tower = function (game, x, y) { Phaser.Sprite.call(this, game, x, y, 'tower'); game.physics.enable(this, Phaser.Physics.ARCADE);}; Tower.prototype = Object.create(Phaser.Sprite.prototype);Tower.prototype.constructor = Tower; Tower.prototype.checkDistance = function(enemy) { if (this.game.physics.arcade.distanceBetween(this, enemy) <= 100) { console.log("shoot"); }};var tower = new Tower(...);var bob = new Enemy(...);tower.checkDistance(bob); Link to comment Share on other sites More sharing options...
0penS0urce Posted June 17, 2015 Author Share Posted June 17, 2015 Thanks Rich! Works perfectly! Link to comment Share on other sites More sharing options...
Recommended Posts