Skip to content

Commit 57796a6

Browse files
committed
TileSprites can now receive full Input events, dragging, etc and be positioned in-world and fixed to cameras (fixes phaserjs#321)
1 parent ee1a1cd commit 57796a6

4 files changed

Lines changed: 125 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ New features:
123123
* fixedToCamrea now works for Groups as well :) You can fix a Group to the camera and it will influence its children.
124124
* Tilemap.createCollisionObjects will parse Tiled data for objectgroups and convert polyline instances into physics objects you can collide with in the world.
125125
* Loader can now load JSON files specifically (game.load.json) and they are parsed and stored in the Game.Cache. Retrieve with game.cache.getJSON(key).
126+
* TileSprites can now receive full Input events, dragging, etc and be positioned in-world and fixed to cameras.
126127

127128

128129
Updates:

examples/wip/tilesprite input.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('backdrop', 'assets/pics/remember-me.jpg');
7+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
8+
game.load.image('starfield', 'assets/misc/starfield.jpg');
9+
10+
}
11+
12+
var sprite;
13+
var cursors;
14+
var mushroom;
15+
16+
function create() {
17+
18+
game.world.setBounds(0, 0, 1920, 1200);
19+
game.add.image(0, 0, 'backdrop');
20+
21+
sprite = game.add.tileSprite(100, 100, 200, 200, 'starfield');
22+
sprite.inputEnabled = true;
23+
sprite.events.onInputDown.add(scroll, this);
24+
sprite.input.enableDrag();
25+
26+
mushroom = game.add.sprite(400, 400, 'mushroom');
27+
mushroom.inputEnabled = true;
28+
mushroom.input.enableDrag();
29+
30+
game.camera.follow(mushroom);
31+
32+
cursors = game.input.keyboard.createCursorKeys();
33+
34+
}
35+
36+
function scroll()
37+
{
38+
sprite.autoScroll(0, 100);
39+
}
40+
41+
function update() {
42+
43+
if (cursors.left.isDown)
44+
{
45+
mushroom.x -= 8;
46+
}
47+
else if (cursors.right.isDown)
48+
{
49+
mushroom.x += 8;
50+
}
51+
52+
if (cursors.up.isDown)
53+
{
54+
mushroom.y -= 8;
55+
}
56+
else if (cursors.down.isDown)
57+
{
58+
mushroom.y += 8;
59+
}
60+
61+
}
62+
63+
function render() {
64+
65+
// game.debug.renderText(sprite.frame, 32, 32);
66+
67+
}

src/gameobjects/TileSprite.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ Phaser.TileSprite = function (game, x, y, width, height, key, frame) {
8383

8484
this.position.set(x, y);
8585

86+
/**
87+
* @property {Phaser.InputHandler|null} input - The Input Handler for this object. Needs to be enabled with image.inputEnabled = true before you can use it.
88+
*/
89+
this.input = null;
90+
8691
/**
8792
* @property {Phaser.Point} world - The world coordinates of this Sprite. This differs from the x/y coordinates which are relative to the Sprites container.
8893
*/
@@ -418,3 +423,39 @@ Object.defineProperty(Phaser.TileSprite.prototype, "fixedToCamera", {
418423
}
419424

420425
});
426+
427+
/**
428+
* By default a TileSprite won't process any input events at all. By setting inputEnabled to true the Phaser.InputHandler is
429+
* activated for this object and it will then start to process click/touch events and more.
430+
*
431+
* @name Phaser.TileSprite#inputEnabled
432+
* @property {boolean} inputEnabled - Set to true to allow this object to receive input events.
433+
*/
434+
Object.defineProperty(Phaser.TileSprite.prototype, "inputEnabled", {
435+
436+
get: function () {
437+
438+
return (this.input && this.input.enabled);
439+
440+
},
441+
442+
set: function (value) {
443+
444+
if (value)
445+
{
446+
if (this.input === null)
447+
{
448+
this.input = new Phaser.InputHandler(this);
449+
this.input.start();
450+
}
451+
}
452+
else
453+
{
454+
if (this.input && this.input.enabled)
455+
{
456+
this.input.stop();
457+
}
458+
}
459+
}
460+
461+
});

src/input/Input.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,22 @@ Phaser.Input.prototype = {
730730

731731
return false;
732732
}
733+
else if (displayObject instanceof Phaser.TileSprite)
734+
{
735+
var width = displayObject.width;
736+
var height = displayObject.height;
737+
var x1 = -width * displayObject.anchor.x;
738+
739+
if (this._localPoint.x > x1 && this._localPoint.x < x1 + width)
740+
{
741+
var y1 = -height * displayObject.anchor.y;
742+
743+
if (this._localPoint.y > y1 && this._localPoint.y < y1 + height)
744+
{
745+
return true;
746+
}
747+
}
748+
}
733749
else if (displayObject instanceof PIXI.Sprite)
734750
{
735751
var width = displayObject.texture.frame.width;

0 commit comments

Comments
 (0)