Hi! I have a file of a group of enemies in phaser 3.
My file:
export default class PigGroup extends Phaser.Physics.Arcade.Group {
constructor(physicsWorld, scene) {
super(physicsWorld, scene);
}
crearPig(x,y){
this.create(x,y,'Pig')
.setGravityY(1000)
.setSize(30,30); //hitbox
}
}
I want to create his own animations inside the file and later call them in scene.
Does anyone could help me please?
samme
February 16, 2022, 6:15pm
2
You can use this.scene.anims.create()
within PigGroup
methods.
@samme bro and how do I call an animation in scene? For every members
Now my file is like this, is it correct?
export default class PigGroup extends Phaser.Physics.Arcade.Group {
constructor(physicsWorld, scene) {
super(physicsWorld, scene);
this.animaciones();
}
crearPig(x,y){
this.create(x,y,'Pig')
.setGravityY(1000)
.setSize(30,30); //hitbox
}
animaciones(){
this.scene.anims.create({
key: 'PigEstatico',
frames: this.scene.anims.generateFrameNumbers('Pig', { start: 0, end: 10 }),
frameRate: 15
});
}
}
samme
February 16, 2022, 8:31pm
5
That looks fine.
To play an animation on all group members you do
group.playAnimation('PigEstatico');
@samme bro I tried that but it doesn’t work, in console appeared “Missing animation: PigEstatico”
Could u help me?
samme
February 16, 2022, 10:54pm
7
You need to create the animation before playing it.
You don’t need to create the animation in the PigGroup
class, although you can.
@samme Yes, I created the animation before playing it, but nothing happens.
Also I tried creating the animation in the scene but nothing happens too.
I’ve been trying all day but I can’t
samme
February 17, 2022, 5:44pm
9
Are you still getting “Missing animation”?
What’s the scene code?
@samme this is scene code:
import Rey from “…/clases/rey.js”;
import Pig from “…/clases/pig.js”;
import PigGroup from “…/clases/pigGroup.js”;
import CamaraYMovimiento from “…/clases/CamaraYMovimiento.js”;
export default class Scene1 extends Phaser.Scene {
constructor(){
super({key: ‘Scene1’});
}
preload(){
//mapa
this.load.image("TILES1", "assets/maps/TileSets/Terrain.png");
this.load.image("TILES2", "assets/maps/TileSets/Decorations.png");
this.load.tilemapTiledJSON('map1',"assets/maps/map1.json");
//REY
this.load.spritesheet('Rey','assets/sprites/KingHuman/Idle.png', {frameWidth: 78, frameHeight: 58});
this.load.spritesheet('ReyC','assets/sprites/KingHuman/Run.png', {frameWidth: 78, frameHeight: 58});
//puerquitos
this.load.spritesheet('Pig','assets/sprites/Pig/Idle.png', {frameWidth: 34, frameHeight: 28});
}
create(){
//mapa
const map1 = this.make.tilemap({ key: "map1", tileWidth: 32, tileHeight: 32});
//solidos
const tileset = map1.addTilesetImage("tiles1", "TILES1");
const layer = map1.createLayer("solidos", tileset, 0,0);
//decoraciones
const tileset2 = map1.addTilesetImage("tiles2", "TILES2");
const layer2 = map1.createLayer("decoraciones", tileset2, 0,0);
//rey
this.rey = new Rey(this, 150, 400, 'Rey');
this.pigGroup = new PigGroup(this.physics.world, this);
this.pigGroup.crearPig(200,200);
this.pigGroup.crearPig(250,200);
this.pigGroup.animaciones();
//colisiones
layer.setCollisionByProperty({ solido: true});
this.physics.add.collider(this.rey, layer);
this.physics.add.collider(this.pigGroup,layer);
//camara
this.CamaraYMovimiento = new CamaraYMovimiento(this,map1,this.rey);
}
update(){
this.rey.update();
this.pigGroup.playAnimation('PigEstatico');
}
}