Skip to content

Commit 0a42269

Browse files
committed
Group vs. Group collision working - also fixed a bug in the Body.reset function that was causing some lovely physics errors :)
1 parent ecc91fb commit 0a42269

3 files changed

Lines changed: 125 additions & 10 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
16+
17+
function preload() {
18+
19+
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
20+
game.load.image('bullet', 'assets/misc/bullet0.png');
21+
game.load.spritesheet('veggies', 'assets/sprites/fruitnveg32wh37.png', 32, 32);
22+
23+
}
24+
25+
var sprite;
26+
var bullets;
27+
var veggies;
28+
var bulletTime = 0;
29+
30+
var bullet;
31+
32+
function create() {
33+
34+
game.stage.backgroundColor = '#2d2d2d';
35+
36+
// This will check Group vs. Group collision (bullets vs. veggies!)
37+
38+
veggies = game.add.group();
39+
40+
for (var i = 0; i < 50; i++)
41+
{
42+
var c = veggies.create(game.world.randomX, Math.random() * 500, 'veggies', game.rnd.integerInRange(0, 36));
43+
c.name = 'veg' + i;
44+
c.body.immovable = true;
45+
}
46+
47+
bullets = game.add.group();
48+
49+
for (var i = 0; i < 10; i++)
50+
{
51+
var b = bullets.create(0, 0, 'bullet');
52+
b.name = 'bullet' + i;
53+
b.exists = false;
54+
b.visible = false;
55+
b.events.onOutOfBounds.add(resetBullet, this);
56+
}
57+
58+
sprite = game.add.sprite(400, 550, 'phaser');
59+
60+
}
61+
62+
function update() {
63+
64+
sprite.velocity.x = 0;
65+
sprite.velocity.y = 0;
66+
67+
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
68+
{
69+
sprite.velocity.x = -200;
70+
}
71+
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
72+
{
73+
sprite.velocity.x = 200;
74+
}
75+
76+
if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))
77+
{
78+
fireBullet();
79+
}
80+
81+
game.physics.collide(bullets, veggies, collisionHandler, null, this);
82+
83+
}
84+
85+
function fireBullet () {
86+
87+
if (game.time.now > bulletTime)
88+
{
89+
bullet = bullets.getFirstExists(false);
90+
91+
if (bullet)
92+
{
93+
bullet.reset(sprite.x + 6, sprite.y - 8);
94+
bullet.velocity.y = -300;
95+
bulletTime = game.time.now + 250;
96+
}
97+
}
98+
99+
}
100+
101+
// Called if the bullet goes out of the screen
102+
function resetBullet (bullet) {
103+
bullet.kill();
104+
}
105+
106+
function collisionHandler (bullet, veg) {
107+
108+
bullet.kill();
109+
veg.kill();
110+
111+
}
112+
113+
})();
114+
</script>
115+
116+
</body>
117+
</html>

src/particles/arcade/Emitter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
338338

339339
if (this.width > 1 || this.height > 1)
340340
{
341-
particle.reset(this.x - this.game.rnd.integerInRange(this.left, this.right), this.y - this.game.rnd.integerInRange(this.top, this.bottom));
341+
particle.reset(this.emiteX - this.game.rnd.integerInRange(this.left, this.right), this.emiteY - this.game.rnd.integerInRange(this.top, this.bottom));
342342
}
343343
else
344344
{

src/physics/arcade/Body.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,16 @@ Phaser.Physics.Arcade.Body.prototype = {
205205

206206
reset: function () {
207207

208-
this.velocity = new Phaser.Point;
209-
this.acceleration = new Phaser.Point;
210-
this.drag = new Phaser.Point;
211-
this.gravity = new Phaser.Point;
212-
this.bounce = new Phaser.Point;
213-
this.maxVelocity = new Phaser.Point(10000, 10000);
208+
this.velocity.setTo(0, 0);
209+
this.acceleration.setTo(0, 0);
214210

215211
this.angularVelocity = 0;
216212
this.angularAcceleration = 0;
217-
this.angularDrag = 0;
218-
this.maxAngular = 1000;
219-
this.mass = 1;
213+
214+
this.x = (this.sprite.x - (this.sprite.anchor.x * this.width)) + this.offset.x;
215+
this.y = (this.sprite.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
216+
this.lastX = this.x;
217+
this.lastY = this.y;
220218

221219
},
222220

0 commit comments

Comments
 (0)