I’m pretty new to Phaser let alone Phaser 3 and I’m not completely sure how to make this work. But as the title suggests, how would I make a sprite animated from inside the enemy object class? I believe I have to keep it inside the enemy object since the enemy object is being called into my scene class and inside the enemy object class has my the functions in which the enemy will take damage and die.
And now my predicament, I’m not entirely sure if how to implement the animation inside the object class, especially when the object is moving from point to point until it hits the last point(and then is removed off the gameboard).
var Enemy = new Phaser.Class({ Extends: Phaser.GameObjects.Image, initialize: function Enemy (scene) { Phaser.GameObjects.Image.call(this, scene, 0, 0, 'spider'); this.follower = { t: 0, vec: new Phaser.Math.Vector2() }; }, startOnPath: function () { // set the t parameter at the start of the path this.follower.t = 0; // get x and y of the given t point path.getPoint(this.follower.t, this.follower.vec); // set the x and y of our enemy to the received from the previous step this.setPosition(this.follower.vec.x, this.follower.vec.y); // set HP for each enemy this.hp = ENEMY_LIFE; }, receiveDamage: function(damage) { this.hp -= BULLET_DAMAGE; // if hp drops below 0 we deactivate this enemy if(this.hp <= 0) { this.setActive(false); this.setVisible(false); addScore(10); addMoney(15); endOfRoundTextPopulate(); // when there are no more enemies on the board round over text will display } }, update: function (time, delta) { // move the t point along the path, 0 is the start and 0 is the end this.follower.t += ENEMY_SPEED * delta; // get the new x and y coordinates in vec enemyPosition = path.getPoint(this.follower.t, this.follower.vec); // update enemy x and y to the newly obtained x and y moveToPoint = this.setPosition(this.follower.vec.x, this.follower.vec.y); // if we have reached the end of the path, remove the enemy if (this.follower.t >= 1) { this.setActive(false); this.setVisible(false); }; // lose a life when an enemy gets to the end point if (enemyPosition === path.getPoint(860, 580)){ PLAYER_LIFE -= 1; textLife.text = 'Life: ' + PLAYER_LIFE; endOfRoundTextPopulate(); // when there are no more enemies on the board round over text will display } } });
I have a sprite sheet that animates the ‘spider’ image to go straight, right, left and down. For testing purposes, I broke up the sprite sheet just for the walking straight animation. I could place the animation on the game board via create() function in the mainScene itself with…
this.anims.create({ key: 'walkStraight', frames: [ { key: 'spider'}, { key: 'spider2'}, { key: 'spider3'}, { key: 'spider4'}, { key: 'spider5'}, { key: 'spider6', duration: 100} ], frameRate: 8, repeat: -1 }); this.add.sprite(100, 655, 'spider').play('walkStraight');
But this doesn’t exactly move it from point to point. I’ve looked at the animation examples for Phaser 3, but so far I’ve gained no ground.
Any help in getting pointed in the right direction would be appreciated.