Skip to content

Commit 30fc409

Browse files
committed
Out of bounds and Sprite events hooked up
1 parent ee007cd commit 30fc409

5 files changed

Lines changed: 91 additions & 7 deletions

File tree

examples/outofbounds.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.image('alien', 'assets/sprites/space-baddie.png');
19+
game.load.image('ship', 'assets/sprites/shmup-ship.png');
20+
}
21+
22+
var player;
23+
var aliens;
24+
25+
function create() {
26+
27+
player = game.add.sprite(400, 500, 'ship');
28+
player.anchor.setTo(0.5, 0.5);
29+
30+
aliens = game.add.group();
31+
32+
for (var y = 0; y < 4; y++)
33+
{
34+
for (var x = 0; x < 10; x++)
35+
{
36+
var alien = aliens.create(x * 48, y * 50, 'alien');
37+
alien.name = 'alien' + x.toString() + y.toString();
38+
alien.events.onOutOfBounds.add(alienOut, this);
39+
alien.body.velocity.y = 50 + Math.random() * 150;
40+
}
41+
}
42+
43+
}
44+
45+
function alienOut(alien) {
46+
47+
alien.reset(alien.x, -32);
48+
49+
}
50+
51+
function update() {
52+
}
53+
54+
function render() {
55+
}
56+
57+
})();
58+
59+
</script>
60+
61+
</body>
62+
</html>

src/core/Group.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Phaser.Group.prototype = {
3232
if (child.group !== this)
3333
{
3434
child.group = this;
35+
child.events.onAddedToGroup.dispatch(child, this);
3536
this._container.addChild(child);
3637
}
3738

@@ -44,6 +45,7 @@ Phaser.Group.prototype = {
4445
if (child.group !== this)
4546
{
4647
child.group = this;
48+
child.events.onAddedToGroup.dispatch(child, this);
4749
this._container.addChildAt(child, index);
4850
}
4951

@@ -61,6 +63,7 @@ Phaser.Group.prototype = {
6163

6264
var child = new Phaser.Sprite(this.game, x, y, key, frame);
6365
child.group = this;
66+
child.events.onAddedToGroup.dispatch(child, this);
6467
this._container.addChild(child);
6568
return child;
6669

@@ -186,11 +189,13 @@ Phaser.Group.prototype = {
186189
{
187190
if (newChild.parent != undefined)
188191
{
192+
newChild.events.onRemovedFromGroup.dispatch(newChild, this);
189193
newChild.parent.removeChild(newChild);
190194
}
191195

192196
this._container.removeChild(oldChild);
193197
this._container.addChildAt(newChild, index);
198+
newChild.events.onAddedToGroup.dispatch(newChild, this);
194199
}
195200

196201
},
@@ -600,6 +605,7 @@ Phaser.Group.prototype = {
600605

601606
remove: function (child) {
602607

608+
child.events.onRemovedFromGroup.dispatch(child, this);
603609
this._container.removeChild(child);
604610

605611
},
@@ -608,6 +614,7 @@ Phaser.Group.prototype = {
608614

609615
do
610616
{
617+
this._container.children[0].events.onRemovedFromGroup.dispatch(this._container.children[0], this);
611618
this._container.removeChild(this._container.children[0]);
612619
}
613620
while (this._container.children.length > 0);
@@ -624,6 +631,7 @@ Phaser.Group.prototype = {
624631
for (var i = startIndex; i < endIndex; i++)
625632
{
626633
var child = this._container.children[i];
634+
child.events.onRemovedFromGroup.dispatch(child, this);
627635
this._container.removeChild(child);
628636
}
629637

src/gameobjects/Sprite.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ Phaser.Sprite = function (game, x, y, key, frame) {
142142
// Set-up the physics body
143143
this.body = new Phaser.Physics.Arcade.Body(this);
144144

145+
this._outOfBoundsFired = false;
146+
145147
};
146148

147149
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly)
@@ -270,9 +272,14 @@ Phaser.Sprite.prototype.update = function() {
270272

271273
}
272274

273-
Phaser.Sprite.prototype.postUpdate = function() {
275+
Phaser.Sprite.prototype.reset = function(x, y) {
274276

275-
this.body.postUpdate();
277+
this.x = x;
278+
this.y = y;
279+
this.alive = true;
280+
this.exists = true;
281+
this.visible = true;
282+
this._outOfBoundsFired = false;
276283

277284
}
278285

@@ -299,6 +306,13 @@ Phaser.Sprite.prototype.updateBounds = function() {
299306
this._cache.boundsX = this._cache.x;
300307
this._cache.boundsY = this._cache.y;
301308

309+
// Check world bounds
310+
if (this._outOfBoundsFired == false && Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, 100) == false)
311+
{
312+
this.events.onOutOfBounds.dispatch(this);
313+
this._outOfBoundsFired = true;
314+
}
315+
302316
}
303317

304318
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {

src/input/Input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ Phaser.Input.prototype = {
381381
}
382382

383383
this.currentPointers = 0;
384-
// this.game.stage.canvas.style.cursor = "default";
384+
this.game.stage.canvas.style.cursor = "default";
385385

386386
if (hard == true)
387387
{

src/input/Pointer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ Phaser.Pointer.prototype = {
240240

241241
if (this.game.input.multiInputOverride == Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers == 0))
242242
{
243-
//this.game.input.x = this.x * this.game.input.scale.x;
244-
//this.game.input.y = this.y * this.game.input.scale.y;
245-
this.game.input.x = this.x;
246-
this.game.input.y = this.y;
243+
this.game.input.x = this.x * this.game.input.scale.x;
244+
this.game.input.y = this.y * this.game.input.scale.y;
245+
// this.game.input.x = this.x;
246+
// this.game.input.y = this.y;
247247
this.game.input.position.setTo(this.x, this.y);
248248
this.game.input.onDown.dispatch(this);
249249
this.game.input.resetSpeed(this.x, this.y);

0 commit comments

Comments
 (0)