Skip to content

Commit f9aa7f7

Browse files
committed
Fixed small bug in Game where AUTO didn't detect the lack of WebGL properly. Tidied up Math a bit more.
1 parent 54d9894 commit f9aa7f7

5 files changed

Lines changed: 106 additions & 28 deletions

File tree

examples/sprite3.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
(function () {
1414

15-
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }, false, false);
1616

1717
var timer = 0;
1818
var total = 0;
@@ -34,6 +34,10 @@ function releaseMummy() {
3434

3535
var mummy = game.add.sprite(-(Math.random() * 800), game.world.randomY, 'mummy');
3636

37+
mummy.scale.setTo(2, 2);
38+
// If you prefer to work in degrees rather than radians then you can use Phaser.Sprite.angle
39+
// otherwise use Phaser.Sprite.rotation
40+
mummy.angle = game.rnd.angle();
3741
mummy.animations.add('walk');
3842
mummy.animations.play('walk', 50, true);
3943

src/core/Game.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ Phaser.Game.prototype = {
303303

304304
setUpRenderer: function () {
305305

306-
if (this.renderType == Phaser.CANVAS || (this.renderer == Phaser.AUTO && this.device.webGL == false))
306+
if (this.renderType == Phaser.CANVAS || (this.renderType == Phaser.AUTO && this.device.webGL == false))
307307
{
308308
if (this.device.canvas)
309309
{

src/gameobjects/Sprite.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,15 @@ Phaser.Sprite = function (game, x, y, key, frame) {
9191
*/
9292
this._height = 0;
9393

94-
// if (texture.baseTexture.hasLoaded)
95-
// {
96-
this.updateFrame = true;
97-
// }
98-
94+
this.updateFrame = true;
9995
this.renderable = true;
10096

10197
this.position.x = x;
10298
this.position.y = y;
10399

100+
// Replaces the PIXI.Point with a slightly more flexible one
101+
this.scale = new Phaser.Point(1, 1);
102+
104103
};
105104

106105
Phaser.Sprite.prototype = Object.create(PIXI.Sprite.prototype);
@@ -116,6 +115,18 @@ Phaser.Sprite.prototype.update = function() {
116115

117116
}
118117

118+
Object.defineProperty(Phaser.Sprite.prototype, 'angle', {
119+
120+
get: function() {
121+
return Phaser.Math.radToDeg(this.rotation);
122+
},
123+
124+
set: function(value) {
125+
this.rotation = Phaser.Math.degToRad(value);
126+
}
127+
128+
});
129+
119130
Object.defineProperty(Phaser.Sprite.prototype, 'x', {
120131

121132
get: function() {

src/math/Math.js

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -241,38 +241,53 @@ Phaser.Math = {
241241
* set an angle within the bounds of -PI to PI
242242
*/
243243
normalizeAngle: function (angle, radians) {
244+
244245
if (typeof radians === "undefined") { radians = true; }
246+
245247
var rd = (radians) ? GameMath.PI : 180;
246248
return this.wrap(angle, rd, -rd);
249+
247250
},
248251

249252
/**
250253
* closest angle between two angles from a1 to a2
251254
* absolute value the return for exact angle
252255
*/
253256
nearestAngleBetween: function (a1, a2, radians) {
257+
254258
if (typeof radians === "undefined") { radians = true; }
259+
255260
var rd = (radians) ? GameMath.PI : 180;
256261
a1 = this.normalizeAngle(a1, radians);
257262
a2 = this.normalizeAngle(a2, radians);
258-
if(a1 < -rd / 2 && a2 > rd / 2) {
263+
264+
if (a1 < -rd / 2 && a2 > rd / 2)
265+
{
259266
a1 += rd * 2;
260267
}
261-
if(a2 < -rd / 2 && a1 > rd / 2) {
268+
269+
if (a2 < -rd / 2 && a1 > rd / 2)
270+
{
262271
a2 += rd * 2;
263272
}
273+
264274
return a2 - a1;
275+
265276
},
266277

267278
/**
268279
* interpolate across the shortest arc between two angles
269280
*/
270281
interpolateAngles: function (a1, a2, weight, radians, ease) {
282+
271283
if (typeof radians === "undefined") { radians = true; }
272284
if (typeof ease === "undefined") { ease = null; }
285+
273286
a1 = this.normalizeAngle(a1, radians);
274287
a2 = this.normalizeAngleToAnother(a2, a1, radians);
288+
275289
return (typeof ease === 'function') ? ease(weight, a1, a2 - a1, 1) : this.interpolateFloat(a1, a2, weight);
290+
276291
},
277292

278293
/**
@@ -285,18 +300,29 @@ Phaser.Math = {
285300
* @return true if the roll passed, or false
286301
*/
287302
chanceRoll: function (chance) {
303+
288304
if (typeof chance === "undefined") { chance = 50; }
289-
if(chance <= 0) {
305+
306+
if (chance <= 0)
307+
{
290308
return false;
291-
} else if(chance >= 100) {
309+
}
310+
else if (chance >= 100)
311+
{
292312
return true;
293-
} else {
294-
if(Math.random() * 100 >= chance) {
313+
}
314+
else
315+
{
316+
if (Math.random() * 100 >= chance)
317+
{
295318
return false;
296-
} else {
319+
}
320+
else
321+
{
297322
return true;
298323
}
299324
}
325+
300326
},
301327

302328
/**
@@ -308,11 +334,16 @@ Phaser.Math = {
308334
* @return The new value
309335
*/
310336
maxAdd: function (value, amount, max) {
337+
311338
value += amount;
312-
if(value > max) {
339+
340+
if (value > max)
341+
{
313342
value = max;
314343
}
344+
315345
return value;
346+
316347
},
317348

318349
/**
@@ -324,11 +355,16 @@ Phaser.Math = {
324355
* @return The new value
325356
*/
326357
minSub: function (value, amount, min) {
358+
327359
value -= amount;
328-
if(value < min) {
360+
361+
if (value < min)
362+
{
329363
value = min;
330364
}
365+
331366
return value;
367+
332368
},
333369

334370
/**
@@ -341,12 +377,15 @@ Phaser.Math = {
341377
* @return The wrapped value
342378
*/
343379
wrapValue: function (value, amount, max) {
380+
344381
var diff;
345382
value = Math.abs(value);
346383
amount = Math.abs(amount);
347384
max = Math.abs(max);
348385
diff = (value + amount) % max;
386+
349387
return diff;
388+
350389
},
351390

352391
/**
@@ -366,11 +405,9 @@ Phaser.Math = {
366405
* @return True if the given number is odd. False if the given number is even.
367406
*/
368407
isOdd: function (n) {
369-
if(n & 1) {
370-
return true;
371-
} else {
372-
return false;
373-
}
408+
409+
return (n & 1);
410+
374411
},
375412

376413
/**
@@ -381,11 +418,16 @@ Phaser.Math = {
381418
* @return True if the given number is even. False if the given number is odd.
382419
*/
383420
isEven: function (n) {
384-
if(n & 1) {
421+
422+
if (n & 1)
423+
{
385424
return false;
386-
} else {
425+
}
426+
else
427+
{
387428
return true;
388429
}
430+
389431
},
390432

391433
/**
@@ -397,17 +439,25 @@ Phaser.Math = {
397439
* @return The new angle value, returns the same as the input angle if it was within bounds
398440
*/
399441
wrapAngle: function (angle) {
442+
400443
var result = angle;
444+
401445
// Nothing needs to change
402-
if(angle >= -180 && angle <= 180) {
446+
if (angle >= -180 && angle <= 180)
447+
{
403448
return angle;
404449
}
450+
405451
// Else normalise it to -180, 180
406452
result = (angle + 180) % 360;
407-
if(result < 0) {
453+
454+
if (result < 0)
455+
{
408456
result += 360;
409457
}
458+
410459
return result - 180;
460+
411461
},
412462

413463
/**
@@ -541,18 +591,27 @@ Phaser.Math = {
541591
* @return The random object that was selected.
542592
*/
543593
getRandom: function (objects, startIndex, length) {
594+
544595
if (typeof startIndex === "undefined") { startIndex = 0; }
545596
if (typeof length === "undefined") { length = 0; }
546-
if(objects != null) {
597+
598+
if (objects != null) {
599+
547600
var l = length;
548-
if((l == 0) || (l > objects.length - startIndex)) {
601+
602+
if ((l == 0) || (l > objects.length - startIndex))
603+
{
549604
l = objects.length - startIndex;
550605
}
551-
if(l > 0) {
606+
607+
if (l > 0)
608+
{
552609
return objects[startIndex + Math.floor(Math.random() * l)];
553610
}
554611
}
612+
555613
return null;
614+
556615
},
557616

558617
/**
@@ -563,8 +622,11 @@ Phaser.Math = {
563622
* @return The rounded value of that number.
564623
*/
565624
floor: function (value) {
625+
566626
var n = value | 0;
627+
567628
return (value > 0) ? (n) : ((n != value) ? (n - 1) : (n));
629+
568630
},
569631

570632
/**

src/system/Device.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Phaser.Device.prototype = {
309309
this.file = !!window['File'] && !!window['FileReader'] && !!window['FileList'] && !!window['Blob'];
310310
this.fileSystem = !!window['requestFileSystem'];
311311
this.webGL = ( function () { try { return !! window.WebGLRenderingContext && !! document.createElement( 'canvas' ).getContext( 'experimental-webgl' ); } catch( e ) { return false; } } )();
312+
// this.webGL = !!window['WebGLRenderingContext'];
312313
this.worker = !!window['Worker'];
313314

314315
if ('ontouchstart' in document.documentElement || window.navigator.msPointerEnabled) {

0 commit comments

Comments
 (0)