Updated games examples

This commit is contained in:
alvinsight 2014-03-13 11:47:17 +00:00
parent 39add47ac3
commit 20d7ebd0c0
6 changed files with 93 additions and 55 deletions

View file

@ -26,12 +26,14 @@ var s;
function create() {
// This tells the world to include walls on the left, right and top, but not the bottom - so the ball can 'fall' away.
game.physics.setBoundsToWorld(true, true, true, false);
game.physics.setBoundsToWorld();
s = game.add.tileSprite(0, 0, 800, 600, 'starfield');
var brick;
bricks = game.add.group();
bricks.enableBody = true;
bricks.physicsBodyType = Phaser.Physics.ARCADE;
for (var y = 0; y < 4; y++)
{
@ -45,12 +47,14 @@ function create() {
paddle = game.add.sprite(game.world.centerX, 500, 'breakout', 'paddle_big.png');
paddle.anchor.setTo(0.5, 0.5);
game.physics.enable(paddle, Phaser.Physics.ARCADE);
paddle.body.collideWorldBounds = true;
paddle.body.bounce.setTo(1, 1);
paddle.body.immovable = true;
ball = game.add.sprite(game.world.centerX, paddle.y - 16, 'breakout', 'ball_1.png');
ball.anchor.setTo(0.5, 0.5);
game.physics.enable(ball, Phaser.Physics.ARCADE);
ball.body.collideWorldBounds = true;
ball.body.bounce.setTo(1, 1);
ball.animations.add('spin', [ 'ball_1.png', 'ball_2.png', 'ball_3.png', 'ball_4.png', 'ball_5.png' ], 50, true, false);
@ -70,25 +74,29 @@ function update () {
// Fun, but a little sea-sick inducing :) Uncomment if you like!
// s.tilePosition.x += (game.input.speed.x / 2);
paddle.x = game.input.x;
paddle.body.x = game.input.x;
if (paddle.x < 24)
if (paddle.body.x < 24)
{
paddle.x = 24;
paddle.body.x = 24;
}
else if (paddle.x > game.width - 24)
else if (paddle.body.x > game.width - 24)
{
paddle.x = game.width - 24;
paddle.body.x = game.width - 24;
}
if(ball.body.y > game.height - 20){
ballLost();
}
if (ballOnPaddle)
{
ball.x = paddle.x;
ball.body.x = paddle.body.x;
}
else
{
game.physics.collide(ball, paddle, ballHitPaddle, null, this);
game.physics.collide(ball, bricks, ballHitBrick, null, this);
game.physics.arcade.collide(ball, paddle, ballHitPaddle, null, this);
game.physics.arcade.collide(ball, bricks, ballHitBrick, null, this);
}
}
@ -116,11 +124,13 @@ function ballLost () {
}
else
{
livesText.content = 'lives: ' + lives;
console.log('Hi');
livesText.text = 'lives: ' + lives;
ballOnPaddle = true;
ball.x = paddle.x + 16;
ball.y = paddle.y - 16;
ball.body.reset();
ball.body.x = paddle.body.x + 16;
ball.body.y = paddle.y - 22;
ball.animations.stop();
}
@ -130,7 +140,7 @@ function gameOver () {
ball.body.velocity.setTo(0, 0);
introText.content = 'Game Over!';
introText.text = 'Game Over!';
introText.visible = true;
}
@ -141,15 +151,15 @@ function ballHitBrick (_ball, _brick) {
score += 10;
scoreText.content = 'score: ' + score;
scoreText.text = 'score: ' + score;
// Are they any bricks left?
if (bricks.countLiving() == 0)
{
// New level starts
score += 1000;
scoreText.content = 'score: ' + score;
introText.content = '- Next Level -';
scoreText.text = 'score: ' + score;
introText.text = '- Next Level -';
// Let's move the ball back to the paddle
ballOnPaddle = true;

View file

@ -32,11 +32,16 @@ var livingEnemies = [];
function create() {
// The scrolling starfield background
starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');
// Our bullet group
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(30, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 1);
@ -44,6 +49,8 @@ function create() {
// The enemy's bullets
enemyBullets = game.add.group();
enemyBullets.enableBody = true;
enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;
enemyBullets.createMultiple(30, 'enemyBullet');
enemyBullets.setAll('anchor.x', 0.5);
enemyBullets.setAll('anchor.y', 1);
@ -52,22 +59,25 @@ function create() {
// The hero!
player = game.add.sprite(400, 500, 'ship');
player.anchor.setTo(0.5, 0.5);
game.physics.enable(player, Phaser.Physics.ARCADE);
// The baddies!
aliens = game.add.group();
aliens.enableBody = true;
aliens.physicsBodyType = Phaser.Physics.ARCADE;
createAliens();
// The score
scoreString = 'Score : ';
scoreText = game.add.text(10, 10, scoreString + score, { fontSize: '34px', fill: '#fff' });
scoreText = game.add.text(10, 10, scoreString + score, { font: '34px Arial', fill: '#fff' });
// Lives
lives = game.add.group();
game.add.text(game.world.width - 100, 10, 'Lives : ', { fontSize: '34px', fill: '#fff' });
game.add.text(game.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' });
// Text
stateText = game.add.text(game.world.centerX,game.world.centerY,'', { fontSize: '84px', fill: '#fff' });
stateText = game.add.text(game.world.centerX,game.world.centerY,' ', { font: '84px Arial', fill: '#fff' });
stateText.anchor.setTo(0.5, 0.5);
stateText.visible = false;
@ -79,14 +89,22 @@ function create() {
ship.alpha = 0.4;
}
// An explosion pool
explosions = game.add.group();
explosions.createMultiple(30, 'kaboom');
explosions.forEach(setupInvader, this);
// And some controls to play the game with
cursors = game.input.keyboard.createCursorKeys();
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}
function createAliens () {
@ -155,8 +173,8 @@ function update() {
}
// Run collision
game.physics.overlap(bullets, aliens, collisionHandler, null, this);
game.physics.overlap(enemyBullets, player, enemyHitsPlayer, null, this);
game.physics.arcade.overlap(bullets, aliens, collisionHandler, null, this);
game.physics.arcade.overlap(enemyBullets, player, enemyHitsPlayer, null, this);
}
@ -168,20 +186,20 @@ function collisionHandler (bullet, alien) {
// Increase the score
score += 20;
scoreText.content = scoreString + score;
scoreText.text = scoreString + score;
// And create an explosion :)
var explosion = explosions.getFirstDead();
var explosion = explosions.getFirstExists(false);
explosion.reset(alien.body.x, alien.body.y);
explosion.play('kaboom', 30, false, true);
if (aliens.countLiving() == 0)
{
score += 1000;
scoreText.content = scoreString + score;
scoreText.text = scoreString + score;
enemyBullets.callAll('kill',this);
stateText.content = " You Won, \n Click to restart";
stateText.text = " You Won, \n Click to restart";
stateText.visible = true;
//the "click to restart" handler
@ -202,7 +220,7 @@ function enemyHitsPlayer (player,bullet) {
}
// And create an explosion :)
var explosion = explosions.getFirstDead();
var explosion = explosions.getFirstExists(false);
explosion.reset(player.body.x, player.body.y);
explosion.play('kaboom', 30, false, true);
@ -212,7 +230,7 @@ function enemyHitsPlayer (player,bullet) {
player.kill();
enemyBullets.callAll('kill');
stateText.content=" GAME OVER \n Click to restart";
stateText.text=" GAME OVER \n Click to restart";
stateText.visible = true;
//the "click to restart" handler
@ -238,14 +256,14 @@ function enemyFires () {
if (enemyBullet && livingEnemies.length > 0)
{
var random=game.rnd.integerInRange(0,livingEnemies.length);
var random=game.rnd.integerInRange(0,livingEnemies.length-1);
// randomly select one of them
var shooter=livingEnemies[random];
// And fire the bullet from this enemy
enemyBullet.reset(shooter.body.x, shooter.body.y);
game.physics.moveToObject(enemyBullet,player,120);
game.physics.arcade.moveToObject(enemyBullet,player,120);
firingTimer = game.time.now + 2000;
}

View file

@ -6,7 +6,7 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:
function preload() {
game.load.tilemap('matching', 'assets/tilemaps/maps/phaser_tiles.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/tilemaps/tiles/phaser_tiles.png', 100, 100, -1, 1, 1);
game.load.image('tiles', 'assets/tilemaps/tiles/phaser_tiles.png');
}
var timeCheck = 0;
@ -41,11 +41,11 @@ function create() {
map = game.add.tilemap('matching');
tileset = game.add.tileset('tiles');
layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0);
map.addTilesetImage('tiles', 'tiles');
layer = map.createLayer('World1');
//layer.resizeWorld();
layer.resizeWorld();
marker = game.add.graphics();
marker.lineStyle(2, 0x00FF00, 1);

View file

@ -30,6 +30,7 @@ function create() {
{
item = simon.create(150 + 168 * i, 150, 'item', i);
// Enable input.
item.inputEnabled = true;
item.input.start(0, true);
item.events.onInputDown.add(select);
item.events.onInputUp.add(release);
@ -41,6 +42,7 @@ function create() {
{
item = simon.create(150 + 168 * i, 318, 'item', i + 3);
// Enable input.
item.inputEnabled = true;
item.input.start(0, true);
item.events.onInputDown.add(select);
item.events.onInputUp.add(release);

View file

@ -43,10 +43,11 @@ function create() {
layer.resizeWorld();
game.physics.gravity.y = 250;
game.physics.setBoundsToWorld();
game.physics.arcade.gravity.y = 250;
game.physics.arcade.setBoundsToWorld();
player = game.add.sprite(32, 32, 'dude');
game.physics.enable(player, Phaser.Physics.ARCADE);
player.body.bounce.y = 0.2;
player.body.minVelocity.y = 5;
player.body.collideWorldBounds = true;
@ -68,7 +69,7 @@ function create() {
function update() {
game.physics.collide(player, layer);
game.physics.arcade.collide(player, layer);
player.body.velocity.x = 0;
@ -121,10 +122,10 @@ function update() {
function render () {
if (player.debug)
{
game.debug.physicsBody(player.body);
game.debug.bodyInfo(player, 16, 24);
}
// if (player.debug)
// {
// game.debug.physicsBody(player.body);
// game.debug.bodyInfo(player, 16, 24);
// }
}

View file

@ -21,13 +21,14 @@ EnemyTank = function (index, game, player, bullets) {
this.turret.anchor.setTo(0.3, 0.5);
this.tank.name = index.toString();
game.physics.enable(this.tank, Phaser.Physics.ARCADE);
this.tank.body.immovable = false;
this.tank.body.collideWorldBounds = true;
this.tank.body.bounce.setTo(1, 1);
this.tank.angle = game.rnd.angle();
game.physics.velocityFromRotation(this.tank.rotation, 100, this.tank.body.velocity);
game.physics.arcade.velocityFromRotation(this.tank.rotation, 100, this.tank.body.velocity);
};
@ -58,9 +59,9 @@ EnemyTank.prototype.update = function() {
this.turret.x = this.tank.x;
this.turret.y = this.tank.y;
this.turret.rotation = this.game.physics.angleBetween(this.tank, this.player);
this.turret.rotation = this.game.physics.arcade.angleBetween(this.tank, this.player);
if (this.game.physics.distanceBetween(this.tank, this.player) < 300)
if (this.game.physics.arcade.distanceBetween(this.tank, this.player) < 300)
{
if (this.game.time.now > this.nextFire && this.bullets.countDead() > 0)
{
@ -70,7 +71,7 @@ EnemyTank.prototype.update = function() {
bullet.reset(this.turret.x, this.turret.y);
bullet.rotation = this.game.physics.moveToObject(bullet, this.player, 500);
bullet.rotation = this.game.physics.arcade.moveToObject(bullet, this.player, 500);
}
}
@ -123,7 +124,8 @@ function create () {
tank.animations.add('move', ['tank1', 'tank2', 'tank3', 'tank4', 'tank5', 'tank6'], 20, true);
// This will force it to decelerate and limit its speed
tank.body.linearDamping = 0.2;
game.physics.enable(tank, Phaser.Physics.ARCADE);
tank.body.drag.set(0.2);
tank.body.maxVelocity.setTo(400, 400);
tank.body.collideWorldBounds = true;
@ -133,7 +135,10 @@ function create () {
// The enemies bullet group
enemyBullets = game.add.group();
enemyBullets.enableBody = true;
enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;
enemyBullets.createMultiple(100, 'bullet');
enemyBullets.setAll('anchor.x', 0.5);
enemyBullets.setAll('anchor.y', 0.5);
enemyBullets.setAll('outOfBoundsKill', true);
@ -152,6 +157,8 @@ function create () {
// Our bullet group
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(30, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 0.5);
@ -192,14 +199,14 @@ function removeLogo () {
function update () {
game.physics.overlap(enemyBullets, tank, bulletHitPlayer, null, this);
game.physics.arcade.overlap(enemyBullets, tank, bulletHitPlayer, null, this);
for (var i = 0; i < enemies.length; i++)
{
if (enemies[i].alive)
{
game.physics.collide(tank, enemies[i].tank);
game.physics.overlap(bullets, enemies[i].tank, bulletHitEnemy, null, this);
game.physics.arcade.collide(tank, enemies[i].tank);
game.physics.arcade.overlap(bullets, enemies[i].tank, bulletHitEnemy, null, this);
enemies[i].update();
}
}
@ -228,7 +235,7 @@ function update () {
if (currentSpeed > 0)
{
game.physics.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);
game.physics.arcade.velocityFromRotation(tank.rotation, currentSpeed, tank.body.velocity);
}
land.tilePosition.x = -game.camera.x;
@ -242,7 +249,7 @@ function update () {
turret.x = tank.x;
turret.y = tank.y;
turret.rotation = game.physics.angleToPointer(turret);
turret.rotation = game.physics.arcade.angleToPointer(turret);
if (game.input.activePointer.isDown)
{
@ -266,7 +273,7 @@ function bulletHitEnemy (tank, bullet) {
if (destroyed)
{
var explosionAnimation = explosions.getFirstDead();
var explosionAnimation = explosions.getFirstExists(false);
explosionAnimation.reset(tank.x, tank.y);
explosionAnimation.play('kaboom', 30, false, true);
}
@ -279,11 +286,11 @@ function fire () {
{
nextFire = game.time.now + fireRate;
var bullet = bullets.getFirstDead();
var bullet = bullets.getFirstExists(false);
bullet.reset(turret.x, turret.y);
bullet.rotation = game.physics.moveToPointer(bullet, 1000);
bullet.rotation = game.physics.arcade.moveToPointer(bullet, 1000, game.input.activePointer, 500);
}
}