Jump to content

trying to count clicks, but it count wrong


qvintusgud
 Share

Recommended Posts

Hi, 

 

Im trying to make an infinite loop where I count click on the sprites, 
But something I get 2-4 counts extra on every click, 

Tried a couple solutions but I dont get it to work. 

Anyone have a clue what I'm doing wrong? 

I use the code below: 

let info;
let bg;
let canvasHeight;
let canvasWidth;


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 ,0, 'ship');
          // scene.physics.world.enable(this);
          this.speed = Phaser.Math.GetSpeed(500, 1);

          console.log(this);
        },
      fire: function(x, y)
      {
        console.log('Spawn new diamond');
        this.setPosition(x, y - 50);
        this.setInteractive();
        this.setVelocityY(300);
        
        this.setActive(true);
        this.setVisible(true);
      
      },
      update: function(){
        if (this.y < -50 || this.y > (canvasHeight + 50)  )
        {
          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' });

    function testSomething() {}

    (function testloop() {
      let rand = Math.round(Math.random() * (2000 - 500)) + 500;
      setTimeout(function() {
        testSomething();
        testloop();
        let diamond = diamonds.get();
        diamond.fire(100, 100);
        diamond.on('pointerdown', function(pointer){
          testClicks = testClicks + 1;
          this.setActive(false);
          this.setVisible(false);
        });
       
      }, rand);
    }());
    
  }

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

  }
}

export default GameScene;

 

best regards

Link to comment
Share on other sites

HI Rich, 

 

Its just so don't get exactly interval between the sprites. 

The part I having trouble with is:

diamond.on('pointerdown', function(pointer){
          testClicks = testClicks + 1;
          this.setActive(false);
          this.setVisible(false);
        });

somethings when I click it count more then one. 

I'm trying to figure out if I'm binding clicks to the group right or something else I have made wrong

 

Link to comment
Share on other sites

Thanks a lot for your respond Rich, 

I fixed the setTimeout och now using timedevent. 

Thats right I would like them to just vanish, but I will make some sort of animation on that when everything is right first. 

Now it seems to work right, but I dont know if its right way to do it, 

 

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;

best regards

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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