Phaser now running on iOS. Also fixed a legacy bug where a pending sound wouldn't play once it was touch unlocked. Also fixed Input not working on WebGL contexts. Added WebGL texture updates to the Group/World swap functions.

This commit is contained in:
Richard Davey
2013-09-10 16:46:39 +01:00
parent 5da0e42be4
commit a486bf6b4a
17 changed files with 338 additions and 201 deletions
+7
View File
@@ -8,6 +8,11 @@
</head>
<body>
<input type="text" id="mx" value="0" />
<input type="text" id="my" value="0" />
<br />
<script type="text/javascript">
(function () {
@@ -31,6 +36,8 @@
}
function update() {
document.getElementById('mx').value = game.input.x;
document.getElementById('my').value = game.input.y;
}
})();
+60
View File
@@ -0,0 +1,60 @@
<!DOCTYPE HTML>
<html>
<head>
<title>phaser.js - a new beginning</title>
<?php
require('js.php');
?>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>
<script type="text/javascript">
(function () {
var game = new Phaser.Game(400, 300, Phaser.AUTO, '', { preload: preload, create: create, update: update });
function preload() {
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
var m;
function create() {
// game.stage.canvas.style['-webkit-transform-origin'] = 'top left';
// game.stage.canvas.style['-webkit-transform'] = 'scale(3,3)';
// game.stage.scaleMode = Phaser.StageScaleMode.EXACT_FIT;
// game.stage.scale.
// game.stage.scale.setScreenSize();
m = game.add.sprite(0, 0, 'mushroom');
m.anchor.setTo(0.5, 0.5);
// game.input.onDown.add(newMushroom, this);
}
function newMushroom(pointer) {
// game.add.sprite(pointer.x, pointer.y, 'mushroom');
}
function update() {
m.x = game.input.x;
m.y = game.input.y;
}
})();
</script>
</body>
</html>
+1 -5
View File
@@ -26,7 +26,7 @@
function create() {
game.world._stage.backgroundColorString = '#182d3b';
game.stage.backgroundColor = '#182d3b';
music = game.add.audio('boden');
music.play();
@@ -37,15 +37,11 @@
}
function update() {
s.rotation += 0.01;
}
function render() {
game.debug.renderSoundInfo(music, 20, 32);
}
})();
+3 -1
View File
@@ -312,9 +312,11 @@ Phaser.Game.prototype = {
this.load.onLoadComplete.add(this.loadComplete, this);
this.stage.boot();
this.world.boot();
this.state.boot();
this.input.boot();
this.sound.boot();
if (this.renderType == Phaser.CANVAS)
{
@@ -355,7 +357,7 @@ Phaser.Game.prototype = {
else
{
// They must have requested WebGL and their browser supports it
this.renderer = new PIXI.WebGLRenderer(this.width, this.height, null, this.transparent, this.antialias);
this.renderer = new PIXI.WebGLRenderer(this.width, this.height, this.stage.canvas, this.transparent, this.antialias);
this.canvas = this.renderer.view;
this.context = null;
}
+30
View File
@@ -132,6 +132,16 @@ Phaser.Group.prototype = {
if (child1Prev) { child1Prev._iNext = child2; }
if (child2Next) { child2Next._iPrev = child1; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
else if (child2._iNext == child1)
@@ -145,6 +155,16 @@ Phaser.Group.prototype = {
if (child2Prev) { child2Prev._iNext = child1; }
if (child1Next) { child2Next._iPrev = child2; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
else
@@ -160,6 +180,16 @@ Phaser.Group.prototype = {
if (child2Prev) { child2Prev._iNext = child1; }
if (child2Next) { child2Next._iPrev = child1; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
+23 -19
View File
@@ -27,29 +27,11 @@ Phaser.Stage = function (game, width, height) {
// The Pixi Stage which is hooked to the renderer
this._stage = new PIXI.Stage(0x000000, false);
this._stage.name = '_stage_root';
Phaser.Canvas.getOffset(this.canvas, this.offset);
this.bounds = new Phaser.Rectangle(this.offset.x, this.offset.y, this.game.width, this.game.height);
this.scaleMode = Phaser.StageScaleMode.NO_SCALE;
this.scale = new Phaser.StageScaleMode(this.game, width, height);
this.aspectRatio = width / height;
var _this = this;
this._onChange = function (event) {
return _this.visibilityChange(event);
}
document.addEventListener('visibilitychange', this._onChange, false);
document.addEventListener('webkitvisibilitychange', this._onChange, false);
document.addEventListener('pagehide', this._onChange, false);
document.addEventListener('pageshow', this._onChange, false);
window.onblur = this._onChange;
window.onfocus = this._onChange;
};
Phaser.Stage.prototype = {
@@ -60,6 +42,28 @@ Phaser.Stage.prototype = {
bounds: null,
offset: null,
boot: function () {
Phaser.Canvas.getOffset(this.canvas, this.offset);
this.bounds = new Phaser.Rectangle(this.offset.x, this.offset.y, this.game.width, this.game.height);
var _this = this;
this._onChange = function (event) {
return _this.visibilityChange(event);
}
document.addEventListener('visibilitychange', this._onChange, false);
document.addEventListener('webkitvisibilitychange', this._onChange, false);
document.addEventListener('pagehide', this._onChange, false);
document.addEventListener('pageshow', this._onChange, false);
window.onblur = this._onChange;
window.onfocus = this._onChange;
},
/**
* This method is called when the document visibility is changed.
*/
+30
View File
@@ -148,6 +148,16 @@ Phaser.World.prototype = {
if (child1Prev) { child1Prev._iNext = child2; }
if (child2Next) { child2Next._iPrev = child1; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
else if (child2._iNext == child1)
@@ -161,6 +171,16 @@ Phaser.World.prototype = {
if (child2Prev) { child2Prev._iNext = child1; }
if (child1Next) { child2Next._iPrev = child2; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
else
@@ -176,6 +196,16 @@ Phaser.World.prototype = {
if (child2Prev) { child2Prev._iNext = child1; }
if (child2Next) { child2Next._iPrev = child1; }
if (child1.__renderGroup)
{
child1.__renderGroup.updateTexture(child1);
}
if (child2.__renderGroup)
{
child2.__renderGroup.updateTexture(child2);
}
return true;
}
+5 -5
View File
@@ -52,12 +52,12 @@ Phaser.MSPointer.prototype = {
return _this.onPointerUp(event);
};
this.game.stage.canvas.addEventListener('MSPointerDown', this._onMSPointerDown, false);
this.game.stage.canvas.addEventListener('MSPointerMove', this._onMSPointerMove, false);
this.game.stage.canvas.addEventListener('MSPointerUp', this._onMSPointerUp, false);
this.game.renderer.view.addEventListener('MSPointerDown', this._onMSPointerDown, false);
this.game.renderer.view.addEventListener('MSPointerMove', this._onMSPointerMove, false);
this.game.renderer.view.addEventListener('MSPointerUp', this._onMSPointerUp, false);
this.game.stage.canvas.style['-ms-content-zooming'] = 'none';
this.game.stage.canvas.style['-ms-touch-action'] = 'none';
this.game.renderer.view.style['-ms-content-zooming'] = 'none';
this.game.renderer.view.style['-ms-touch-action'] = 'none';
}
+4 -3
View File
@@ -55,9 +55,10 @@ Phaser.Mouse.prototype = {
return _this.onMouseUp(event);
};
this.game.stage.canvas.addEventListener('mousedown', this._onMouseDown, true);
this.game.stage.canvas.addEventListener('mousemove', this._onMouseMove, true);
this.game.stage.canvas.addEventListener('mouseup', this._onMouseUp, true);
this.game.renderer.view.addEventListener('mousedown', this._onMouseDown, true);
this.game.renderer.view.addEventListener('mousemove', this._onMouseMove, true);
this.game.renderer.view.addEventListener('mouseup', this._onMouseUp, true);
},
/**
-2
View File
@@ -242,8 +242,6 @@ Phaser.Pointer.prototype = {
{
this.game.input.x = this.x * this.game.input.scale.x;
this.game.input.y = this.y * this.game.input.scale.y;
// this.game.input.x = this.x;
// this.game.input.y = this.y;
this.game.input.position.setTo(this.x, this.y);
this.game.input.onDown.dispatch(this);
this.game.input.resetSpeed(this.x, this.y);
+6 -6
View File
@@ -77,12 +77,12 @@ Phaser.Touch.prototype = {
return _this.consumeTouchMove(event);
};
this.game.stage.canvas.addEventListener('touchstart', this._onTouchStart, false);
this.game.stage.canvas.addEventListener('touchmove', this._onTouchMove, false);
this.game.stage.canvas.addEventListener('touchend', this._onTouchEnd, false);
this.game.stage.canvas.addEventListener('touchenter', this._onTouchEnter, false);
this.game.stage.canvas.addEventListener('touchleave', this._onTouchLeave, false);
this.game.stage.canvas.addEventListener('touchcancel', this._onTouchCancel, false);
this.game.renderer.view.addEventListener('touchstart', this._onTouchStart, false);
this.game.renderer.view.addEventListener('touchmove', this._onTouchMove, false);
this.game.renderer.view.addEventListener('touchend', this._onTouchEnd, false);
this.game.renderer.view.addEventListener('touchenter', this._onTouchEnter, false);
this.game.renderer.view.addEventListener('touchleave', this._onTouchLeave, false);
this.game.renderer.view.addEventListener('touchcancel', this._onTouchCancel, false);
document.addEventListener('touchmove', this._documentTouchMove, false);
}
+4 -2
View File
@@ -167,7 +167,7 @@ Phaser.Cache.prototype = {
decoded = true;
}
this._sounds[key] = { url: url, data: data, locked: locked, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag };
this._sounds[key] = { url: url, data: data, isDecoding: false, decoded: decoded, webAudio: webAudio, audioTag: audioTag };
},
@@ -354,7 +354,9 @@ Phaser.Cache.prototype = {
* @return {object} The sound data you want.
*/
isSoundReady: function (key) {
return (this._sounds[key] && this._sounds[key].decoded == true && this._sounds[key].locked == false);
return (this._sounds[key] && this._sounds[key].decoded && this.game.sound.touchLocked == false);
},
/**
+40 -48
View File
@@ -3,6 +3,42 @@ Phaser.Sound = function (game, key, volume, loop) {
volume = volume || 1;
loop = loop || false;
/**
* Reference to AudioContext instance.
*/
this.context = null;
/**
* Decoded data buffer / Audio tag.
*/
this._buffer = null;
this._muted = false;
this.name = '';
this.markers = {};
this.autoplay = false;
this.totalDuration = 0;
this.startTime = 0;
this.currentTime = 0;
this.duration = 0;
this.stopTime = 0;
this.paused = false;
this.isPlaying = false;
this.currentMarker = '';
this.pendingPlayback = false;
this.override = false;
this.onDecoded = null;
this.onPlay = null;
this.onPause = null;
this.onResume = null;
this.onLoop = null;
this.onStop = null;
this.onMute = null;
this.onMarkerComplete = null;
this.game = game;
this.key = key;
this._volume = volume;
@@ -54,55 +90,13 @@ Phaser.Sound = function (game, key, volume, loop) {
Phaser.Sound.prototype = {
game: null,
/**
* Reference to AudioContext instance.
*/
context: null,
/**
* Decoded data buffer / Audio tag.
*/
_buffer: null,
_muted: false,
key: '',
name: '',
markers: {},
usingWebAudio: false,
usingAudioTag: false,
autoplay: false,
totalDuration: 0,
startTime: 0,
currentTime: 0,
duration: 0,
stopTime: 0,
paused: false,
loop: false,
isPlaying: false,
currentMarker: '',
pendingPlayback: false,
override: false,
onDecoded: null,
onPlay: null,
onPause: null,
onResume: null,
onLoop: null,
onStop: null,
onMute: null,
onMarkerComplete: null,
soundHasUnlocked: function (key) {
if (key == this.key)
{
this._sound = this.game.cache.getSoundData(this.key);
this.totalDuration = this._sound.duration;
console.log('sound has unlocked', this._sound);
// console.log('sound has unlocked' + this._sound);
}
},
@@ -201,7 +195,7 @@ Phaser.Sound.prototype = {
loop = loop || false;
forceRestart = forceRestart || false;
console.log('play', marker, 'current is', this.currentMarker);
// console.log('play ' + marker + ' position ' + position + ' volume ' + volume + ' loop ' + loop);
if (this.isPlaying == true && forceRestart == false && this.override == false)
{
@@ -211,7 +205,7 @@ Phaser.Sound.prototype = {
if (this.isPlaying && this.override)
{
//console.log('asked to play', marker, 'but already playing', this.currentMarker);
// console.log('asked to play ' + marker + ' but already playing ' + this.currentMarker);
if (this.usingWebAudio)
{
@@ -240,7 +234,7 @@ Phaser.Sound.prototype = {
this.loop = this.markers[marker].loop;
this.duration = this.markers[marker].duration * 1000;
//console.log('marker info loaded', this.loop, this.duration);
// console.log('marker info loaded', this.loop, this.duration);
this._tempMarker = marker;
this._tempPosition = this.position;
this._tempVolume = this.volume;
@@ -301,8 +295,6 @@ Phaser.Sound.prototype = {
this.currentTime = 0;
this.stopTime = this.startTime + this.duration;
this.onPlay.dispatch(this);
//console.log('playing, start', this.startTime, 'stop');
}
else
{
+89 -85
View File
@@ -7,79 +7,6 @@ Phaser.SoundManager = function (game) {
this.game = game;
this.onSoundDecode = new Phaser.Signal;
if (this.game.device.iOS && this.game.device.webAudio == false)
{
this.channels = 1;
}
if (game.device.iOS || (window['PhaserGlobal'] && window['PhaserGlobal'].fakeiOSTouchLock))
{
this.game.input.touch.callbackContext = this;
this.game.input.touch.touchStartCallback = this.unlock;
this.game.input.mouse.callbackContext = this;
this.game.input.mouse.mouseDownCallback = this.unlock;
this.touchLocked = true;
}
else
{
// What about iOS5?
this.touchLocked = false;
}
if (window['PhaserGlobal'])
{
// Check to see if all audio playback is disabled (i.e. handled by a 3rd party class)
if (window['PhaserGlobal'].disableAudio == true)
{
this.usingWebAudio = false;
this.noAudio = true;
return;
}
// Check if the Web Audio API is disabled (for testing Audio Tag playback during development)
if (window['PhaserGlobal'].disableWebAudio == true)
{
this.usingWebAudio = false;
this.usingAudioTag = true;
this.noAudio = false;
return;
}
}
if (!!window['AudioContext'])
{
this.context = new window['AudioContext']();
}
else if(!!window['webkitAudioContext'])
{
this.context = new window['webkitAudioContext']();
}
else if(!!window['Audio'])
{
this.usingWebAudio = false;
this.usingAudioTag = true;
}
else
{
this.usingWebAudio = false;
this.noAudio = true;
}
if (this.context !== null)
{
if (typeof this.context.createGain === 'undefined')
{
this.masterGain = this.context.createGainNode();
}
else
{
this.masterGain = this.context.createGain();
}
this.masterGain.gain.value = 1;
this.masterGain.connect(this.context.destination);
}
};
@@ -103,6 +30,84 @@ Phaser.SoundManager.prototype = {
channels: 32,
boot: function () {
if (this.game.device.iOS && this.game.device.webAudio == false)
{
this.channels = 1;
}
if (this.game.device.iOS || (window['PhaserGlobal'] && window['PhaserGlobal'].fakeiOSTouchLock))
{
this.game.input.touch.callbackContext = this;
this.game.input.touch.touchStartCallback = this.unlock;
this.game.input.mouse.callbackContext = this;
this.game.input.mouse.mouseDownCallback = this.unlock;
this.touchLocked = true;
}
else
{
// What about iOS5?
this.touchLocked = false;
}
if (window['PhaserGlobal'])
{
// Check to see if all audio playback is disabled (i.e. handled by a 3rd party class)
if (window['PhaserGlobal'].disableAudio == true)
{
this.usingWebAudio = false;
this.noAudio = true;
return;
}
// Check if the Web Audio API is disabled (for testing Audio Tag playback during development)
if (window['PhaserGlobal'].disableWebAudio == true)
{
this.usingWebAudio = false;
this.usingAudioTag = true;
this.noAudio = false;
return;
}
}
if (!!window['AudioContext'])
{
this.context = new window['AudioContext']();
}
else if(!!window['webkitAudioContext'])
{
this.context = new window['webkitAudioContext']();
}
else if(!!window['Audio'])
{
this.usingWebAudio = false;
this.usingAudioTag = true;
}
else
{
this.usingWebAudio = false;
this.noAudio = true;
}
if (this.context !== null)
{
if (typeof this.context.createGain === 'undefined')
{
this.masterGain = this.context.createGainNode();
}
else
{
this.masterGain = this.context.createGain();
}
this.masterGain.gain.value = 1;
this.masterGain.connect(this.context.destination);
}
},
unlock: function () {
if (this.touchLocked == false)
@@ -110,18 +115,8 @@ Phaser.SoundManager.prototype = {
return;
}
console.log('SoundManager touch unlocked');
if (this.game.device.webAudio && (window['PhaserGlobal'] && window['PhaserGlobal'].disableWebAudio == false))
{
// Create empty buffer and play it
var buffer = this.context.createBuffer(1, 1, 22050);
this._unlockSource = this.context.createBufferSource();
this._unlockSource.buffer = buffer;
this._unlockSource.connect(this.context.destination);
this._unlockSource.noteOn(0);
}
else
// Global override (mostly for Audio Tag testing)
if (this.game.device.webAudio == false || (window['PhaserGlobal'] && window['PhaserGlobal'].disableWebAudio == true))
{
// Create an Audio tag?
this.touchLocked = false;
@@ -131,6 +126,15 @@ Phaser.SoundManager.prototype = {
this.game.input.mouse.callbackContext = null;
this.game.input.mouse.mouseDownCallback = null;
}
else
{
// Create empty buffer and play it
var buffer = this.context.createBuffer(1, 1, 22050);
this._unlockSource = this.context.createBufferSource();
this._unlockSource.buffer = buffer;
this._unlockSource.connect(this.context.destination);
this._unlockSource.noteOn(0);
}
},
+1
View File
@@ -15,6 +15,7 @@ Phaser.Canvas = {
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.style.display = 'block';
return canvas;
+33 -24
View File
@@ -106,10 +106,6 @@ Phaser.StageScaleMode = function (game, width, height) {
this.scaleFactor = new Phaser.Point(1, 1);
this.aspectRatio = 0;
this.minWidth = width;
this.minHeight = height;
this.maxWidth = width;
this.maxHeight = height;
var _this = this;
@@ -136,7 +132,7 @@ Phaser.StageScaleMode.prototype = {
return;
}
var element = this.game.stage.canvas;
var element = this.game.canvas;
if (element['requestFullScreen'])
{
@@ -286,7 +282,10 @@ Phaser.StageScaleMode.prototype = {
*/
setScreenSize: function (force) {
force = force || false;
if (typeof force == 'undefined')
{
force = false;
}
if (this.game.device.iPad == false && this.game.device.webApp == false && this.game.device.desktop == false)
{
@@ -307,7 +306,8 @@ Phaser.StageScaleMode.prototype = {
// Set minimum height of content to new window height
document.documentElement['style'].minHeight = window.innerHeight + 'px';
if (this.incorrectOrientation == true) {
if (this.incorrectOrientation == true)
{
this.setMaximum();
}
else if (this.game.stage.scaleMode == Phaser.StageScaleMode.EXACT_FIT)
@@ -351,20 +351,20 @@ Phaser.StageScaleMode.prototype = {
}
}
this.game.stage.canvas.style.width = this.width + 'px';
this.game.stage.canvas.style.height = this.height + 'px';
this.game.canvas.style.width = this.width + 'px';
this.game.canvas.style.height = this.height + 'px';
this.game.input.scale.setTo(this.game.stage.width / this.width, this.game.stage.height / this.height);
this.game.input.scale.setTo(this.game.width / this.width, this.game.height / this.height);
if (this.pageAlignHorizontally)
{
if (this.width < window.innerWidth && this.incorrectOrientation == false)
{
this.game.stage.canvas.style.marginLeft = Math.round((window.innerWidth - this.width) / 2) + 'px';
this.game.canvas.style.marginLeft = Math.round((window.innerWidth - this.width) / 2) + 'px';
}
else
{
this.game.stage.canvas.style.marginLeft = '0px';
this.game.canvas.style.marginLeft = '0px';
}
}
@@ -372,19 +372,21 @@ Phaser.StageScaleMode.prototype = {
{
if (this.height < window.innerHeight && this.incorrectOrientation == false)
{
this.game.stage.canvas.style.marginTop = Math.round((window.innerHeight - this.height) / 2) + 'px';
this.game.canvas.style.marginTop = Math.round((window.innerHeight - this.height) / 2) + 'px';
}
else
{
this.game.stage.canvas.style.marginTop = '0px';
this.game.canvas.style.marginTop = '0px';
}
}
this.game.stage.getOffset(this.game.stage.canvas);
Phaser.Canvas.getOffset(this.game.canvas, this.game.stage.offset);
this.aspectRatio = this.width / this.height;
this.scaleFactor.x = this.game.stage.width / this.width;
this.scaleFactor.y = this.game.stage.height / this.height;
this.scaleFactor.x = this.game.width / this.width;
this.scaleFactor.y = this.game.height / this.height;
},
setMaximum: function () {
@@ -396,33 +398,40 @@ Phaser.StageScaleMode.prototype = {
setShowAll: function () {
var multiplier = Math.min((window.innerHeight / this.game.stage.height), (window.innerWidth / this.game.stage.width));
var multiplier = Math.min((window.innerHeight / this.game.height), (window.innerWidth / this.game.width));
this.width = Math.round(this.game.stage.width * multiplier);
this.height = Math.round(this.game.stage.height * multiplier);
this.width = Math.round(this.game.width * multiplier);
this.height = Math.round(this.game.height * multiplier);
},
setExactFit: function () {
if (this.maxWidth && window.innerWidth > this.maxWidth)
var availableWidth = window.innerWidth - 0;
var availableHeight = window.innerHeight - 5;
console.log('available', availableWidth, availableHeight);
if (this.maxWidth && availableWidth > this.maxWidth)
{
this.width = this.maxWidth;
}
else
{
this.width = window.innerWidth;
this.width = availableWidth;
}
if (this.maxHeight && window.innerHeight > this.maxHeight)
if (this.maxHeight && availableHeight > this.maxHeight)
{
this.height = this.maxHeight;
}
else
{
this.height = window.innerHeight;
this.height = availableHeight;
}
console.log('setExactFit', this.width, this.height, this.game.stage.offset);
}
};
+2 -1
View File
@@ -196,7 +196,8 @@ Phaser.Utils.Debug.prototype = {
color = color || 'rgb(255,255,255)';
this.start(x, y, color);
this.line('Sound: ' + sound.key + ' Locked: ' + sound.game.sound.touchLocked + ' Pending Playback: ' + sound.pendingPlayback);
this.line('Sound: ' + sound.key + ' Locked: ' + sound.game.sound.touchLocked);
this.line('Is Ready?: ' + this.game.cache.isSoundReady(sound.key) + ' Pending Playback: ' + sound.pendingPlayback);
this.line('Decoded: ' + sound.isDecoded + ' Decoding: ' + sound.isDecoding);
this.line('Total Duration: ' + sound.totalDuration + ' Playing: ' + sound.isPlaying);
this.line('Time: ' + sound.currentTime);