Skip to content

Commit ca93203

Browse files
pixelpicoseanphotonstorm
authored andcommitted
Finish document for ScrollRegion, ScrollZone, Tilemap, Device, TilemapLayer.
1 parent 00ef200 commit ca93203

5 files changed

Lines changed: 576 additions & 174 deletions

File tree

Phaser/gameobjects/ScrollRegion.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ module Phaser {
1212

1313
export class ScrollRegion{
1414

15+
/**
16+
* ScrollRegion constructor
17+
* Create a new <code>ScrollRegion</code>.
18+
*
19+
* @param x {number} X position in world coordinate.
20+
* @param y {number} Y position in world coordinate.
21+
* @param width {number} Width of this object.
22+
* @param height {number} Height of this object.
23+
* @param speedX {number} X-axis scrolling speed.
24+
* @param speedY {number} Y-axis scrolling speed.
25+
*/
1526
constructor(x: number, y: number, width: number, height: number, speedX:number, speedY:number) {
1627

1728
// Our seamless scrolling quads
@@ -38,9 +49,21 @@ module Phaser {
3849
private _inverseWidth: number = 0;
3950
private _inverseHeight: number = 0;
4051

52+
/**
53+
* Will this region be rendered? (default to true)
54+
* @type {boolean}
55+
*/
4156
public visible: bool = true;
57+
/**
58+
* Region scrolling speed.
59+
* @type {MicroPoint}
60+
*/
4261
public scrollSpeed: MicroPoint;
4362

63+
/**
64+
* Update region scrolling with tick time.
65+
* @param delta {number} Elapsed time since last update.
66+
*/
4467
public update(delta: number) {
4568

4669
this._scroll.x += this.scrollSpeed.x;
@@ -102,6 +125,15 @@ module Phaser {
102125

103126
}
104127

128+
/**
129+
* Render this region to specific context.
130+
* @param context {CanvasRenderingContext2D} Canvas context this region will be rendered to.
131+
* @param texture {object} The texture to be rendered.
132+
* @param dx {number} X position in world coordinate.
133+
* @param dy {number} Y position in world coordinate.
134+
* @param width {number} Width of this region to be rendered.
135+
* @param height {number} Height of this region to be rendered.
136+
*/
105137
public render(context:CanvasRenderingContext2D, texture, dx: number, dy: number, dw: number, dh: number) {
106138

107139
if (this.visible == false)
@@ -126,6 +158,21 @@ module Phaser {
126158

127159
}
128160

161+
/**
162+
* Crop part of the texture and render it to the given context.
163+
* @param context {CanvasRenderingContext2D} Canvas context the texture will be rendered to.
164+
* @param texture {object} Texture to be rendered.
165+
* @param srcX {number} Target region top-left x coordinate in the texture.
166+
* @param srcX {number} Target region top-left y coordinate in the texture.
167+
* @param srcW {number} Target region width in the texture.
168+
* @param srcH {number} Target region height in the texture.
169+
* @param destX {number} Render region top-left x coordinate in the context.
170+
* @param destX {number} Render region top-left y coordinate in the context.
171+
* @param destW {number} Target region width in the context.
172+
* @param destH {number} Target region height in the context.
173+
* @param offsetX {number} X offset to the context.
174+
* @param offsetY {number} Y offset to the context.
175+
*/
129176
private crop(context, texture, srcX, srcY, srcW, srcH, destX, destY, destW, destH, offsetX, offsetY) {
130177

131178
offsetX += destX;

Phaser/gameobjects/ScrollZone.ts

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ module Phaser {
1515

1616
export class ScrollZone extends GameObject {
1717

18+
/**
19+
* ScrollZone constructor
20+
* Create a new <code>ScrollZone</code>.
21+
*
22+
* @param game {Phaser.Game} Current game instance.
23+
* @param key {string} Asset key for image texture of this object.
24+
* @param x {number} X position in world coordinate.
25+
* @param y {number} Y position in world coordinate.
26+
* @param width {number} Optional, width of this object.
27+
* @param height {number} Optional, height of this object.
28+
*/
1829
constructor(game: Game, key:string, x: number = 0, y: number = 0, width?: number = 0, height?: number = 0) {
1930

2031
super(game, x, y, width, height);
@@ -49,19 +60,63 @@ module Phaser {
4960

5061
}
5162

63+
/**
64+
* Texture of this object.
65+
*/
5266
private _texture;
67+
/**
68+
* If this zone is larger than texture image, this will be filled with a pattern of texture.
69+
* @type {DynamicTexture}
70+
*/
5371
private _dynamicTexture: DynamicTexture = null;
5472

55-
// local rendering related temp vars to help avoid gc spikes
73+
/**
74+
* Local rendering related temp vars to help avoid gc spikes.
75+
* @type {number}
76+
*/
5677
private _dx: number = 0;
78+
/**
79+
* Local rendering related temp vars to help avoid gc spikes.
80+
* @type {number}
81+
*/
5782
private _dy: number = 0;
83+
/**
84+
* Local rendering related temp vars to help avoid gc spikes.
85+
* @type {number}
86+
*/
5887
private _dw: number = 0;
88+
/**
89+
* Local rendering related temp vars to help avoid gc spikes.
90+
* @type {number}
91+
*/
5992
private _dh: number = 0;
6093

94+
/**
95+
* Current region this zone is scrolling.
96+
* @type {ScrollRegion}
97+
*/
6198
public currentRegion: ScrollRegion;
99+
/**
100+
* Array contains all added regions.
101+
* @type {ScrollRegion[]}
102+
*/
62103
public regions: ScrollRegion[];
104+
/**
105+
* Flip this zone vertically? (default to false)
106+
* @type {boolean}
107+
*/
63108
public flipped: bool = false;
64109

110+
/**
111+
* Add a new region to this zone.
112+
* @param x {number} X position of the new region.
113+
* @param y {number} Y position of the new region.
114+
* @param width {number} Width of the new region.
115+
* @param height {number} Height of the new region.
116+
* @param speedX {number} Optional, x-axis scrolling speed.
117+
* @param speedY {number} Optional, y-axis scrolling speed.
118+
* @return {ScrollRegion} The newly added region.
119+
*/
65120
public addRegion(x: number, y: number, width: number, height: number, speedX?:number = 0, speedY?:number = 0):ScrollRegion {
66121

67122
if (x > this.width || y > this.height || x < 0 || y < 0 || (x + width) > this.width || (y + height) > this.height)
@@ -78,6 +133,11 @@ module Phaser {
78133

79134
}
80135

136+
/**
137+
* Set scrolling speed of current region.
138+
* @param x {number} X speed of current region.
139+
* @param y {number} Y speed of current region.
140+
*/
81141
public setSpeed(x: number, y: number) {
82142

83143
if (this.currentRegion)
@@ -89,6 +149,9 @@ module Phaser {
89149

90150
}
91151

152+
/**
153+
* Update regions.
154+
*/
92155
public update() {
93156

94157
for (var i = 0; i < this.regions.length; i++)
@@ -98,8 +161,13 @@ module Phaser {
98161

99162
}
100163

164+
/**
165+
* Check whether this zone is visible in a specific camera rectangle.
166+
* @param camera {Rectangle} The rectangle you want to check.
167+
* @return {boolean} Return true if bound of this zone intersects the given rectangle, otherwise return false.
168+
*/
101169
public inCamera(camera: Rectangle): bool {
102-
170+
103171
if (this.scrollFactor.x !== 1.0 || this.scrollFactor.y !== 1.0)
104172
{
105173
this._dx = this.bounds.x - (camera.x * this.scrollFactor.x);
@@ -116,6 +184,13 @@ module Phaser {
116184

117185
}
118186

187+
/**
188+
* Render this zone object to a specific camera.
189+
* @param camera {Camera} The camera this object will be render to.
190+
* @param cameraOffsetX {number} X offset of camera.
191+
* @param cameraOffsetY {number} Y offset of camera.
192+
* @return Return false if not rendered, otherwise return true.
193+
*/
119194
public render(camera: Camera, cameraOffsetX: number, cameraOffsetY: number) {
120195

121196
// Render checks
@@ -189,6 +264,10 @@ module Phaser {
189264

190265
}
191266

267+
/**
268+
* Create repeating texture with _texture, and store it into the _dynamicTexture.
269+
* Used to create texture when texture image is small than size of the zone.
270+
*/
192271
private createRepeatingTexture(regionWidth: number, regionHeight: number) {
193272

194273
// Work out how many we'll need of the source image to make it tile properly

0 commit comments

Comments
 (0)