phaser/examples/physics/mass velocity test.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-10-22 02:58:20 +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.image('car', 'assets/sprites/car90.png');
game.load.image('baddie', 'assets/sprites/space-baddie.png');
}
var car;
var aliens;
function create() {
aliens = game.add.group();
game.physics.gravity.y = 100;
2013-10-22 02:58:20 +00:00
for (var i = 0; i < 50; i++)
{
var s = aliens.create(game.world.randomX, game.world.randomY, 'baddie');
s.name = 'alien' + s;
s.body.collideWorldBounds = true;
s.body.bounce.setTo(0.8, 0.8);
2014-02-05 05:54:25 +00:00
s.body.linearDamping = 0;
s.body.minVelocity.setTo(0, 0);
2013-10-22 02:58:20 +00:00
s.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40);
}
car = game.add.sprite(400, 300, 'car');
car.name = 'car';
2013-10-22 02:58:20 +00:00
car.anchor.setTo(0.5, 0.5);
car.body.collideWorldBounds = true;
// car.body.bounce.setTo(0.8, 0.8);
2013-10-22 02:58:20 +00:00
car.body.allowRotation = true;
// car.body.immovable = true;
// car.body.minBounceVelocity = 0;
2013-10-22 02:58:20 +00:00
}
function update() {
game.physics.collide(car, aliens);
2013-10-22 02:58:20 +00:00
car.body.velocity.x = 0;
car.body.velocity.y = 0;
car.body.angularVelocity = 0;
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
car.body.angularVelocity = -200;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
car.body.angularVelocity = 200;
}
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
car.body.velocity.copyFrom(game.physics.velocityFromAngle(car.angle, 300));
}
}
function render() {
for (var i = 0; i < aliens._container.children.length; i++)
{
game.debug.renderPolygon(aliens._container.children[i].body.polygons);
}
game.debug.renderPolygon(car.body.polygons);
// game.debug.renderBodyInfo(aliens._container.children[0], 32, 32);
game.debug.renderBodyInfo(aliens._container.children[0], 32, 32);
// game.debug.renderBodyInfo(car, 16, 24);
// game.debug.renderBodyInfo(aliens.getFirstAlive(), 16, 24);
2013-10-22 02:58:20 +00:00
}