Skip to content

Commit 7b4374c

Browse files
committed
Added loop and yoyo properties to Tweens
1 parent b1d836d commit 7b4374c

30 files changed

Lines changed: 1611 additions & 690 deletions

Phaser/components/animation/AnimationManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* sprite specific animations.
1313
*/
1414

15-
module Phaser {
15+
module Phaser.Components {
1616

1717
export class AnimationManager {
1818

@@ -22,10 +22,10 @@ module Phaser {
2222
*
2323
* @param parent {Sprite} Owner sprite of this manager.
2424
*/
25-
constructor(game: Game, parent: Sprite) {
25+
constructor(parent: Sprite) {
2626

27-
this._game = game;
2827
this._parent = parent;
28+
this._game = parent.game;
2929
this._anims = {};
3030

3131
}

Phaser/components/sprite/Texture.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ module Phaser.Components {
1212

1313
export class Texture {
1414

15-
constructor(parent: Sprite, key?: string = '', canvas?: HTMLCanvasElement = null, context?: CanvasRenderingContext2D = null) {
15+
constructor(parent: Sprite, key?: string = '') {
1616

1717
this._game = parent.game;
1818
this._sprite = parent;
1919

20-
this.canvas = canvas;
21-
this.context = context;
20+
this.canvas = parent.game.stage.canvas;
21+
this.context = parent.game.stage.context;
2222
this.alpha = 1;
2323
this.flippedX = false;
2424
this.flippedY = false;
@@ -137,27 +137,23 @@ module Phaser.Components {
137137
*/
138138
public loadImage(key: string, clearAnimations: bool = true) {
139139

140-
//if (clearAnimations && sprite.animations.frameData !== null)
141-
//{
142-
// sprite.animations.destroy();
143-
//}
140+
if (clearAnimations && this._sprite.animations.frameData !== null)
141+
{
142+
this._sprite.animations.destroy();
143+
}
144144

145145
if (this._game.cache.getImage(key) !== null)
146146
{
147147
this.setTo(this._game.cache.getImage(key), null);
148148

149149
if (this._game.cache.isSpriteSheet(key))
150150
{
151-
//sprite.animations.loadFrameData(sprite._game.cache.getFrameData(key));
152-
//sprite.collisionMask.width = sprite.animations.currentFrame.width;
153-
//sprite.collisionMask.height = sprite.animations.currentFrame.height;
151+
this._sprite.animations.loadFrameData(this._sprite.game.cache.getFrameData(key));
154152
}
155153
else
156154
{
157155
this._sprite.frameBounds.width = this.width;
158156
this._sprite.frameBounds.height = this.height;
159-
//sprite.collisionMask.width = sprite._texture.width;
160-
//sprite.collisionMask.height = sprite._texture.height;
161157
}
162158
}
163159

@@ -170,10 +166,10 @@ module Phaser.Components {
170166
*/
171167
public loadDynamicTexture(texture: DynamicTexture) {
172168

173-
//if (sprite.animations.frameData !== null)
174-
//{
175-
// sprite.animations.destroy();
176-
//}
169+
if (this._sprite.animations.frameData !== null)
170+
{
171+
this._sprite.animations.destroy();
172+
}
177173

178174
this.setTo(null, texture);
179175
this._sprite.frameBounds.width = this.width;

Phaser/core/Vec2.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ module Phaser {
230230

231231
}
232232

233+
/**
234+
* Check if both the x and y of this vector equal the given value.
235+
*
236+
* @return {Boolean}
237+
*/
238+
public equals(value): bool {
239+
return (this.x == value && this.y == value);
240+
}
241+
233242
/**
234243
* Returns a string representation of this object.
235244
* @method toString

Phaser/gameobjects/Sprite.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path="../Game.ts" />
2+
/// <reference path="../core/Vec2.ts" />
23
/// <reference path="../core/Rectangle.ts" />
34
/// <reference path="../components/animation/AnimationManager.ts" />
45
/// <reference path="../components/sprite/Texture.ts" />
@@ -34,19 +35,23 @@ module Phaser {
3435
this.alive = true;
3536

3637
this.frameBounds = new Rectangle(x, y, width, height);
37-
this.origin = new Phaser.Vec2(0, 0);
3838
this.scrollFactor = new Phaser.Vec2(1, 1);
39-
this.scale = new Phaser.Vec2(1, 1);
4039

4140
this.x = x;
4241
this.y = y;
43-
this.z = 0;
42+
this.z = 0; // not used yet
4443

45-
this.texture = new Phaser.Components.Texture(this, key, game.stage.canvas, game.stage.context);
44+
this.animations = new Phaser.Components.AnimationManager(this);
45+
this.texture = new Phaser.Components.Texture(this, key);
4646

4747
this.width = this.frameBounds.width;
4848
this.height = this.frameBounds.height;
4949

50+
// Transform related (if we add any more then move to a component)
51+
this.origin = new Phaser.Vec2(this.width / 2, this.height / 2);
52+
this.scale = new Phaser.Vec2(1, 1);
53+
this.skew = new Phaser.Vec2(0, 0);
54+
5055
}
5156

5257
/**
@@ -99,6 +104,12 @@ module Phaser {
99104
*/
100105
public texture: Phaser.Components.Texture;
101106

107+
/**
108+
* This manages animations of the sprite. You can modify animations though it. (see AnimationManager)
109+
* @type AnimationManager
110+
*/
111+
public animations: Phaser.Components.AnimationManager;
112+
102113
/**
103114
* The frame boundary around this Sprite.
104115
* For non-animated sprites this matches the loaded texture dimensions.
@@ -111,6 +122,16 @@ module Phaser {
111122
*/
112123
public scale: Phaser.Vec2;
113124

125+
/**
126+
* Skew the Sprite along the x and y axis. A skew value of 0 is no skew.
127+
*/
128+
public skew: Phaser.Vec2;
129+
130+
/**
131+
* A boolean representing if the Sprite has been modified in any way via a scale, rotate, flip or skew.
132+
*/
133+
public modified: bool = false;
134+
114135
/**
115136
* The influence of camera movement upon the Sprite.
116137
*/
@@ -159,6 +180,34 @@ module Phaser {
159180
this._rotation = this.game.math.wrap(value, 360, 0);
160181
}
161182

183+
/**
184+
* Set the animation frame by frame number.
185+
*/
186+
public set frame(value: number) {
187+
this.animations.frame = value;
188+
}
189+
190+
/**
191+
* Get the animation frame number.
192+
*/
193+
public get frame(): number {
194+
return this.animations.frame;
195+
}
196+
197+
/**
198+
* Set the animation frame by frame name.
199+
*/
200+
public set frameName(value: string) {
201+
this.animations.frameName = value;
202+
}
203+
204+
/**
205+
* Get the animation frame name.
206+
*/
207+
public get frameName(): string {
208+
return this.animations.frameName;
209+
}
210+
162211
/**
163212
* Pre-update is called right before update() on each object in the game loop.
164213
*/
@@ -169,6 +218,11 @@ module Phaser {
169218

170219
//this.collisionMask.preUpdate();
171220

221+
if (this.modified == false && (!this.scale.equals(1) || !this.skew.equals(0) || this.rotation != 0 || this.rotationOffset != 0 || this.texture.flippedX || this.texture.flippedY))
222+
{
223+
this.modified = true;
224+
}
225+
172226
}
173227

174228
/**
@@ -182,9 +236,9 @@ module Phaser {
182236
*/
183237
public postUpdate() {
184238

185-
/*
186239
this.animations.update();
187240

241+
/*
188242
if (this.moves)
189243
{
190244
this.updateMotion();
@@ -232,6 +286,11 @@ module Phaser {
232286
this.touching = Collision.NONE;
233287
*/
234288

289+
if (this.modified == true && this.scale.equals(1) && this.skew.equals(0) && this.rotation == 0 && this.rotationOffset == 0 && this.texture.flippedX == false && this.texture.flippedY == false)
290+
{
291+
this.modified = false;
292+
}
293+
235294
}
236295

237296
/**

Phaser/renderers/CanvasRenderer.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,20 @@ module Phaser {
103103
this._dw = sprite.frameBounds.width;
104104
this._dh = sprite.frameBounds.height;
105105

106-
/*
107-
if (this._dynamicTexture == false && this.animations.currentFrame !== null)
106+
if (sprite.animations.currentFrame !== null)
108107
{
109-
this._sx = this.animations.currentFrame.x;
110-
this._sy = this.animations.currentFrame.y;
108+
this._sx = sprite.animations.currentFrame.x;
109+
this._sy = sprite.animations.currentFrame.y;
111110

112-
if (this.animations.currentFrame.trimmed)
111+
if (sprite.animations.currentFrame.trimmed)
113112
{
114-
this._dx += this.animations.currentFrame.spriteSourceSizeX;
115-
this._dy += this.animations.currentFrame.spriteSourceSizeY;
113+
this._dx += sprite.animations.currentFrame.spriteSourceSizeX;
114+
this._dy += sprite.animations.currentFrame.spriteSourceSizeY;
116115
}
117116
}
118117

119118
// Apply camera difference - looks like this is already applied?
119+
/*
120120
if (sprite.scrollFactor.x !== 1 || sprite.scrollFactor.y !== 1)
121121
{
122122
//this._dx -= (camera.worldView.x * this.scrollFactor.x);
@@ -125,7 +125,7 @@ module Phaser {
125125
*/
126126

127127
// Rotation and Flipped
128-
if (sprite.scale.x != 1 || sprite.scale.y != 1 || sprite.rotation != 0 || sprite.rotationOffset != 0 || sprite.texture.flippedX || sprite.texture.flippedY)
128+
if (sprite.modified)
129129
{
130130
if (sprite.texture.renderRotation == true && (sprite.rotation !== 0 || sprite.rotationOffset !== 0))
131131
{
@@ -142,15 +142,17 @@ module Phaser {
142142
// f = translate y
143143

144144
sprite.texture.context.save();
145-
sprite.texture.context.setTransform(this._cos * this._fx, this._sin * this._fx, -this._sin * this._fy, this._cos * this._fy, this._dx, this._dy);
145+
sprite.texture.context.setTransform(this._cos * this._fx, (this._sin * this._fx) + sprite.skew.x, -(this._sin * this._fy) + sprite.skew.y, this._cos * this._fy, this._dx, this._dy);
146146

147147
this._dx = -sprite.origin.x;
148148
this._dy = -sprite.origin.y;
149149
}
150150
else
151151
{
152-
this._dw = sprite.frameBounds.width * sprite.scale.x;
153-
this._dh = sprite.frameBounds.height * sprite.scale.y;
152+
this._dx -= sprite.origin.x;
153+
this._dy -= sprite.origin.y;
154+
//this._dw = sprite.frameBounds.width * sprite.scale.x;
155+
//this._dh = sprite.frameBounds.height * sprite.scale.y;
154156
}
155157

156158
this._sx = Math.round(this._sx);
@@ -183,7 +185,7 @@ module Phaser {
183185
sprite.texture.context.fillRect(this._dx, this._dy, this._dw, this._dh);
184186
}
185187

186-
if (sprite.scale.x != 1 || sprite.scale.y != 1 || sprite.rotation != 0 || sprite.rotationOffset != 0 || sprite.texture.flippedX || sprite.texture.flippedY)
188+
if (sprite.modified)
187189
{
188190
sprite.texture.context.restore();
189191
}

0 commit comments

Comments
 (0)