Jump to content

Help me with my Dimmer class


neonwarge04
 Share

Recommended Posts

I was not permitted to use Phaser.js so I am stuck doing this on Pixi.js. Though I am using tween.js, I can't seem to make my class work. I can, if I start to dim in (opacity 1 to 0), but not dim out (opacity 0 to 1). I wonder what I got wrong. 

Here is my class:

 

function Dimmer(stage , screenSize , dimSpeed , color , maxOpacity){  this.mStage = stage;  this.mScreenSize = screenSize;  this.mDimSpeed = dimSpeed;  var mDimmerQuadrant = new PIXI.Graphics();  mDimmerQuadrant.lineStyle(0 , 0x000000 , 0);  mDimmerQuadrant.beginFill(color , 0);  mDimmerQuadrant.drawRect(0 , 0 , this.mScreenSize.width , this.mScreenSize.height);  this.mFadeOut = new TWEEN.Tween({alpha : mDimmerQuadrant.alpha})    .to({alpha : 1} , this.mDimSpeed)    .onStart(function()      {          this.alpha = 0;      })    .onUpdate(function()      {        mDimmerQuadrant.alpha = this.alpha;      });  this.mStage.addChild(mDimmerQuadrant);}Dimmer.prototype.fadeOut = function(){  this.mFadeOut.start();}Dimmer.prototype.fadeIn = function(){}

I believe I don't have to post other parts of my code. This is where it all happens.

I can have an opacity from 0 to 1. When I ran my code, the opacity is already set to 1. I can't even see my dimmer go into view. This is not the case otherwise. I guess I am missing something critical witw the Graphics actually works under the hood.

I appreciate your help!

Link to comment
Share on other sites

The alpha properties in your lineStyle and beginFill calls are set to zero, they should not be. The alpha property of the graphics display object is what you are trying to apply the tween to and that is a separate value to the ones used in drawing the vector shape. You should set the lineStyle an beginFill alpha values to 1.

Link to comment
Share on other sites

@alex_h

Thanks for your help! You are definitely right. 

here is my latest code for this one, a little rusty but works for me for now:

 

function Dimmer(stage , screenSize , dimSpeed , color , minOpacity , maxOpacity , enableFadeOut , delayBeforeFade , fadeOutDimSpeed){  this.mStage = stage;  this.mScreenSize = screenSize;    var mDimmerQuadrant = new PIXI.Graphics();  mDimmerQuadrant.lineStyle(0 , 0x000000 , 0);  mDimmerQuadrant.beginFill(color , 1);  mDimmerQuadrant.drawRect(0 , 0 , this.mScreenSize.width , this.mScreenSize.height);  mDimmerQuadrant.endFill();  mDimmerQuadrant.alpha = minOpacity;  this.mFade = new TWEEN.Tween({alpha : mDimmerQuadrant.alpha})    .to({alpha : maxOpacity} , dimSpeed)    .onUpdate(function()      {        mDimmerQuadrant.alpha = this.alpha;      })    .onComplete(function()      {        if(enableFadeOut === true)        {          new TWEEN.Tween({alpha : mDimmerQuadrant.alpha})            .to({alpha : minOpacity} , fadeOutDimSpeed)            .delay(delayBeforeFade)            .onUpdate(function()            {              mDimmerQuadrant.alpha = this.alpha;            }).start();        }      });  this.mStage.addChild(mDimmerQuadrant);}Dimmer.prototype.setOnComplete = function(callback){  this.mFade.onComplete(callback);}Dimmer.prototype.start = function(){  this.mFade.start();}

Thanks!

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...