mirror of
https://github.com/wassname/phaser.git
synced 2026-08-12 12:20:38 +08:00
Group vs. Group collision working - also fixed a bug in the Body.reset function that was causing some lovely physics errors :)
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
<!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.AUTO, '', { preload: preload, create: create, update: update });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
||||
game.load.image('bullet', 'assets/misc/bullet0.png');
|
||||
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);
|
||||
|
||||
}
|
||||
|
||||
var sprite;
|
||||
var bullets;
|
||||
var veggies;
|
||||
var bulletTime = 0;
|
||||
|
||||
var bullet;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
// This will check Group vs. Group collision (bullets vs. veggies!)
|
||||
|
||||
veggies = game.add.group();
|
||||
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var c = veggies.create(game.world.randomX, Math.random() * 500, 'veggies', game.rnd.integerInRange(0, 36));
|
||||
c.name = 'veg' + i;
|
||||
c.body.immovable = true;
|
||||
}
|
||||
|
||||
bullets = game.add.group();
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
var b = bullets.create(0, 0, 'bullet');
|
||||
b.name = 'bullet' + i;
|
||||
b.exists = false;
|
||||
b.visible = false;
|
||||
b.events.onOutOfBounds.add(resetBullet, this);
|
||||
}
|
||||
|
||||
sprite = game.add.sprite(400, 550, 'phaser');
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
sprite.velocity.x = 0;
|
||||
sprite.velocity.y = 0;
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
|
||||
{
|
||||
sprite.velocity.x = -200;
|
||||
}
|
||||
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
|
||||
{
|
||||
sprite.velocity.x = 200;
|
||||
}
|
||||
|
||||
if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))
|
||||
{
|
||||
fireBullet();
|
||||
}
|
||||
|
||||
game.physics.collide(bullets, veggies, collisionHandler, null, this);
|
||||
|
||||
}
|
||||
|
||||
function fireBullet () {
|
||||
|
||||
if (game.time.now > bulletTime)
|
||||
{
|
||||
bullet = bullets.getFirstExists(false);
|
||||
|
||||
if (bullet)
|
||||
{
|
||||
bullet.reset(sprite.x + 6, sprite.y - 8);
|
||||
bullet.velocity.y = -300;
|
||||
bulletTime = game.time.now + 250;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Called if the bullet goes out of the screen
|
||||
function resetBullet (bullet) {
|
||||
bullet.kill();
|
||||
}
|
||||
|
||||
function collisionHandler (bullet, veg) {
|
||||
|
||||
bullet.kill();
|
||||
veg.kill();
|
||||
|
||||
}
|
||||
|
||||
})();
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -338,7 +338,7 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
|
||||
|
||||
if (this.width > 1 || this.height > 1)
|
||||
{
|
||||
particle.reset(this.x - this.game.rnd.integerInRange(this.left, this.right), this.y - this.game.rnd.integerInRange(this.top, this.bottom));
|
||||
particle.reset(this.emiteX - this.game.rnd.integerInRange(this.left, this.right), this.emiteY - this.game.rnd.integerInRange(this.top, this.bottom));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -205,18 +205,16 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
reset: function () {
|
||||
|
||||
this.velocity = new Phaser.Point;
|
||||
this.acceleration = new Phaser.Point;
|
||||
this.drag = new Phaser.Point;
|
||||
this.gravity = new Phaser.Point;
|
||||
this.bounce = new Phaser.Point;
|
||||
this.maxVelocity = new Phaser.Point(10000, 10000);
|
||||
this.velocity.setTo(0, 0);
|
||||
this.acceleration.setTo(0, 0);
|
||||
|
||||
this.angularVelocity = 0;
|
||||
this.angularAcceleration = 0;
|
||||
this.angularDrag = 0;
|
||||
this.maxAngular = 1000;
|
||||
this.mass = 1;
|
||||
|
||||
this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
|
||||
this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
|
||||
this.lastX = this.x;
|
||||
this.lastY = this.y;
|
||||
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user