2013-09-04 02:48:15 +00:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>phaser.js - a new beginning</title>
|
|
|
|
<?php
|
|
|
|
require('js.php');
|
|
|
|
?>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
|
|
|
|
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
|
|
|
|
|
|
|
|
function preload() {
|
|
|
|
game.load.image('ship', 'assets/sprites/xenon2_ship.png');
|
|
|
|
game.load.image('baddie', 'assets/sprites/space-baddie.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
var ship;
|
2013-09-04 03:54:43 +00:00
|
|
|
var total;
|
|
|
|
var aliens;
|
2013-09-04 02:48:15 +00:00
|
|
|
|
|
|
|
function create() {
|
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
game.world.setSize(2000, 2000);
|
|
|
|
|
2013-09-04 03:54:43 +00:00
|
|
|
aliens = [];
|
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
for (var i = 0; i < 1000; i++)
|
2013-09-04 02:48:15 +00:00
|
|
|
{
|
|
|
|
var s = game.add.sprite(game.world.randomX, game.world.randomY, 'baddie');
|
2013-09-04 12:54:55 +00:00
|
|
|
s.name = 'alien' + s;
|
2013-09-04 02:48:15 +00:00
|
|
|
s.body.collideWorldBounds = true;
|
|
|
|
s.body.bounce.setTo(1, 1);
|
2013-09-04 03:54:43 +00:00
|
|
|
s.body.velocity.setTo(10 + Math.random() * 10, 10 + Math.random() * 10);
|
|
|
|
aliens.push(s);
|
2013-09-04 02:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ship = game.add.sprite(400, 400, 'ship');
|
|
|
|
ship.body.collideWorldBounds = true;
|
|
|
|
ship.body.bounce.setTo(0.5, 0.5);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
|
|
|
|
2013-09-04 03:54:43 +00:00
|
|
|
for (var i = 0; i < aliens.length; i++)
|
|
|
|
{
|
|
|
|
aliens[i].alpha = 0.3;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = game.physics.quadTree.retrieve(ship);
|
|
|
|
|
2013-09-04 12:54:55 +00:00
|
|
|
// Get the ships top-most ID. If the length of that ID is 1 then we can ignore every other result,
|
|
|
|
// it's simply not colliding with anything :)
|
|
|
|
|
2013-09-04 03:54:43 +00:00
|
|
|
for (var i = 0; i < total.length; i++)
|
|
|
|
{
|
|
|
|
total[i].sprite.alpha = 1;
|
|
|
|
}
|
|
|
|
|
2013-09-04 02:48:15 +00:00
|
|
|
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
|
|
|
|
{
|
|
|
|
ship.body.velocity.x -= 2;
|
|
|
|
}
|
|
|
|
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
|
|
|
|
{
|
|
|
|
ship.body.velocity.x += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
|
|
|
|
{
|
|
|
|
ship.body.velocity.y -= 2;
|
|
|
|
}
|
|
|
|
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
|
|
|
|
{
|
|
|
|
ship.body.velocity.y += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
|
2013-09-04 03:54:43 +00:00
|
|
|
for (var i = 0; i < aliens.length; i++)
|
|
|
|
{
|
2013-09-04 12:54:55 +00:00
|
|
|
// game.debug.renderRectangle(aliens[i].bounds);
|
2013-09-04 03:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
game.debug.renderText(total.length, 32, 32);
|
|
|
|
game.debug.renderQuadTree(game.physics.quadTree);
|
2013-09-04 12:54:55 +00:00
|
|
|
// game.debug.renderRectangle(ship);
|
|
|
|
|
|
|
|
game.debug.renderText('Index: ' + ship.body.quadTreeIndex, 32, 80);
|
|
|
|
|
|
|
|
for (var i = 0; i < ship.body.quadTreeIDs.length; i++)
|
|
|
|
{
|
|
|
|
game.debug.renderText('ID: ' + ship.body.quadTreeIDs[i], 32, 100 + (i * 20));
|
|
|
|
}
|
2013-09-04 02:48:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|