Jump to content

how can I add physics to Phaser.GameObject.Sprite ?


qvintusgud
 Share

Recommended Posts

Hi, 

I wonder how I can add physics to my extended  class, 

Tried to change Phaser.GameObject.Sprite to Phaser.Physics.Matter.Sprite but I don't make it to work. 

I have this code 

let Evil = new Phaser.Class({

  Extends: Phaser.GameObjects.Sprite,

  initialize:

  function Evil (scene)
  {
    Phaser.GameObjects.Sprite.call(this, scene, 400 ,400, 'ship');
    this.speed = Phaser.Math.GetSpeed(500, 1);
  },
  fire: function(x, y)
  {
    this.setPosition(x, y - 50);
    this.setActive(true);
    this.setVisible(true);
  },
  update: function(time, delta)
  {
    this.y += 10;

  }

});

evils = this.add.group({
  classType: Evil,
  maxSize: 100,
  runChildUpdate: true
});

 

But I would like to have physics and tired to change it, but this dosen't work: 

 

let Evil = new Phaser.Class({

  Extends: Phaser.Physics.Matter.Sprite,

  initialize:

    function Evil (world)
    {
      Phaser.Physics.Matter.Sprite.call(this, world, 400 ,400, 'ship');
      this.speed = Phaser.Math.GetSpeed(500, 1);
    },
  fire: function(x, y)
  {
    this.setPosition(x, y - 50);
    this.setVelocityY(20);
    this.setActive(true);
    this.setVisible(true);
  },
  update: function(time, delta)
  {
  
  }

});

evils = this.add.group({
  classType: Evil,
  maxSize: 100,
  runChildUpdate: true
});

 

How can I make this to work? 

 

best regards, 

Link to comment
Share on other sites

Oki, but I make more then one, but now I found an other problem. 

I'm trying to count clicks on the spites. But my counter sometimes give me more then one click. 

Can't find the problem. 

So now my code looks like this

 

 

let info;
let bg;
let timedEvent;
let diamonds;
let testClicks = 0;


class GameScene extends Phaser.Scene {
  constructor(){
    super({
      key: 'GameScene'
    });
  }

  preload () {

    bg = this.add.image(400, 300, 'sky');
    bg.setInteractive();

    this.load.image('diamond', 'assets/diamant.png');

  }

  create () {

    let Diamond = new  Phaser.Class({

      Extends: Phaser.Physics.Arcade.Sprite,

      initialize:

        function Diamond (scene) {
          Phaser.Physics.Arcade.Sprite.call(this, scene, 100 ,10, 'diamond');
          // scene.physics.world.enable(this);
          // this.speed = Phaser.Math.GetSpeed(500, 1);

          console.log(this);
        },
      fire: function(x, y)
      {

        this.setPosition(x, y - 50);
        this.setInteractive();
        this.setVelocityY(300);

        this.setActive(true);
        this.setVisible(true);

        this.once('pointerdown', function(){
          this.setActive(false);
          this.setVisible(false);
          testClicks ++;
        });

      },
      update: function(){
        if ( this.y > 750  )
        {
          this.setActive(false);
          this.setVisible(false);

        }
      }

    });

    diamonds = this.physics.add.group({
      classType: Diamond,
      maxSize: 100,
      runChildUpdate: true
    });

    info = this.add.text(0, 300, '', { fill: '#00ff00' });

    timedEvent = this.time.addEvent({ delay: 1500, callback: this.onEvent, callbackScope: this, loop: true });

  }
  onEvent(){
    let diamond = diamonds.get();
    diamond.fire(100, 100);
  }
  update (time, delta){

    info.setText([
      'Test Clicks ' + testClicks,
      'Diamonds' + diamonds.getTotalUsed()
    ]);

  }
}

export default GameScene;

 

sometimes it register 3-4 clicks at once, can't understand how I should fix it.

First  I had a function that just made new spites but I after a while it start running slow, I think it was because it been to many, 

Then I made this class so I was able to remove them once they where out of canvas. 

Should I solve this with some other solution ? 

 

Link to comment
Share on other sites

Think I get it to work by using the code below, 

It two types om object, one you should click and one you should avoid.

Dont now if its wight way to do it, but I use the code below: 

 

let info;
let bg;
let timedDiamond;
let timedEvil;
let diamonds;
let evils;
let testClicks = 0;
let canvasHeight;
let canvasWidth;

class GameScene extends Phaser.Scene {
  constructor(){
    super({
      key: 'GameScene'
    });
  }

  preload () {


    // load image
    this.add.image(400, 300, 'sky');
    this.load.image('diamond', 'assets/diamant.png');
    this.load.image('evil', 'assets/evil.png');


    canvasHeight = this.sys.canvas.height;
    canvasWidth = this.sys.canvas.width;
  }

  create () {

    // groups for objects
    diamonds = this.physics.add.group({
      defaultKey: 'diamond',
      maxSize: 10,
     runChildUpdate: true
    });

    evils = this.physics.add.group({
      defaultKey: 'evil',
      maxSize: 10,
      runChildUpdate: true
    });

    this.input.on('gameobjectdown',this.onObjectClicked);
    info = this.add.text(0, 300, '', { fill: '#00ff00' });
    timedDiamond = this.time.addEvent({ delay: Phaser.Math.FloatBetween(850, 3000), callback: this.addDiamond, callbackScope: this, loop: true });
    timedEvil = this.time.addEvent({ delay: Phaser.Math.FloatBetween(1200, 4000), callback: this.addEvil, callbackScope: this, loop: true });

  }
  onObjectClicked(pointer,gameObject){

    if(gameObject.texture.key == 'diamond'){
      gameObject.setActive(false).setVisible(false);
      testClicks ++;
    }
    if(gameObject.texture.key == 'evil'){
      gameObject.setActive(false).setVisible(false);
      testClicks --;
    }

  }
  addDiamond(){

    let diamond = diamonds.get(Phaser.Math.Between(50, (canvasWidth - 50)), 0).setVelocityY(Phaser.Math.Between(300, 600)).setInteractive();
    this.activateObject(diamond);

  }
  addEvil() {
    let evil = evils.get(Phaser.Math.Between(50, (canvasWidth - 50)), 0).setVelocityY(Phaser.Math.Between(300, 600)).setInteractive();
    this.activateObject(evil);
  }
  activateObject(obj){
    obj.setActive(true).setVisible(true);

  }
  update (time, delta){
    Phaser.Actions.IncY(diamonds.getChildren(), 1);
    diamonds.children.iterate(function (diamond) {
      if (diamond.y > (canvasHeight + 100)) {

        diamonds.killAndHide(diamond);

      }
    });

    info.setText([
      'Points: ' + testClicks,
      'Diamonds: ' + diamonds.getTotalUsed(),
      'Evils: ' + evils.getTotalUsed()
    ]);

  }
}

export default GameScene;

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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