2014-01-22 16:16:53 +00:00
|
|
|
|
|
|
|
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
|
|
|
|
|
|
|
function preload() {
|
|
|
|
|
|
|
|
game.load.tilemap('map', 'assets/tilemaps/maps/platform.json', null, Phaser.Tilemap.TILED_JSON);
|
|
|
|
game.load.image('platformer_tiles', 'assets/tilemaps/tiles/platformer_tiles.png');
|
|
|
|
game.load.spritesheet('gameboy', 'assets/sprites/gameboy_seize_color_40x60.png', 40, 60);
|
2014-01-23 01:50:43 +00:00
|
|
|
game.load.spritesheet('balls', 'assets/sprites/balls.png', 17, 17);
|
2014-01-22 16:16:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var map;
|
|
|
|
var layer;
|
|
|
|
var sprite;
|
2014-01-23 01:50:43 +00:00
|
|
|
var sprite2;
|
|
|
|
var balls;
|
2014-01-22 16:16:53 +00:00
|
|
|
|
|
|
|
function create() {
|
|
|
|
|
|
|
|
game.stage.backgroundColor = '#124184';
|
|
|
|
|
|
|
|
map = game.add.tilemap('map');
|
|
|
|
|
|
|
|
map.addTilesetImage('platformer_tiles');
|
|
|
|
|
2014-01-23 01:50:43 +00:00
|
|
|
map.setCollisionBetween(21, 53);
|
2014-01-22 16:16:53 +00:00
|
|
|
|
|
|
|
layer = map.createLayer('Tile Layer 1');
|
|
|
|
|
|
|
|
// layer.debug = true;
|
|
|
|
|
2014-01-23 01:50:43 +00:00
|
|
|
game.physics.gravity.y = 150;
|
|
|
|
|
|
|
|
balls = game.add.group();
|
|
|
|
|
|
|
|
for (var i = 0; i < 50; i++)
|
|
|
|
{
|
2014-01-23 21:45:35 +00:00
|
|
|
var s = balls.create(game.rnd.integerInRange(100, 700), game.rnd.integerInRange(100, 200), 'balls', game.rnd.integerInRange(0, 6));
|
2014-01-23 01:50:43 +00:00
|
|
|
s.body.velocity.x = game.rnd.integerInRange(-400, 400);
|
|
|
|
s.body.velocity.y = game.rnd.integerInRange(-100, -200);
|
|
|
|
s.name = 'ball' + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
balls.setAll('body.collideWorldBounds', true);
|
|
|
|
balls.setAll('body.bounce.x', 0.8);
|
|
|
|
balls.setAll('body.bounce.y', 0.9);
|
2014-01-23 21:45:35 +00:00
|
|
|
balls.setAll('body.minBounceVelocity', 0.9);
|
|
|
|
balls.setAll('body.friction', 0.5);
|
2014-01-22 16:16:53 +00:00
|
|
|
|
|
|
|
sprite = game.add.sprite(300, 100, 'gameboy', 0);
|
|
|
|
sprite.name = 'red';
|
|
|
|
sprite.body.collideWorldBounds = true;
|
2014-01-23 21:45:35 +00:00
|
|
|
sprite.body.minBounceVelocity = 0.9;
|
|
|
|
sprite.body.bounce.setTo(0.5, 0.9);
|
|
|
|
sprite.body.friction = 0.5;
|
2014-01-22 16:16:53 +00:00
|
|
|
|
2014-01-23 01:50:43 +00:00
|
|
|
sprite2 = game.add.sprite(300, 250, 'gameboy', 2);
|
|
|
|
sprite2.name = 'yellow';
|
|
|
|
sprite2.body.collideWorldBounds = true;
|
|
|
|
// sprite2.body.bounce.setTo(0.5, 0.5);
|
|
|
|
|
2014-01-22 16:16:53 +00:00
|
|
|
game.input.onDown.add(launch, this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function launch() {
|
|
|
|
|
|
|
|
sprite.body.velocity.x = -200;
|
|
|
|
sprite.body.velocity.y = -200;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function hit(face, body1, body2) {
|
|
|
|
|
|
|
|
console.log('hit', face);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-23 01:50:43 +00:00
|
|
|
var flag = false;
|
|
|
|
|
2014-01-22 16:16:53 +00:00
|
|
|
function update() {
|
|
|
|
|
2014-01-23 01:50:43 +00:00
|
|
|
game.physics.collide(balls, layer);
|
2014-01-22 16:16:53 +00:00
|
|
|
game.physics.collide(sprite, layer);
|
2014-01-23 01:50:43 +00:00
|
|
|
game.physics.collide(sprite2, layer);
|
|
|
|
game.physics.collide(sprite, sprite2);
|
|
|
|
|
|
|
|
if (!flag)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < balls._container.children.length; i++)
|
|
|
|
{
|
|
|
|
if (balls._container.children[i].y < 5 && balls._container.children[i].body.velocity.y === 0)
|
|
|
|
{
|
|
|
|
balls._container.children[i].alpha = 0.5;
|
|
|
|
console.log(balls._container.children[i]);
|
|
|
|
flag = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-01-22 16:16:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
|
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
game.debug.renderBodyInfo(sprite, 20, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|