Advertisement
tempneff

Blinking Sprite

Nov 16th, 2020
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. //lastest version 11/15/2020
  2. ////////This class takes in key and makes it blink and glow. The glow is a duplicate of the sprite placed behind and slightly larger
  3. ///called like this: var blinker = new Blinker(this,100,100,"booton","0xff00ff",2,500);
  4. class Blinker extends Phaser.GameObjects.Sprite{
  5. constructor(scene, x, y, key, color,invScale,timeDelay){
  6. super(scene, x, y, key);
  7. this.scene = scene; //the scene this game object will be added to
  8. this.scene.events.on('update', (time, delta) => { this.update(time, delta)} );// this line forces the parent scene to call the update inside this class
  9. this.x = x; //x
  10. this.y = y; //y
  11. this.key = key; //image key
  12. this.timeDelay = timeDelay //controls blink timing
  13. this.invScale = invScale; //this adjusts the size of the sprite while blinking-not sure why I did it inverted
  14. this.color = color;//this is for color
  15. this.direction = false; //controls if the blink expands or contracts
  16. //this.scene.physics.world.enable(this); //adds the objects to the physics world
  17. this.scene.add.existing(this);//adds the objects to the scene
  18. this.create();
  19. }
  20.  
  21. create(){
  22. this.timedEvent = this.scene.time.addEvent({ delay: this.timeDelay, loop:true,callbackScope: this, callback: this.switch});//main loop to control the blink timing
  23. this.setTint(this.color).setAlpha(1);//colors the sprite
  24. this.backImage = this.scene.add.image(this.x,this.y, this.key).setTint(this.color).setAlpha(0.4);//duplicate image glows behind the sprite
  25. }
  26.  
  27. update(){
  28. if(this.direction){ //contracting blink
  29. var scaler = this.timedEvent.getElapsed()/(this.timeDelay*this.invScale);
  30. this.setScale(scaler);
  31. this.backImage.setScale(scaler*1.5);
  32. }
  33. else{ //or expanding blink
  34. var scaler = this.timedEvent.getElapsed()/(this.timeDelay*this.invScale);
  35. var invsScaler = this.timeDelay/(this.timeDelay*this.invScale)-scaler;
  36. this.setScale(invsScaler);
  37. this.backImage.setScale(invsScaler*1.5)
  38.  
  39. }
  40.  
  41.  
  42. }
  43. switch(){this.direction = !this.direction}//timer calls this function to change from grow to shrink
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement