Out of bounds and Sprite events hooked up

This commit is contained in:
Richard Davey 2013-09-09 13:29:33 +01:00
parent ee007cd28e
commit 30fc4099c6
5 changed files with 91 additions and 7 deletions

62
examples/outofbounds.php Normal file
View file

@ -0,0 +1,62 @@
<!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('alien', 'assets/sprites/space-baddie.png');
game.load.image('ship', 'assets/sprites/shmup-ship.png');
}
var player;
var aliens;
function create() {
player = game.add.sprite(400, 500, 'ship');
player.anchor.setTo(0.5, 0.5);
aliens = game.add.group();
for (var y = 0; y < 4; y++)
{
for (var x = 0; x < 10; x++)
{
var alien = aliens.create(x * 48, y * 50, 'alien');
alien.name = 'alien' + x.toString() + y.toString();
alien.events.onOutOfBounds.add(alienOut, this);
alien.body.velocity.y = 50 + Math.random() * 150;
}
}
}
function alienOut(alien) {
alien.reset(alien.x, -32);
}
function update() {
}
function render() {
}
})();
</script>
</body>
</html>

View file

@ -32,6 +32,7 @@ Phaser.Group.prototype = {
if (child.group !== this) if (child.group !== this)
{ {
child.group = this; child.group = this;
child.events.onAddedToGroup.dispatch(child, this);
this._container.addChild(child); this._container.addChild(child);
} }
@ -44,6 +45,7 @@ Phaser.Group.prototype = {
if (child.group !== this) if (child.group !== this)
{ {
child.group = this; child.group = this;
child.events.onAddedToGroup.dispatch(child, this);
this._container.addChildAt(child, index); this._container.addChildAt(child, index);
} }
@ -61,6 +63,7 @@ Phaser.Group.prototype = {
var child = new Phaser.Sprite(this.game, x, y, key, frame); var child = new Phaser.Sprite(this.game, x, y, key, frame);
child.group = this; child.group = this;
child.events.onAddedToGroup.dispatch(child, this);
this._container.addChild(child); this._container.addChild(child);
return child; return child;
@ -186,11 +189,13 @@ Phaser.Group.prototype = {
{ {
if (newChild.parent != undefined) if (newChild.parent != undefined)
{ {
newChild.events.onRemovedFromGroup.dispatch(newChild, this);
newChild.parent.removeChild(newChild); newChild.parent.removeChild(newChild);
} }
this._container.removeChild(oldChild); this._container.removeChild(oldChild);
this._container.addChildAt(newChild, index); this._container.addChildAt(newChild, index);
newChild.events.onAddedToGroup.dispatch(newChild, this);
} }
}, },
@ -600,6 +605,7 @@ Phaser.Group.prototype = {
remove: function (child) { remove: function (child) {
child.events.onRemovedFromGroup.dispatch(child, this);
this._container.removeChild(child); this._container.removeChild(child);
}, },
@ -608,6 +614,7 @@ Phaser.Group.prototype = {
do do
{ {
this._container.children[0].events.onRemovedFromGroup.dispatch(this._container.children[0], this);
this._container.removeChild(this._container.children[0]); this._container.removeChild(this._container.children[0]);
} }
while (this._container.children.length > 0); while (this._container.children.length > 0);
@ -624,6 +631,7 @@ Phaser.Group.prototype = {
for (var i = startIndex; i < endIndex; i++) for (var i = startIndex; i < endIndex; i++)
{ {
var child = this._container.children[i]; var child = this._container.children[i];
child.events.onRemovedFromGroup.dispatch(child, this);
this._container.removeChild(child); this._container.removeChild(child);
} }

View file

@ -142,6 +142,8 @@ Phaser.Sprite = function (game, x, y, key, frame) {
// Set-up the physics body // Set-up the physics body
this.body = new Phaser.Physics.Arcade.Body(this); this.body = new Phaser.Physics.Arcade.Body(this);
this._outOfBoundsFired = false;
}; };
// Needed to keep the PIXI.Sprite constructor in the prototype chain (as the core pixi renderer uses an instanceof check sadly) // 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() {
} }
Phaser.Sprite.prototype.postUpdate = function() { Phaser.Sprite.prototype.reset = function(x, y) {
this.body.postUpdate(); this.x = x;
this.y = y;
this.alive = true;
this.exists = true;
this.visible = true;
this._outOfBoundsFired = false;
} }
@ -299,6 +306,13 @@ Phaser.Sprite.prototype.updateBounds = function() {
this._cache.boundsX = this._cache.x; this._cache.boundsX = this._cache.x;
this._cache.boundsY = this._cache.y; this._cache.boundsY = this._cache.y;
// Check world bounds
if (this._outOfBoundsFired == false && Phaser.Rectangle.intersects(this.bounds, this.game.world.bounds, 100) == false)
{
this.events.onOutOfBounds.dispatch(this);
this._outOfBoundsFired = true;
}
} }
Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) { Phaser.Sprite.prototype.getLocalPosition = function(p, x, y) {

View file

@ -381,7 +381,7 @@ Phaser.Input.prototype = {
} }
this.currentPointers = 0; this.currentPointers = 0;
// this.game.stage.canvas.style.cursor = "default"; this.game.stage.canvas.style.cursor = "default";
if (hard == true) if (hard == true)
{ {

View file

@ -240,10 +240,10 @@ Phaser.Pointer.prototype = {
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)) 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))
{ {
//this.game.input.x = this.x * this.game.input.scale.x; this.game.input.x = this.x * this.game.input.scale.x;
//this.game.input.y = this.y * this.game.input.scale.y; this.game.input.y = this.y * this.game.input.scale.y;
this.game.input.x = this.x; // this.game.input.x = this.x;
this.game.input.y = this.y; // this.game.input.y = this.y;
this.game.input.position.setTo(this.x, this.y); this.game.input.position.setTo(this.x, this.y);
this.game.input.onDown.dispatch(this); this.game.input.onDown.dispatch(this);
this.game.input.resetSpeed(this.x, this.y); this.game.input.resetSpeed(this.x, this.y);