Loading
DevLeague Coding Bootcamp
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
Part 2 - Building Games with the Phaser Framework
Game.hero = null;
Game.platformsGroup = null;
Game.cursors = null;
const preload = _ => {
game.load.atlasJSONHash(
CFG.ASSETS.GFX,
CFG.ASSETS.ATLAS_PNG_PATH,
CFG.ASSETS.ATLAS_JSON_PATH
);
};
const create = _ => {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.arcade.gravity.y = CFG.GRAVITY;
game.stage.backgroundColor = CFG.BG_COLOR;
Game.cursors = game.input.keyboard.createCursorKeys();
Game.platformsGroup = game.add.group();
Game.LevelDesigner.load(game, 1);
Game.hero = new Game.Hero(game, 500, CFG.GAME_HEIGHT - 200);
game.camera.follow(hero.sprite, null, CFG.CAMERA_LERP, CFG.CAMERA_LERP);
};
do not copy and paste these class definitions
((Phaser, Game, CFG) => {
// get or create Game module
if( Game === undefined ){
Game = window.Game = {};
}
Game.Hero = class{
constructor(){
}
};
})(window.Phaser, window.Game, window.Game.Configuration);
<script src="./js/configuration.js"></script>
<script src="./js/LevelDesigner.js"></script>
<script src="./js/main.js"></script>
HERE
const SCALE = 1;
const MOVE_SPEED = 850;
const JUMP_VELOCITY = 2950;
// also height, gravity:9750 jumpVel:2950 = can clear 400px
const ANIMATIONS = {
IDLE_SPEED : 8,
LEFT_SPEED : 8,
RIGHT_SPEED : 8,
JUMP_SPEED : 4,
};
this.game = game;
this.sprite = this.game.add.sprite(x, y, CFG.ASSETS.GFX);
this.sprite.scale.set(SCALE);
this.game.physics.enable(this.sprite, Phaser.Physics.ARCADE);
this.sprite.body.setSize(30, 80, 52, 25);
this.animations = {
idle : this.sprite.animations.add('idle', [ 'hero-idle-1.png', 'hero-idle-2.png' ]),
left : this.sprite.animations.add('left', [ 'hero-left-1.png', 'hero-left-2.png' ]),
right : this.sprite.animations.add('right', [ 'hero-right-1.png', 'hero-right-2.png' ]),
jump : this.sprite.animations.add('jump', [ 'hero-jump-2.png', 'hero-jump-1.png' ]),
};
// allows passing through platforms
this.sprite.body.checkCollision.up =
this.sprite.body.checkCollision.left =
this.sprite.body.checkCollision.right = false;
this.sprite.update = this.update.bind(this);
constructor(game, x, y)
update(){
let hitPlatform = this.game.physics.arcade.collide(this.sprite, Game.platformsGroup);
let jumping = this.sprite.body.velocity.y !== 0;
this.sprite.body.velocity.x = 0;
if (Game.cursors.left.isDown) {
this.sprite.body.velocity.x = -MOVE_SPEED;
if(!jumping && this.sprite.animations.currentAnim !== this.animations.left){
this.animations.left.play(ANIMATIONS.LEFT_SPEED, true);
}
} else if (Game.cursors.right.isDown) {
this.sprite.body.velocity.x = MOVE_SPEED;
if(!jumping && this.sprite.animations.currentAnim !== this.animations.right){
this.animations.right.play(ANIMATIONS.RIGHT_SPEED, true);
}
} else if(!jumping){
// Stand still
if(this.sprite.animations.currentAnim !== this.animations.idle){
this.animations.idle.play(ANIMATIONS.IDLE_SPEED, true);
}
}
// Allow the player to jump if they are touching the ground.
if (Game.cursors.up.isDown && this.sprite.body.touching.down && hitPlatform)
{
this.sprite.body.velocity.y = -JUMP_VELOCITY;
if(this.sprite.animations.currentAnim !== this.animations.jump){
this.animations.jump.play(ANIMATIONS.JUMP_SPEED, false);
}
}
// wrap around stage
if(this.sprite.x > CFG.GAME_WIDTH){
this.sprite.x = -this.sprite.width;
} else if(this.sprite.x < -this.sprite.width){
this.sprite.x = CFG.GAME_WIDTH;
}
}
const SCALE = 1;
const ATLAS_LABELS = {
1 : 'platform-1.png',
2 : 'platform-2.png',
3 : 'platform-3.png',
4 : 'platform-4.png',
};
/*
* y is the height from the BOTTOM of the stage
* only the top edge should be collidable
*
*/
constructor(game, x, y, size){
if(size < 1 || size > 4){
throw new RangeError('Platform size must be 1-4');
}
this.game = game;
this.sprite = this.game.add.sprite(x, CFG.GAME_HEIGHT - y, CFG.ASSETS.GFX, ATLAS_LABELS[size]);
this.sprite.scale.set(SCALE);
this.game.physics.enable(this.sprite, Phaser.Physics.ARCADE);
this.sprite.body.allowGravity = false;
this.sprite.body.immovable = true;
}
constructor(game, x, y, size)
const SCALE = 1;
this.game = game;
this.sprite = this.game.add.sprite(x, CFG.GAME_HEIGHT - y, CFG.ASSETS.GFX, spriteLabel);
this.sprite.scale.set(SCALE);
this.game.physics.enable(this.sprite, Phaser.Physics.ARCADE);
// allows passing through platforms
// #TODO this will be bad for checking other objects though
this.sprite.body.checkCollision.up =
this.sprite.body.checkCollision.left =
this.sprite.body.checkCollision.right = false;
this.sprite.update = this.update.bind(this);
constructor(game, x, y, spriteLabel)
update(){
let hitPlatform = this.game.physics.arcade.collide(this.sprite, Game.platformsGroup);
// wrap around stage
if(this.sprite.x > CFG.GAME_WIDTH){
this.sprite.x = -this.sprite.width;
} else if(this.sprite.x < -this.sprite.width){
this.sprite.x = CFG.GAME_WIDTH;
}
}
const SPAWN_Y_OFFSET = 97;
constructor(game, x, y){
super(game, x, y + SPAWN_Y_OFFSET, 'baddie-2.png');
this.sprite.body.setSize(5, 72, 100, 22);
}
const SPAWN_Y_OFFSET = 103;
constructor(game, x, y){
super(game, x, y + SPAWN_Y_OFFSET, 'baddie-1.png');
this.sprite.body.setSize(15, 75, 75, 27);
}
see /assets/pngs/ directory to find the asset name
see if you can follow the patterns in the LevelDesigner module
const levels = {
1 :
`-----------------------------|
|
|
|
|
___o___ ___o___ ___o___|
|
|
|
|
_______ |
spawn 3 ammo pellets using 'o' character
follow the patterns in the first Phaser Workshop
follow the patterns in the first Phaser Workshop
after creating the "fire" key
in main.js create()
Game.cursors.fire.onUp.add( Game.hero.handleFire.bind(Game.hero) );
follow the patterns in the first Phaser Workshop
// keep track of enemies
Game.enemiesGroup = game.add.group();
in main.js create()
in LevelDesigner.js create()
switch(SpawnableClass){
case Game.GhostEnemy:
Game.enemiesGroup.add(new Game.GhostEnemy(game, x, y).sprite);
break;
case Game.SparkEnemy:
Game.enemiesGroup.add(new Game.SparkEnemy(game, x, y).sprite);
break;
case Game.Ammo: new Game.Ammo(game, x, y); break;
}
follow the patterns in the first Phaser Workshop
https://github.com/GomaGames/Phaser-SHMUP/compare/5_destroy-baddies...6_player-death
const update = _ => {
if( Game.enemiesGroup.children.some( enemySprite =>
enemySprite.overlap(Game.hero.sprite))
){
gameOver();
}
};
const handlePointerPressed = pointer => {
console.log(pointer.clientX,pointer.clientY);
};
game.input.onTap.add( handlePointerPressed.bind(this) );