Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Timer Issue


luiskcs89
 Share

Recommended Posts

I am using the Basic Phaser Template and I have something like the following:

 

BasicGame.Game = function (game) {
 
    this.shadow;
   this.doflash;
 
 
};
 
BasicGame.Game.prototype = {
 
    create: function () {
 
       this.shadow = this.game.add.graphics(0, 0);
       this.doflash = true;
 
    },
 
    update: function () {
       if(doflash) {
          doflash = true;
          this.game.time.events.add(Phaser.Timer.SECOND * 1, this.flash, this);
      }
    },
 
    flash: function () {
        this.shadow.clear();
           this.shadow.beginFill(0xFFFFFF, this.shadowAlpha);
 
    }
 
};
 
And I get the following error "Uncaught TypeError: Cannot call method 'clear' of undefined".
Any ideas?
 
Thank you
 
Link to comment
Share on other sites

Graphics is completely different than canvas drawing. 

 

You'll want to do something like the following:

BasicGame.Game = function (game) {   this.shadow;  this.doflash;  }; BasicGame.Game.prototype = {     create: function () {        this.shadow = this.game.add.bitmapData(this.game.width, this.game.height);       this.background = this.game.add.sprite(0,0, this.shadow);       this.doflash = true;     },     update: function () {       if(doflash) {          doflash = true;          this.game.time.events.add(Phaser.Timer.SECOND * 1, this.flash, this);      }    },     flash: function () {        this.shadow.clear();        this.shadow.ctx.rect(0, 0, this.game.width, this.game.height);        this.shadow.ctx.fillStyle = 'white';        this.shadow.ctx.fill();        this.shadow.render();        this.shadow.refreshBuffer();    } };

What this is doing is:

It's using a bitmapData (this.shadow) for the texture of the newly created background sprite. Each time flash() is called, it clear's the bitmapData's context, draws a large white square, renders it, and refreshes the buffer so the sprite will use the new texture.

Link to comment
Share on other sites

I can't see why shadow would be undefined in the flash call from the code you posted.

What line number is the error coming from? Does it match the one you have in the flash function?

Have you put a break point in the flash function to see what 'this' actually is at that point?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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