Skip to content

Commit 574f4f3

Browse files
committed
Github Issue phaserjs#101 fixed. Plus more Tilemap updates.
1 parent 0201bae commit 574f4f3

4 files changed

Lines changed: 87 additions & 51 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Version 1.0.7 (in progress in the dev branch)
114114
* Added killOnComplete parameter to Animation.play. Really useful in situations where you want a Sprite to animate once then kill itself on complete, like an explosion effect.
115115
* Added Sprite.loadTexture(key, frame) which allows you to load a new texture set into an existing sprite rather than having to create a new sprite.
116116
* Tweens .to will now always return the parent (thanks powerfear)
117+
* You can now pass a PIXI.Texture to Sprite (you also need to pass a Phaser.Frame as the frame parameter) but this is useful for Sprites sharing joint canvases.
118+
* Fixed Issue #101 (Mouse Button 0 is not recognised, thanks rezoner)
117119

118120

119121
* TODO: look at Sprite.crop (http://www.html5gamedevs.com/topic/1617-error-in-spritecrop/)

examples/tilemaps/wip1.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function preload() {
1717

1818
var layer;
1919
var cursors;
20+
var sprite2;
2021

2122
function create() {
2223

@@ -36,16 +37,22 @@ function create() {
3637
'#bcbcbc', '#bcbcbc'
3738
];
3839

39-
map.dump();
40+
// map.dump();
4041

41-
layer = new Phaser.TilemapLayer(game, 200, 200, 320, 200);
42+
// layer = new Phaser.TilemapLayer(game, 0, 0, 640, 400);
43+
layer = new Phaser.TilemapLayer(game, 0, 0, 320, 200);
4244
layer.updateTileset('tiles');
4345
layer.updateMapData(map, 0);
4446

45-
layer.sprite.anchor.setTo(0.5, 0.5);
47+
// layer.sprite.anchor.setTo(0.5, 0.5);
4648

4749
game.world.add(layer.sprite);
4850

51+
// layer.sprite.scale.setTo(2, 2);
52+
53+
game.add.sprite(320, 0, layer.texture, layer.frame);
54+
game.add.sprite(0, 200, layer.texture, layer.frame);
55+
game.add.sprite(320, 200, layer.texture, layer.frame);
4956

5057
cursors = game.input.keyboard.createCursorKeys();
5158
}

src/input/Pointer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Phaser.Pointer.prototype = {
247247
this.identifier = event.identifier;
248248
this.target = event.target;
249249

250-
if (event.button)
250+
if (typeof event.button !== 'undefined')
251251
{
252252
this.button = event.button;
253253
}
@@ -351,7 +351,7 @@ Phaser.Pointer.prototype = {
351351
return;
352352
}
353353

354-
if (event.button)
354+
if (typeof event.button !== 'undefined')
355355
{
356356
this.button = event.button;
357357
}
@@ -430,8 +430,6 @@ Phaser.Pointer.prototype = {
430430

431431
if (this._highestRenderObject == null)
432432
{
433-
// console.log("HRO null");
434-
435433
// The pointer isn't currently over anything, check if we've got a lingering previous target
436434
if (this.targetObject)
437435
{

src/tilemap/TilemapLayer.js

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, mapData,
3030
*/
3131
this.texture = new PIXI.Texture(this.baseTexture);
3232

33-
// index, x, y, width, height, name, uuid
3433
this.frame = new Phaser.Frame(0, 0, 0, renderWidth, renderHeight, 'tilemaplayer', game.rnd.uuid());
3534

3635
/**
@@ -47,8 +46,20 @@ Phaser.TilemapLayer = function (game, x, y, renderWidth, renderHeight, mapData,
4746
this.tileWidth = 0;
4847
this.tileHeight = 0;
4948

50-
this.widthInTiles = 0;
51-
this.heightInTiles = 0;
49+
/**
50+
* Read-only variable, do NOT recommend changing after the map is loaded!
51+
* @property {number} widthInPixels
52+
* @default
53+
*/
54+
this.widthInPixels = 0;
55+
56+
/**
57+
* Read-only variable, do NOT recommend changing after the map is loaded!
58+
* @property {number} heightInPixels
59+
* @default
60+
*/
61+
this.heightInPixels = 0;
62+
5263

5364
this.renderWidth = renderWidth;
5465
this.renderHeight = renderHeight;
@@ -143,15 +154,7 @@ Phaser.TilemapLayer.prototype = {
143154
this.tilemap = tilemap;
144155
this.layer = this.tilemap.layers[layerID];
145156

146-
if (this._maxX > this.layer.width)
147-
{
148-
this._maxX = this.layer.width;
149-
}
150-
151-
if (this._maxY > this.layer.height)
152-
{
153-
this._maxY = this.layer.height;
154-
}
157+
this.updateMax();
155158

156159
},
157160

@@ -161,49 +164,39 @@ Phaser.TilemapLayer.prototype = {
161164
this.tileWidth = this.tileset.tileWidth;
162165
this.tileHeight = this.tileset.tileHeight;
163166

164-
// This don't need to be calculated every frame!
165-
this._maxX = this.game.math.ceil(this.canvas.width / this.tileWidth) + 1;
166-
this._maxY = this.game.math.ceil(this.canvas.height / this.tileHeight) + 1;
167-
167+
this.updateMax();
168168
},
169169

170-
render: function () {
170+
updateMax: function () {
171171

172-
if (this.visible == false || this.dirty == false)
173-
{
174-
return;
175-
}
172+
this._maxX = this.game.math.ceil(this.canvas.width / this.tileWidth) + 1;
173+
this._maxY = this.game.math.ceil(this.canvas.height / this.tileHeight) + 1;
176174

177-
// Work out how many tiles we can fit into our canvas and round it up for the edges
178-
// this._maxX = this.game.math.ceil(this.canvas.width / this.tileWidth) + 1;
179-
// this._maxY = this.game.math.ceil(this.canvas.height / this.tileHeight) + 1;
175+
if (this.layer)
176+
{
177+
if (this._maxX > this.layer.width)
178+
{
179+
this._maxX = this.layer.width;
180+
}
180181

181-
// And now work out where in the tilemap the camera actually is
182-
this._startX = this.game.math.floor(this._x / this.tileWidth);
183-
this._startY = this.game.math.floor(this._y / this.tileHeight);
182+
if (this._maxY > this.layer.height)
183+
{
184+
this._maxY = this.layer.height;
185+
}
184186

185-
// Tilemap bounds check
186-
if (this._startX < 0)
187-
{
188-
this._startX = 0;
187+
this.widthInPixels = this.layer.width * this.tileWidth;
188+
this.heightInPixels = this.layer.height * this.tileHeight;
189189
}
190190

191-
if (this._startY < 0)
192-
{
193-
this._startY = 0;
194-
}
191+
},
195192

196-
if (this._startX + this._maxX > this.layer.width)
197-
{
198-
this._startX = this.layer.width - this._maxX;
199-
}
193+
render: function () {
200194

201-
if (this._startY + this._maxY > this.layer.height)
195+
if (this.visible == false || this.dirty == false)
202196
{
203-
this._startY = this.layer.height - this._maxY;
197+
return;
204198
}
205199

206-
// Finally get the offset to avoid the blocky movement
207200
this._dx = -(this._x - (this._startX * this.tileWidth));
208201
this._dy = -(this._y - (this._startY * this.tileHeight));
209202

@@ -282,9 +275,27 @@ Object.defineProperty(Phaser.TilemapLayer.prototype, "x", {
282275

283276
set: function (value) {
284277

285-
if (value !== this._x)
278+
if (value !== this._x && value >= 0)
286279
{
287280
this._x = value;
281+
282+
if (this._x > (this.widthInPixels - this.renderWidth))
283+
{
284+
this._x = this.widthInPixels - this.renderWidth;
285+
}
286+
287+
this._startX = this.game.math.floor(this._x / this.tileWidth);
288+
289+
if (this._startX < 0)
290+
{
291+
this._startX = 0;
292+
}
293+
294+
if (this._startX + this._maxX > this.layer.width)
295+
{
296+
this._startX = this.layer.width - this._maxX;
297+
}
298+
288299
this.dirty = true;
289300
}
290301

@@ -300,9 +311,27 @@ Object.defineProperty(Phaser.TilemapLayer.prototype, "y", {
300311

301312
set: function (value) {
302313

303-
if (value !== this._y)
314+
if (value !== this._y && value >= 0)
304315
{
305316
this._y = value;
317+
318+
if (this._y > (this.heightInPixels - this.renderHeight))
319+
{
320+
this._y = this.heightInPixels - this.renderHeight;
321+
}
322+
323+
this._startY = this.game.math.floor(this._y / this.tileHeight);
324+
325+
if (this._startY < 0)
326+
{
327+
this._startY = 0;
328+
}
329+
330+
if (this._startY + this._maxY > this.layer.height)
331+
{
332+
this._startY = this.layer.height - this._maxY;
333+
}
334+
306335
this.dirty = true;
307336
}
308337

0 commit comments

Comments
 (0)