Skip to content

Commit 2fe8a3a

Browse files
committed
Physics integration and a fix to Tween that stopped the repeat/yoyo from working.
1 parent bdc1c2c commit 2fe8a3a

9 files changed

Lines changed: 331 additions & 91 deletions

File tree

examples/body1.php

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,41 @@
1616

1717
function preload() {
1818
game.load.atlasJSONHash('bot', 'assets/sprites/running_bot.png', 'assets/sprites/running_bot.json');
19+
game.load.image('bunny', 'assets/sprites/bunny.png');
1920
}
2021

21-
var s;
22+
var bunny;
23+
var bot;
2224

2325
function create() {
2426

2527
game.world._stage.backgroundColorString = '#182d3b';
2628

27-
s = game.add.sprite(game.world.centerX, game.world.centerY, 'bot');
28-
// s.anchor.setTo(0.5, 0.5);
29+
bunny = game.add.sprite(500, game.world.centerY, 'bunny');
30+
bunny.anchor.setTo(0.5, 0.5);
2931

30-
// s.body.offset.setTo(0, 0);
32+
bot = game.add.sprite(150, game.world.centerY, 'bot');
33+
bot.anchor.setTo(0.5, 0.5);
34+
bot.animations.add('run');
35+
bot.animations.play('run', 10, true);
3136

32-
33-
// s.scale.setTo(2, 2);
34-
35-
s.animations.add('run');
36-
s.animations.play('run', 10, true);
37+
game.add.tween(bot.scale).to({ x: 3, y: 3 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
3738

3839
}
3940

4041
function update() {
4142

42-
s.rotation += 0.01;
43-
44-
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
45-
{
46-
s.x -= 4;
47-
}
48-
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
49-
{
50-
s.x += 4;
51-
}
52-
53-
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
54-
{
55-
s.y -= 4;
56-
}
57-
else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
58-
{
59-
s.y += 4;
60-
}
43+
bunny.angle += 1;
44+
bot.angle += 1;
6145

6246
}
6347

6448
function render() {
6549

66-
game.debug.renderSpriteCorners(s, true, true);
67-
// game.debug.renderRectangle(s.body.bounds, 'rgba(255,0,0,0.3)');
68-
game.debug.renderSpriteInfo(s, 20, 32);
50+
game.debug.renderSpriteCorners(bunny, true, true);
51+
game.debug.renderSpriteCorners(bot, true, true);
52+
game.debug.renderSpriteInfo(bunny, 20, 32);
53+
game.debug.renderSpriteInfo(bot, 20, 132);
6954

7055
}
7156

examples/body2.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.atlasJSONHash('bot', 'assets/sprites/running_bot.png', 'assets/sprites/running_bot.json');
19+
game.load.image('bunny', 'assets/sprites/mana_card.png');
20+
}
21+
22+
var bunny;
23+
var bot;
24+
25+
function create() {
26+
27+
game.world._stage.backgroundColorString = '#182d3b';
28+
29+
bunny = game.add.sprite(500, game.world.centerY, 'bunny');
30+
31+
bunny.body.offset.setTo();
32+
33+
bot = game.add.sprite(150, game.world.centerY, 'bot');
34+
bot.anchor.setTo(0.5, 0.5);
35+
bot.animations.add('run');
36+
bot.animations.play('run', 10, true);
37+
38+
game.add.tween(bot.scale).to({ x: 3, y: 3 }, 2000, Phaser.Easing.Linear.None, true, 0, 1000, true);
39+
40+
}
41+
42+
function update() {
43+
44+
bunny.angle += 1;
45+
bot.angle += 1;
46+
47+
}
48+
49+
function render() {
50+
51+
game.debug.renderSpriteCorners(bunny, true, true);
52+
game.debug.renderSpriteCorners(bot, true, true);
53+
54+
game.debug.renderRectangle(bunny.body.hitArea);
55+
56+
}
57+
58+
})();
59+
60+
</script>
61+
62+
</body>
63+
</html>

examples/body3.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.CANVAS, '', { preload: preload, create: create, update: update, render: render });
16+
17+
function preload() {
18+
game.load.atlasJSONHash('bot', 'assets/sprites/running_bot.png', 'assets/sprites/running_bot.json');
19+
game.load.image('bunny', 'assets/sprites/mana_card.png');
20+
}
21+
22+
var bunny;
23+
var bot;
24+
25+
function create() {
26+
27+
game.world._stage.backgroundColorString = '#182d3b';
28+
29+
bunny = game.add.sprite(200, 200, 'bunny');
30+
31+
bunny.body.acceleration.x = 10;
32+
// bunny.body.velocity.y = 20;
33+
34+
}
35+
36+
function update() {
37+
38+
39+
}
40+
41+
function render() {
42+
43+
game.debug.renderSpriteCorners(bunny, true, true);
44+
game.debug.renderRectangle(bunny.body.hitArea);
45+
46+
}
47+
48+
})();
49+
50+
</script>
51+
52+
</body>
53+
</html>

src/core/Game.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
*/
2727
Phaser.Game = function (width, height, renderer, parent, state, transparent, antialias) {
2828

29-
if (typeof width === "undefined") { width = 800; }
30-
if (typeof height === "undefined") { height = 600; }
31-
if (typeof renderer === "undefined") { renderer = Phaser.AUTO; }
32-
if (typeof parent === "undefined") { parent = ''; }
33-
if (typeof state === "undefined") { state = null; }
34-
if (typeof transparent === "undefined") { transparent = false; }
35-
if (typeof antialias === "undefined") { antialias = true; }
29+
width = width || 800;
30+
height = height || 600;
31+
renderer = renderer || Phaser.AUTO;
32+
parent = parent || '';
33+
state = state || null;
34+
transparent = transparent || false;
35+
antialias = antialias || true;
3636

3737
this.id = Phaser.GAMES.push(this) - 1;
3838
this.parent = parent;
@@ -263,7 +263,8 @@ Phaser.Game.prototype = {
263263
*/
264264
boot: function () {
265265

266-
if (this.isBooted) {
266+
if (this.isBooted)
267+
{
267268
return;
268269
}
269270

@@ -296,7 +297,7 @@ Phaser.Game.prototype = {
296297
this.tweens = new Phaser.TweenManager(this);
297298
this.input = new Phaser.Input(this);
298299
this.sound = new Phaser.SoundManager(this);
299-
// this.physics = new Phaser.Physics.PhysicsManager(this);
300+
this.physics = new Phaser.Physics.Arcade(this);
300301
this.plugins = new Phaser.PluginManager(this, this);
301302
this.net = new Phaser.Net(this);
302303
this.debug = new Phaser.Utils.Debug(this);

src/gameobjects/Sprite.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
22

33
x = x || 0;
44
y = y || 0;
5-
// if null we ought to set to the phaser logo or something :)
6-
key = key || null;
5+
key = key || null; // if null we ought to set to the phaser logo or something :)
76
frame = frame || null;
87

98
this.game = game;
@@ -14,7 +13,6 @@ Phaser.Sprite = function (game, x, y, key, frame) {
1413
this.alive = true;
1514

1615
this.group = null;
17-
1816
this.name = '';
1917

2018
if (key)
@@ -53,7 +51,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
5351
}
5452
else
5553
{
56-
this.currentFrame = new Phaser.Animation.Frame(x, y, width, height, '', '');
54+
this.currentFrame = this.game.cache.getFrame(key);
5755
}
5856

5957
/**
@@ -111,13 +109,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
111109
width: this.currentFrame.sourceSizeW, height: this.currentFrame.sourceSizeH,
112110

113111
// The actual width/height of the image if from a trimmed atlas, multiplied by the final calculated scale size
114-
// actualWidth: 0, actualHeight: 0,
115-
116-
// The actual width/height of the image if from a trimmed atlas, multiplied by the final calculated scale size
117-
halfWidth: Math.floor(this.currentFrame.sourceSizeW), halfHeight: Math.floor(this.currentFrame.sourceSizeH),
118-
119-
// The actual width/height of the image if from a trimmed atlas, multiplied by the final calculated scale size
120-
// centerX: 0, centerY: 0,
112+
halfWidth: Math.floor(this.currentFrame.sourceSizeW / 2), halfHeight: Math.floor(this.currentFrame.sourceSizeH / 2),
121113

122114
// The current frame details
123115
frameID: this.currentFrame.uuid, frameWidth: this.currentFrame.width, frameHeight: this.currentFrame.height,
@@ -131,7 +123,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
131123

132124
// Corner point defaults
133125
this.offset = new Phaser.Point;
134-
this.center = new Phaser.Point(Math.floor(this._cache.width / 2), Math.floor(this._cache.height / 2));
126+
this.center = new Phaser.Point(x + Math.floor(this._cache.width / 2), y + Math.floor(this._cache.height / 2));
135127
this.topLeft = new Phaser.Point(x, y);
136128
this.topRight = new Phaser.Point(x + this._cache.width, y);
137129
this.bottomRight = new Phaser.Point(x + this._cache.width, y + this._cache.height);
@@ -182,7 +174,7 @@ Phaser.Sprite.prototype.update = function() {
182174
{
183175
this._cache.a00 = this.worldTransform[0]; // scaleX a
184176
this._cache.a01 = this.worldTransform[1]; // skewY c
185-
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01));
177+
this._cache.scaleX = Math.sqrt((this._cache.a00 * this._cache.a00) + (this._cache.a01 * this._cache.a01)); // round this off a bit?
186178
this._cache.a01 *= -1;
187179
this._cache.dirty = true;
188180
}
@@ -192,7 +184,7 @@ Phaser.Sprite.prototype.update = function() {
192184
{
193185
this._cache.a10 = this.worldTransform[3]; // skewX b
194186
this._cache.a11 = this.worldTransform[4]; // scaleY d
195-
this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11));
187+
this._cache.scaleY = Math.sqrt((this._cache.a10 * this._cache.a10) + (this._cache.a11 * this._cache.a11)); // round this off a bit?
196188
this._cache.a10 *= -1;
197189
this._cache.dirty = true;
198190
}
@@ -209,14 +201,16 @@ Phaser.Sprite.prototype.update = function() {
209201
{
210202
this._cache.frameWidth = this.texture.frame.width;
211203
this._cache.frameHeight = this.texture.frame.height;
204+
this._cache.frameID = this.currentFrame.uuid;
205+
this._cache.dirty = true;
212206
}
213207

214208
if (this._cache.dirty)
215209
{
216-
this._cache.width = this.currentFrame.sourceSizeW * this._cache.scaleX;
217-
this._cache.height = this.currentFrame.sourceSizeH * this._cache.scaleY;
218-
219-
// this.getLocalPosition(this.center, this.x - (this.anchor.x * this._cache.width), this.y - (this.anchor.y * this._cache.height));
210+
this._cache.width = Math.floor(this.currentFrame.sourceSizeW * this._cache.scaleX);
211+
this._cache.height = Math.floor(this.currentFrame.sourceSizeH * this._cache.scaleY);
212+
this._cache.halfWidth = Math.floor(this._cache.width / 2);
213+
this._cache.halfHeight = Math.floor(this._cache.height / 2);
220214

221215
this._cache.id = 1 / (this._cache.a00 * this._cache.a11 + this._cache.a01 * -this._cache.a10);
222216

@@ -249,10 +243,10 @@ Phaser.Sprite.prototype.update = function() {
249243
}
250244

251245
// Update our physics bounds
252-
this.body.update();
246+
this.body.update(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
253247
}
254248

255-
// Check our bounds
249+
this.body.updateMotion();
256250

257251
}
258252

@@ -262,8 +256,6 @@ Phaser.Sprite.prototype.updateBounds = function() {
262256

263257
this.offset.setTo(this._cache.a02 - (this.anchor.x * this._cache.width), this._cache.a12 - (this.anchor.y * this._cache.height));
264258

265-
// this.getLocalPosition(this.center, this.x - (this.anchor.x * this._cache.width), this.y - (this.anchor.y * this._cache.height));
266-
267259
this.getLocalPosition(this.center, this.offset.x + this._cache.halfWidth, this.offset.y + this._cache.halfHeight);
268260
this.getLocalPosition(this.topLeft, this.offset.x, this.offset.y);
269261
this.getLocalPosition(this.topRight, this.offset.x + this._cache.width, this.offset.y);

src/loader/Cache.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Phaser.Cache.prototype = {
115115
addImage: function (key, url, data) {
116116

117117
this._images[key] = { url: url, data: data, spriteSheet: false };
118+
this._images[key].frame = new Phaser.Animation.Frame(0, 0, data.width, data.height, '', '');
118119

119120
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
120121
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
@@ -129,8 +130,8 @@ Phaser.Cache.prototype = {
129130
*/
130131
addSound: function (key, url, data, webAudio, audioTag) {
131132

132-
if (typeof webAudio === "undefined") { webAudio = true; }
133-
if (typeof audioTag === "undefined") { audioTag = false; }
133+
webAudio = webAudio || true;
134+
audioTag = audioTag || false;
134135

135136
var locked = this.game.sound.touchLocked;
136137
var decoded = false;
@@ -246,6 +247,21 @@ Phaser.Cache.prototype = {
246247
return null;
247248
},
248249

250+
/**
251+
* Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.
252+
* @param key Asset key of the frame data you want.
253+
* @return {object} The frame data you want.
254+
*/
255+
getFrame: function (key) {
256+
257+
if (this._images[key] && this._images[key].spriteSheet == false)
258+
{
259+
return this._images[key].frame;
260+
}
261+
262+
return null;
263+
},
264+
249265
/**
250266
* Get sound by key.
251267
* @param key Asset key of the sound you want.

0 commit comments

Comments
 (0)