Jump to content

DistanceBetween() OOP


0penS0urce
 Share

Recommended Posts

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

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...