phaser/Tests/misc/multi game.ts

105 lines
2.5 KiB
TypeScript
Raw Normal View History

/// <reference path="../../Phaser/Game.ts" />
2013-04-12 16:19:56 +00:00
(function () {
// Let's test having 2 totally separate games embedded on the same page
2013-04-18 13:16:18 +00:00
var myGame = new Phaser.Game(this, 'game', 400, 400, init, create, update);
2013-04-12 16:19:56 +00:00
// They can share the same parent div, they'll just butt-up next to each other
2013-04-18 13:16:18 +00:00
var myGame2 = new Phaser.Game(this, 'game', 400, 400, init2, create2, update2);
2013-04-12 16:19:56 +00:00
function init() {
myGame.world.setSize(3000, 3000);
myGame.loader.addImageFile('melon', 'assets/sprites/melon.png');
myGame.loader.load();
}
function create() {
myGame.camera.setBounds(0, 0, myGame.world.width, myGame.world.height);
for (var i = 0; i < 1000; i++)
{
myGame.createSprite(myGame.world.randomX, myGame.world.randomY, 'melon');
}
}
function update() {
2013-04-18 13:16:18 +00:00
if (myGame.input.keyboard.isDown(Phaser.Keyboard.LEFT))
2013-04-12 16:19:56 +00:00
{
myGame.camera.scroll.x -= 4;
}
2013-04-18 13:16:18 +00:00
else if (myGame.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
2013-04-12 16:19:56 +00:00
{
myGame.camera.scroll.x += 4;
}
2013-04-18 13:16:18 +00:00
if (myGame.input.keyboard.isDown(Phaser.Keyboard.UP))
2013-04-12 16:19:56 +00:00
{
myGame.camera.scroll.y += 4;
}
2013-04-18 13:16:18 +00:00
else if (myGame.input.keyboard.isDown(Phaser.Keyboard.DOWN))
2013-04-12 16:19:56 +00:00
{
myGame.camera.scroll.y -= 4;
}
}
// And now for game 2, we're basically just duplicating the functions from above, but that's fine for this test
function init2() {
myGame2.world.setSize(1920, 1920);
myGame2.loader.addImageFile('grid', 'assets/tests/debug-grid-1920x1920.png');
myGame2.loader.addImageFile('car', 'assets/sprites/car90.png');
myGame2.loader.load();
}
var car;
function create2() {
myGame2.camera.setBounds(0, 0, myGame.world.width, myGame.world.height);
myGame2.createSprite(0, 0, 'grid');
car = myGame2.createSprite(400, 300, 'car');
myGame2.camera.follow(car);
}
function update2() {
car.velocity.x = 0;
car.velocity.y = 0;
car.angularVelocity = 0;
car.angularAcceleration = 0;
2013-04-18 13:16:18 +00:00
if (myGame2.input.keyboard.isDown(Phaser.Keyboard.LEFT))
2013-04-12 16:19:56 +00:00
{
car.angularVelocity = -200;
}
2013-04-18 13:16:18 +00:00
else if (myGame2.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
2013-04-12 16:19:56 +00:00
{
car.angularVelocity = 200;
}
2013-04-18 13:16:18 +00:00
if (myGame2.input.keyboard.isDown(Phaser.Keyboard.UP))
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
car.velocity.copyFrom(myGame.motion.velocityFromAngle(car.angle, 300));
2013-04-12 16:19:56 +00:00
}
}
})();