import { IImageConstructor } from '../interfaces/image.interface' ; export class Coin extends Phaser . GameObjects . Image { private centerOfScreen : number ; private changePositionTimer : Phaser . Time . TimerEvent ; private lastPosition : string ; constructor ( aParams ) { super ( aParams . scene , aParams . x , aParams . y , aParams . texture ) ; this . initVariables ( ) ; this . initImage ( ) ; this . initEvents ( ) ; this . scene . add . existing ( this ) ; } private initVariables ( ) { this . centerOfScreen = this . scene . sys . canvas . width / 2 ; this . changePositionTimer = null ; this . setFieldSide ( ) ; } private initImage ( ) { this . setOrigin ( 0.5 , 0.5 ) ; } private initEvents ( ) { this . changePositionTimer = this . scene . time . addEvent ( { delay : 2000 , callback : this . changePosition , callbackScope : this , loop : true } ) ; } update ( ) { } public changePosition ( ) { this . setNewPosition ( ) ; this . setFieldSide ( ) ; this . changePositionTimer . reset ( { delay : 2000 , callback : this . changePosition , callbackScope : this , loop : true } ) ; } private setNewPosition ( ) { if ( this . lastPosition == 'right' ) { this . x = Phaser . Math . RND . integerInRange ( 100 , this . centerOfScreen ) ; } else { this . x = Phaser . Math . RND . integerInRange ( 385 , 700 ) ; } this . y = Phaser . Math . RND . integerInRange ( 100 , 500 ) ; } private setFieldSide ( ) { if ( this . x <= this . centerOfScreen ) { this . lastPosition = 'left' ; } else { this . lastPosition = 'right' ; } } }