Skip to content

Commit a30762e

Browse files
committed
Fixed world drag support and other input modifications.
1 parent 7305948 commit a30762e

37 files changed

Lines changed: 1078 additions & 435 deletions

Phaser/Game.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ module Phaser {
416416
this.onUpdateCallback.call(this.callbackContext);
417417
}
418418

419+
this.world.postUpdate();
420+
419421
this.renderer.render();
420422

421423
if (this._loadComplete && this.onRenderCallback)

Phaser/World.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ module Phaser {
9696

9797
}
9898

99+
/**
100+
* This is called automatically every frame, and is where main logic happens.
101+
*/
102+
public postUpdate() {
103+
104+
//this.physics.postUpdate();
105+
this.group.postUpdate();
106+
this.cameras.postUpdate();
107+
108+
}
109+
99110
/**
100111
* Clean up memory.
101112
*/

Phaser/cameras/Camera.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,6 @@ module Phaser {
291291
{
292292
this.modified = true;
293293
}
294-
else if (this.modified == true && this.transform.scale.equals(1) && this.transform.skew.equals(0) && this.transform.rotation == 0 && this.transform.rotationOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false)
295-
{
296-
this.modified = false;
297-
}
298294

299295
this.fx.preUpdate();
300296

@@ -365,6 +361,42 @@ module Phaser {
365361
}
366362
}
367363

364+
}
365+
366+
/**
367+
* Update focusing and scrolling.
368+
*/
369+
public postUpdate() {
370+
371+
if (this.modified == true && this.transform.scale.equals(1) && this.transform.skew.equals(0) && this.transform.rotation == 0 && this.transform.rotationOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false)
372+
{
373+
this.modified = false;
374+
}
375+
376+
// Make sure we didn't go outside the cameras worldBounds
377+
if (this.worldBounds !== null)
378+
{
379+
if (this.worldView.x < this.worldBounds.left)
380+
{
381+
this.worldView.x = this.worldBounds.left;
382+
}
383+
384+
if (this.worldView.x > this.worldBounds.right - this.width)
385+
{
386+
this.worldView.x = (this.worldBounds.right - this.width) + 1;
387+
}
388+
389+
if (this.worldView.y < this.worldBounds.top)
390+
{
391+
this.worldView.y = this.worldBounds.top;
392+
}
393+
394+
if (this.worldView.y > this.worldBounds.bottom - this.height)
395+
{
396+
this.worldView.y = (this.worldBounds.bottom - this.height) + 1;
397+
}
398+
}
399+
368400
this.fx.postUpdate();
369401

370402
}
@@ -380,11 +412,12 @@ module Phaser {
380412
this.game.stage.context.fillStyle = color;
381413
this.game.stage.context.fillText('Camera ID: ' + this.ID + ' (' + this.screenView.width + ' x ' + this.screenView.height + ')', x, y);
382414
this.game.stage.context.fillText('X: ' + this.screenView.x + ' Y: ' + this.screenView.y + ' rotation: ' + this.transform.rotation, x, y + 14);
383-
this.game.stage.context.fillText('World X: ' + this.worldView.x.toFixed(1) + ' World Y: ' + this.worldView.y.toFixed(1), x, y + 28);
415+
this.game.stage.context.fillText('World X: ' + this.worldView.x + ' World Y: ' + this.worldView.y + ' W: ' + this.worldView.width + ' H: ' + this.worldView.height + ' R: ' + this.worldView.right + ' B: ' + this.worldView.bottom, x, y + 28);
416+
this.game.stage.context.fillText('ScreenView X: ' + this.screenView.x + ' Y: ' + this.screenView.y + ' W: ' + this.screenView.width + ' H: ' + this.screenView.height, x, y + 42);
384417

385418
if (this.worldBounds)
386419
{
387-
this.game.stage.context.fillText('Bounds: ' + this.worldBounds.width + ' x ' + this.worldBounds.height, x, y + 42);
420+
this.game.stage.context.fillText('Bounds: ' + this.worldBounds.width + ' x ' + this.worldBounds.height, x, y + 56);
388421
}
389422

390423
}

Phaser/cameras/CameraManager.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,24 @@ module Phaser {
8383
* Update cameras.
8484
*/
8585
public update() {
86-
this._cameras.forEach((camera) => camera.update());
86+
87+
for (var i = 0; i < this._cameras.length; i++)
88+
{
89+
this._cameras[i].update();
90+
}
91+
92+
}
93+
94+
/**
95+
* postUpdate cameras.
96+
*/
97+
public postUpdate() {
98+
99+
for (var i = 0; i < this._cameras.length; i++)
100+
{
101+
this._cameras[i].postUpdate();
102+
}
103+
87104
}
88105

89106
/**

Phaser/components/sprite/Input.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ module Phaser.Components.Sprite {
258258
}
259259
else
260260
{
261-
return RectangleUtils.contains(this.sprite.worldView, pointer.scaledX, pointer.scaledY);
261+
return RectangleUtils.contains(this.sprite.worldView, pointer.worldX(), pointer.worldY());
262262
}
263263

264264
}
@@ -279,10 +279,10 @@ module Phaser.Components.Sprite {
279279
}
280280
else if (this._pointerData[pointer.id].isOver == true)
281281
{
282-
if (RectangleUtils.contains(this.sprite.worldView, pointer.scaledX, pointer.scaledY))
282+
if (RectangleUtils.contains(this.sprite.worldView, pointer.worldX(), pointer.worldY()))
283283
{
284-
this._pointerData[pointer.id].x = pointer.scaledX - this.sprite.x;
285-
this._pointerData[pointer.id].y = pointer.scaledY - this.sprite.y;
284+
this._pointerData[pointer.id].x = pointer.x - this.sprite.x;
285+
this._pointerData[pointer.id].y = pointer.y - this.sprite.y;
286286
return true;
287287
}
288288
else

Phaser/core/Group.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,28 @@ module Phaser {
186186
{
187187
this.modified = true;
188188
}
189-
else if (this.modified == true && this.transform.scale.equals(1) && this.transform.skew.equals(0) && this.transform.rotation == 0 && this.transform.rotationOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false)
189+
190+
this._i = 0;
191+
192+
while (this._i < this.length)
193+
{
194+
this._member = this.members[this._i++];
195+
196+
if (this._member != null && this._member.exists && this._member.active)
197+
{
198+
this._member.preUpdate();
199+
this._member.update();
200+
}
201+
}
202+
}
203+
204+
/**
205+
* Calls update on all members of this Group who have a status of active=true and exists=true
206+
* You can also call Object.postUpdate directly, which will bypass the active/exists check.
207+
*/
208+
public postUpdate() {
209+
210+
if (this.modified == true && this.transform.scale.equals(1) && this.transform.skew.equals(0) && this.transform.rotation == 0 && this.transform.rotationOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false)
190211
{
191212
this.modified = false;
192213
}
@@ -199,8 +220,6 @@ module Phaser {
199220

200221
if (this._member != null && this._member.exists && this._member.active)
201222
{
202-
this._member.preUpdate();
203-
this._member.update();
204223
this._member.postUpdate();
205224
}
206225
}
@@ -398,10 +417,10 @@ module Phaser {
398417
* @param y {number} Y position of the new sprite.
399418
* @param [key] {string} The image key as defined in the Game.Cache to use as the texture for this sprite
400419
* @param [frame] {string|number} If the sprite uses an image from a texture atlas or sprite sheet you can pass the frame here. Either a number for a frame ID or a string for a frame name.
401-
* @param [bodyType] {number} The physics body type of the object (defaults to BODY_DISABLED)
420+
* @param [bodyType] {number} The physics body type of the object (defaults to BODY_DYNAMIC)
402421
* @returns {Sprite} The newly created sprite object.
403422
*/
404-
public addNewSprite(x: number, y: number, key?: string = '', frame? = null, bodyType?: number = Phaser.Types.BODY_DISABLED): Sprite {
423+
public addNewSprite(x: number, y: number, key?: string = '', frame? = null, bodyType?: number = Phaser.Types.BODY_DYNAMIC): Sprite {
405424
return <Sprite> this.add(new Sprite(this.game, x, y, key, frame, bodyType));
406425
}
407426

Phaser/gameobjects/GameObjectFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module Phaser {
7272
* @param [bodyType] {number} The physics body type of the object (defaults to BODY_DISABLED)
7373
* @returns {Sprite} The newly created sprite object.
7474
*/
75-
public sprite(x: number, y: number, key?: string = '', frame? = null, bodyType?: number = Phaser.Types.BODY_DISABLED): Sprite {
75+
public sprite(x: number, y: number, key?: string = '', frame? = null, bodyType?: number = Phaser.Types.BODY_DYNAMIC): Sprite {
7676
return <Sprite> this._world.group.add(new Sprite(this._game, x, y, key, frame, bodyType));
7777
}
7878

Phaser/gameobjects/Sprite.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ module Phaser {
2323
* @param [x] {number} the initial x position of the sprite.
2424
* @param [y] {number} the initial y position of the sprite.
2525
* @param [key] {string} Key of the graphic you want to load for this sprite.
26-
* @param [bodyType] {number} The physics body type of the object (defaults to BODY_DISABLED)
26+
* @param [bodyType] {number} The physics body type of the object (defaults to BODY_DYNAMIC)
2727
*/
28-
constructor(game: Game, x?: number = 0, y?: number = 0, key?: string = null, frame? = null, bodyType?: number = Phaser.Types.BODY_DISABLED) {
28+
constructor(game: Game, x?: number = 0, y?: number = 0, key?: string = null, frame? = null, bodyType?: number = Phaser.Types.BODY_DYNAMIC) {
2929

3030
this.game = game;
3131
this.type = Phaser.Types.SPRITE;
@@ -43,7 +43,6 @@ module Phaser {
4343
this.transform = new Phaser.Components.Transform(this);
4444
this.animations = new Phaser.Components.AnimationManager(this);
4545
this.texture = new Phaser.Components.Texture(this);
46-
this.body = new Phaser.Physics.Body(this, bodyType);
4746
this.input = new Phaser.Components.Sprite.Input(this);
4847
this.events = new Phaser.Components.Sprite.Events(this);
4948

@@ -68,6 +67,7 @@ module Phaser {
6867
}
6968
}
7069

70+
this.body = new Phaser.Physics.Body(this, bodyType);
7171
this.worldView = new Rectangle(x, y, this.width, this.height);
7272

7373
}
@@ -233,8 +233,10 @@ module Phaser {
233233
*/
234234
public preUpdate() {
235235

236-
this.worldView.x = this.x - (this.game.world.cameras.default.worldView.x * this.transform.scrollFactor.x);
237-
this.worldView.y = this.y - (this.game.world.cameras.default.worldView.y * this.transform.scrollFactor.y);
236+
//this.worldView.x = this.x * this.transform.scrollFactor.x;
237+
//this.worldView.y = this.y * this.transform.scrollFactor.y;
238+
this.worldView.x = this.x - this.transform.origin.x;
239+
this.worldView.y = this.y - this.transform.origin.y;
238240
this.worldView.width = this.width;
239241
this.worldView.height = this.height;
240242

0 commit comments

Comments
 (0)