Fall down

Por 9.99€ al mes tendrás acceso completo a todos los cursos. Sin matrícula ni permanencia.

Cargar fondo

Cargamos la imagen de fondo de la escena principal.

Físicas

create() {
	...
	this.launchMissile();
}

launchMissile() {
	const missile = this.physics.add.sprite(50, 100, 'missile0');
	missile.setVelocity(0, 100);
}
main.js
const config = {
	type: Phaser.AUTO,
	width: 960,
	height: 640,
	scene: Escena,
	scale: {
		mode: Phaser.Scale.FIT
	},
	physics: {
		default: 'arcade',
		arcade: {
			debug: true,
			gravity: {
				y: 300,
			},
		},
	},
};

Poner nuevos misiles cada cierto tiempo

launchMissile() {
	...
	this.time.delayedCall(1000, this.launchMissile, [], this);
}

Detectar pulsación

missile.setInteractive();
missile.on('pointerdown', () => this.clickedMissile(missile));
clickedMissile(m) {
	m.destroy();
}

Misiles variados

const random= Math.floor(Math.random() * 2);
const missile = this.physics.add.sprite(50, -100, `missile${random}`);

Diferentes posiciones

launchMissile() {
	...
	const missilePosition = Math.floor(Math.random() * (gameWidth -100)) + 50;
	...
}

Cohetes explotan

preload() {
	...
	this.load.spritesheet('crash', crash, {
		frameWidth: 199,
		frameHeight: 200
	});
}

create() {
	...
	this.anims.create({
		key: 'crashAnim',
		frames: this.anims.generateFrameNumbers('crash', {start: 0, end: 4}),
		frameRate: 7
	});
}

clickedMissile(m) {
	m.play("crashAnim");
}

Cohetes explotan y desaparecen

launchMissile(){
	...
	missile.on('animationcomplete', this.animationComplete, this);
	...
}

animationComplete(animation, frame, sprite) {
	if (animation.key === 'crashAnim') {
		sprite.destroy();
	}
}

Poner vidas en pantalla

preload(){
	...
	this.load.spritesheet('life', life, {
		frameWidth: 50,
		frameHeight: 50
	});
}

create(){
	...
	this.life1 = this.add.sprite(50, 30, 'life');
	this.life2 = this.add.sprite(100, 30, 'life');
	this.life3 = this.add.sprite(150, 30, 'life');
}

Gestionar vidas

create(){
	...
	this.lifeCounter= 3;

	this.physics.world.on('worldbounds', (body, up, down, left, right) => {
            if (down) {
                body.gameObject.disableBody();
                body.gameObject.play("crashAnim");
                console.log(up, down, left, right, body),
                    --this.lifeCounter;
                if (this.lifeCounter == 2) {
                    this.life3.setFrame(1);
                } else if (this.lifeCounter == 1) {
                    this.life2.setFrame(1);
                } else if (this.lifeCounter == 0) {
                    this.life1.setFrame(1);
                    this.scene.start('Lose');
                }
            }
        });
}

launchMissile(){
	...
	missile.setCollideWorldBounds(true); 
	missile.body.onWorldBounds = true;
}

Por 9.99€ al mes tendrás acceso completo a todos los cursos. Sin matrícula ni permanencia.