Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //lastest version 11/15/2020
- ////////This class takes in key and makes it blink and glow. The glow is a duplicate of the sprite placed behind and slightly larger
- ///called like this: var blinker = new Blinker(this,100,100,"booton","0xff00ff",2,500);
- class Blinker extends Phaser.GameObjects.Sprite{
- constructor(scene, x, y, key, color,invScale,timeDelay){
- super(scene, x, y, key);
- this.scene = scene; //the scene this game object will be added to
- this.scene.events.on('update', (time, delta) => { this.update(time, delta)} );// this line forces the parent scene to call the update inside this class
- this.x = x; //x
- this.y = y; //y
- this.key = key; //image key
- this.timeDelay = timeDelay //controls blink timing
- this.invScale = invScale; //this adjusts the size of the sprite while blinking-not sure why I did it inverted
- this.color = color;//this is for color
- this.direction = false; //controls if the blink expands or contracts
- //this.scene.physics.world.enable(this); //adds the objects to the physics world
- this.scene.add.existing(this);//adds the objects to the scene
- this.create();
- }
- create(){
- this.timedEvent = this.scene.time.addEvent({ delay: this.timeDelay, loop:true,callbackScope: this, callback: this.switch});//main loop to control the blink timing
- this.setTint(this.color).setAlpha(1);//colors the sprite
- this.backImage = this.scene.add.image(this.x,this.y, this.key).setTint(this.color).setAlpha(0.4);//duplicate image glows behind the sprite
- }
- update(){
- if(this.direction){ //contracting blink
- var scaler = this.timedEvent.getElapsed()/(this.timeDelay*this.invScale);
- this.setScale(scaler);
- this.backImage.setScale(scaler*1.5);
- }
- else{ //or expanding blink
- var scaler = this.timedEvent.getElapsed()/(this.timeDelay*this.invScale);
- var invsScaler = this.timeDelay/(this.timeDelay*this.invScale)-scaler;
- this.setScale(invsScaler);
- this.backImage.setScale(invsScaler*1.5)
- }
- }
- switch(){this.direction = !this.direction}//timer calls this function to change from grow to shrink
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement