Skip to content

Commit 06658fa

Browse files
committed
TilemapLayer.resize allows you to resize a TilemapLayer. It will update the internal canvas object and corresponding texture dimensions (phaserjs#1881)
1 parent 8aaa8de commit 06658fa

3 files changed

Lines changed: 29 additions & 33 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ Version 2.4 - "Katar" - in dev
321321
* Text.autoRound allows you to control if the text is allowed to render at sub-pixel coordinates or not. Set to `true` to round the coordinates, often eliminating anti-aliasing from certain font types (#1867)
322322
* Tiled Image Collection support is now available and has been added to the TilemapParser and Tilemap classes (thanks @asyed94 #1879)
323323
* Keyboard.addKeys is a practical way to create an object containing user selected hotkeys. For example: `addKeys( [Phaser.Keyboard.W, Phaser.Keyboard.S, Phaser.Keyboard.A, Phaser.Keyboard.D], [ 'up', 'down', 'left', 'right' ] );` would return an object containing the properties `up`, `down`, `left` and `right` that you could poll just like a Phaser.Key object. (thanks @Mourtz #1857)
324+
* TilemapLayer.resize allows you to resize a TilemapLayer. It will update the internal canvas object and corresponding texture dimensions (#1881)
324325

325326
### Updates
326327

src/tilemap/TilemapLayer.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
4545

4646
/**
4747
* The layer object within the Tilemap that this layer represents.
48-
* @property {Phaser.TileLayer} layer
48+
* @property {object} layer
4949
* @protected
5050
* @readonly
5151
*/
@@ -65,27 +65,6 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
6565
*/
6666
this.context = this.canvas.getContext('2d');
6767

68-
/**
69-
* Required Pixi var.
70-
* @property {PIXI.BaseTexture} baseTexture
71-
* @protected
72-
*/
73-
// this.baseTexture = new PIXI.BaseTexture(this.canvas);
74-
75-
/**
76-
* Required Pixi var.
77-
* @property {PIXI.Texture} texture
78-
* @protected
79-
*/
80-
// this.texture = new PIXI.Texture(this.baseTexture);
81-
82-
/**
83-
* Dimensions of the renderable area.
84-
* @property {Phaser.Frame} textureFrame
85-
* @protected
86-
*/
87-
// this.textureFrame = new Phaser.Frame(0, 0, 0, width, height, 'tilemapLayer');
88-
8968
this.setTexture(new PIXI.Texture(new PIXI.BaseTexture(this.canvas)));
9069

9170
/**
@@ -117,7 +96,7 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
11796
* @default
11897
*/
11998
this.renderSettings = {
120-
enableScrollDelta: true,
99+
enableScrollDelta: false,
121100
overdrawRatio: 0.20,
122101
copyCanvas: null
123102
};
@@ -325,20 +304,38 @@ Phaser.TilemapLayer.prototype.postUpdate = function () {
325304
/**
326305
* Resizes the internal canvas and texture frame used by this TilemapLayer.
327306
*
328-
* This is an expensive call, so don't bind it to a window resize event or similar!
307+
* This is an expensive call, so don't bind it to a window resize event! But instead call it at carefully
308+
* selected times.
309+
*
310+
* Be aware that no validation of the new sizes takes place and the current map scroll coordinates are not
311+
* modified either. You will have to handle both of these things from your game code if required.
329312
*
330313
* @method Phaser.TilemapLayer#resize
331314
* @param {number} width - The new width of the TilemapLayer
332315
* @param {number} height - The new height of the TilemapLayer
333316
*/
334-
Phaser.TilemapLayer.resize = function (width, height) {
317+
Phaser.TilemapLayer.prototype.resize = function (width, height) {
318+
319+
this.canvas.width = width;
320+
this.canvas.height = height;
321+
322+
this.texture.frame.resize(width, height);
335323

336-
// this.baseTexture.width = width;
337-
// this.baseTexture.height = height;
324+
this.texture.width = width;
325+
this.texture.height = height;
338326

339-
this.resizeFrame(width, height);
327+
this.texture.crop.width = width;
328+
this.texture.crop.height = height;
340329

341-
// this.textureFrame.resize(width, height);
330+
this.texture.baseTexture.width = width;
331+
this.texture.baseTexture.height = height;
332+
333+
this.texture.baseTexture.dirty();
334+
this.texture.requiresUpdate = true;
335+
336+
this.texture._updateUvs();
337+
338+
this.dirty = true;
342339

343340
};
344341

typescript/phaser.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="pixi.d.ts" />
22
/// <reference path="p2.d.ts" />
33

4-
// Type definitions for Phaser 2.4.0 2015-Jun-22
4+
// Type definitions for Phaser 2.4.0 2015-Jul-07
55
// Project: https://github.com/photonstorm/phaser
66

77
declare class Phaser {
@@ -4560,7 +4560,6 @@ declare module Phaser {
45604560

45614561
constructor(game: Phaser.Game, tilemap: Phaser.Tilemap, index: number, width?: number, height?: number);
45624562

4563-
baseTexture: PIXI.BaseTexture;
45644563
cameraOffset: Phaser.Point;
45654564
canvas: HTMLCanvasElement;
45664565
collisionHeight: number;
@@ -4586,8 +4585,6 @@ declare module Phaser {
45864585
scrollFactorY: number;
45874586
scrollX: number;
45884587
scrollY: number;
4589-
texture: PIXI.Texture;
4590-
textureFrame: Phaser.Frame;
45914588
type: number;
45924589
wrap: boolean;
45934590

@@ -4598,6 +4595,7 @@ declare module Phaser {
45984595
getTileY(y: number): number;
45994596
postUpdate(): void;
46004597
render(): void;
4598+
resize(width: number, height: number): void;
46014599
resizeWorld(): void;
46024600
resetTilesetCache(): void;
46034601
setScale(xScale?: number, yScale?: number): void;

0 commit comments

Comments
 (0)