Skip to content

Commit 936118b

Browse files
committed
AnimationManager done and in, need to fix texture update bug, otherwise finished.
1 parent a81a8ef commit 936118b

9 files changed

Lines changed: 778 additions & 3 deletions

File tree

examples/js.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
<script src="../src/core/Stage.js"></script>
5252
<script src="../src/core/World.js"></script>
5353
<script src="../src/core/Game.js"></script>
54+
<script src="../src/gameobjects/GameObjectFactory.js"></script>
55+
<script src="../src/gameobjects/Sprite.js"></script>
5456
<script src="../src/system/Canvas.js"></script>
5557
<script src="../src/system/Device.js"></script>
5658
<script src="../src/system/RequestAnimationFrame.js"></script>
@@ -64,6 +66,7 @@
6466
<script src="../src/tween/Tween.js"></script>
6567
<script src="../src/tween/Easing.js"></script>
6668
<script src="../src/time/Time.js"></script>
69+
<script src="../src/animation/AnimationManager.js"></script>
6770
<script src="../src/animation/Animation.js"></script>
6871
<script src="../src/animation/Frame.js"></script>
6972
<script src="../src/animation/FrameData.js"></script>

examples/sprite1.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
16+
17+
var bunny;
18+
19+
function preload() {
20+
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
21+
}
22+
23+
function create() {
24+
25+
var test = new Phaser.Sprite(game, 0, 0, 'mushroom');
26+
27+
test.x = 200;
28+
29+
game.world.add(test);
30+
31+
console.log(test.alpha);
32+
33+
}
34+
35+
function update() {
36+
}
37+
38+
})();
39+
40+
</script>
41+
42+
</body>
43+
</html>

examples/sprite2.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
</head>
9+
<body>
10+
11+
<script type="text/javascript">
12+
13+
(function () {
14+
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
16+
17+
var bunny;
18+
19+
function preload() {
20+
game.load.spritesheet('ms', 'assets/sprites/metalslug_mummy37x45.png', 37, 45);
21+
}
22+
23+
function create() {
24+
25+
bunny = new Phaser.Sprite(game, 0, 0, 'ms', 10);
26+
27+
game.world.add(bunny);
28+
29+
}
30+
31+
function update() {
32+
bunny.postUpdate();
33+
}
34+
35+
})();
36+
37+
</script>
38+
39+
</body>
40+
</html>

src/animation/Animation.js

Lines changed: 206 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,207 @@
1-
Phaser.Animation = {
1+
/**
2+
* Animation
3+
*
4+
* An Animation instance contains a single animation and the controls to play it.
5+
* It is created by the AnimationManager and belongs to Game Objects such as Sprite.
6+
*
7+
* @package Phaser.Animation
8+
* @author Richard Davey <rich@photonstorm.com>
9+
* @copyright 2013 Photon Storm Ltd.
10+
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
11+
*
12+
* @param parent {Sprite} Owner sprite of this animation.
13+
* @param frameData {FrameData} The FrameData object contains animation data.
14+
* @param name {string} Unique name of this animation.
15+
* @param frames {number[]/string[]} An array of numbers or strings indicating what frames to play in what order.
16+
* @param delay {number} Time between frames in ms.
17+
* @param looped {bool} Whether or not the animation is looped or just plays once.
18+
*/
19+
Phaser.Animation = function (game, parent, frameData, name, frames, delay, looped) {
20+
21+
this.game = game;
22+
this._parent = parent;
23+
this._frames = frames;
24+
this._frameData = frameData;
25+
this.name = name;
26+
this.delay = 1000 / delay;
27+
this.looped = looped;
28+
this.isFinished = false;
29+
this.isPlaying = false;
30+
this._frameIndex = 0;
31+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
232

3-
};
33+
};
34+
35+
Phaser.Animation.prototype = {
36+
37+
/**
38+
* Play this animation.
39+
* @param frameRate {number} FrameRate you want to specify instead of using default.
40+
* @param loop {bool} Whether or not the animation is looped or just plays once.
41+
*/
42+
play: function (frameRate, loop) {
43+
44+
if (typeof frameRate === "undefined") { frameRate = null; }
45+
if (typeof loop === "undefined") { loop = null; }
46+
47+
if (frameRate !== null)
48+
{
49+
this.delay = 1000 / frameRate;
50+
}
51+
52+
if (loop !== null)
53+
{
54+
// If they set a new loop value then use it, otherwise use the default set on creation
55+
this.looped = loop;
56+
}
57+
58+
this.isPlaying = true;
59+
this.isFinished = false;
60+
61+
this._timeLastFrame = this.game.time.now;
62+
this._timeNextFrame = this.game.time.now + this.delay;
63+
64+
this._frameIndex = 0;
65+
66+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
67+
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
68+
// this._parent.events.onAnimationStart.dispatch(this._parent, this);
69+
70+
return this;
71+
72+
},
73+
74+
/**
75+
* Play this animation from the first frame.
76+
*/
77+
restart: function () {
78+
79+
this.isPlaying = true;
80+
this.isFinished = false;
81+
82+
this._timeLastFrame = this.game.time.now;
83+
this._timeNextFrame = this.game.time.now + this.delay;
84+
85+
this._frameIndex = 0;
86+
87+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
88+
89+
},
90+
91+
/**
92+
* Stop playing animation and set it finished.
93+
*/
94+
stop: function () {
95+
96+
this.isPlaying = false;
97+
this.isFinished = true;
98+
99+
},
100+
101+
/**
102+
* Update animation frames.
103+
*/
104+
update: function () {
105+
106+
if (this.isPlaying == true && this.game.time.now >= this._timeNextFrame)
107+
{
108+
this._frameIndex++;
109+
110+
if (this._frameIndex == this._frames.length)
111+
{
112+
if (this.looped)
113+
{
114+
this._frameIndex = 0;
115+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
116+
// this._parent.events.onAnimationLoop.dispatch(this._parent, this);
117+
}
118+
else
119+
{
120+
this.onComplete();
121+
}
122+
}
123+
else
124+
{
125+
this.currentFrame = this._frameData.getFrame(this._frames[this._frameIndex]);
126+
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
127+
}
128+
129+
this._timeLastFrame = this.game.time.now;
130+
this._timeNextFrame = this.game.time.now + this.delay;
131+
132+
return true;
133+
}
134+
135+
return false;
136+
137+
},
138+
139+
/**
140+
* Clean up animation memory.
141+
*/
142+
destroy: function () {
143+
144+
this.game = null;
145+
this._parent = null;
146+
this._frames = null;
147+
this._frameData = null;
148+
this.currentFrame = null;
149+
this.isPlaying = false;
150+
151+
},
152+
153+
/**
154+
* Animation complete callback method.
155+
*/
156+
onComplete: function () {
157+
158+
this.isPlaying = false;
159+
this.isFinished = true;
160+
// this._parent.events.onAnimationComplete.dispatch(this._parent, this);
161+
162+
}
163+
164+
};
165+
166+
Object.defineProperty(Phaser.Animation.prototype, "frameTotal", {
167+
168+
get: function () {
169+
return this._frames.length;
170+
},
171+
172+
enumerable: true,
173+
configurable: true
174+
175+
});
176+
177+
Object.defineProperty(Phaser.Animation.prototype, "frame", {
178+
179+
get: function () {
180+
181+
if (this.currentFrame !== null)
182+
{
183+
return this.currentFrame.index;
184+
}
185+
else
186+
{
187+
return this._frameIndex;
188+
}
189+
190+
},
191+
192+
set: function (value) {
193+
194+
this.currentFrame = this._frameData.getFrame(value);
195+
196+
if (this.currentFrame !== null)
197+
{
198+
this._frameIndex = value;
199+
this._parent.setTexture(PIXI.TextureCache[this.currentFrame.uuid]);
200+
}
201+
202+
},
203+
204+
enumerable: true,
205+
configurable: true
206+
207+
});

0 commit comments

Comments
 (0)