diff --git a/CocosDenshion/SimpleAudioEngine.js b/CocosDenshion/SimpleAudioEngine.js index 052b5cd379..8ff735cbdb 100644 --- a/CocosDenshion/SimpleAudioEngine.js +++ b/CocosDenshion/SimpleAudioEngine.js @@ -170,6 +170,7 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */ _maxAudioInstance:10, _canPlay:true, _musicListenerBound:null, + _musicIsStopped: false, /** * Constructor @@ -277,6 +278,7 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */ au.loop = loop || false; au.play(); cc.AudioEngine.isMusicPlaying = true; + this._musicIsStopped = false; }, _musicListener:function(e){ @@ -300,10 +302,10 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */ var au = locSoundList[locPlayingMusic].audio; au.pause(); au.currentTime = au.duration; - if (releaseData) { + if (releaseData) delete locSoundList[locPlayingMusic]; - } cc.AudioEngine.isMusicPlaying = false; + this._musicIsStopped = true; } }, @@ -314,7 +316,7 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */ * cc.AudioEngine.getInstance().pauseMusic(); */ pauseMusic:function () { - if (this._soundList.hasOwnProperty(this._playingMusic)) { + if (!this._musicIsStopped && this._soundList.hasOwnProperty(this._playingMusic)) { var au = this._soundList[this._playingMusic].audio; au.pause(); cc.AudioEngine.isMusicPlaying = false; @@ -328,7 +330,7 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */ * cc.AudioEngine.getInstance().resumeMusic(); */ resumeMusic:function () { - if (this._soundList.hasOwnProperty(this._playingMusic)) { + if (!this._musicIsStopped && this._soundList.hasOwnProperty(this._playingMusic)) { var au = this._soundList[this._playingMusic].audio; au.play(); au.addEventListener("pause", this._musicListenerBound , false); @@ -349,6 +351,7 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */ au.play(); au.addEventListener("pause", this._musicListenerBound , false); cc.AudioEngine.isMusicPlaying = true; + this._musicIsStopped = false; } }, @@ -715,6 +718,7 @@ cc.SimpleAudioEngineForMobile = cc.SimpleAudioEngine.extend({ au.loop = loop || false; au.play(); cc.AudioEngine.isMusicPlaying = true; + this._musicIsStopped = false; }, _musicListener:function(){ @@ -1818,7 +1822,7 @@ cc.AudioEngine.getInstance = function () { if (cc.Browser.supportWebAudio && !(/iPhone OS/.test(ua)||/iPad/.test(ua))) { this._instance = new cc.WebAudioEngine(); } else { - if(cc.Browser.isMobile) //TODO construct a supported list for mobile browser + if(cc.Browser.isMobile) // TODO construct a supported list for mobile browser this._instance = new cc.SimpleAudioEngineForMobile(); else this._instance = new cc.SimpleAudioEngine(); diff --git a/cocos2d/platform/CCApplication.js b/cocos2d/platform/CCApplication.js index 06b2540944..8a83dfc588 100644 --- a/cocos2d/platform/CCApplication.js +++ b/cocos2d/platform/CCApplication.js @@ -167,21 +167,23 @@ cc.isAddedHiddenEvent = false; */ cc.setup = function (el, width, height) { var element = cc.$(el) || cc.$('#' + el); + var localCanvas, localContainer, localConStyle; if (element.tagName == "CANVAS") { width = width || element.width; height = height || element.height; //it is already a canvas, we wrap it around with a div - cc.container = cc.$new("DIV"); - cc.canvas = element; - cc.canvas.parentNode.insertBefore(cc.container, cc.canvas); - cc.canvas.appendTo(cc.container); - cc.container.style.width = (width || 480) + "px"; - cc.container.style.height = (height || 320) + "px"; - cc.container.setAttribute('id', 'Cocos2dGameContainer'); - cc.container.style.margin = "0 auto"; - cc.canvas.setAttribute("width", width || 480); - cc.canvas.setAttribute("height", height || 320); + localContainer = cc.container = cc.$new("DIV"); + localConStyle = localContainer.style; + localCanvas = cc.canvas = element; + localCanvas.parentNode.insertBefore(localContainer, localCanvas); + localCanvas.appendTo(localContainer); + localConStyle.width = (width || 480) + "px"; + localConStyle.height = (height || 320) + "px"; + localContainer.setAttribute('id', 'Cocos2dGameContainer'); + localConStyle.margin = "0 auto"; + localCanvas.setAttribute("width", width || 480); + localCanvas.setAttribute("height", height || 320); } else {//we must make a new canvas and place into this element if (element.tagName != "DIV") { cc.log("Warning: target element is not a DIV or CANVAS"); @@ -189,40 +191,41 @@ cc.setup = function (el, width, height) { width = width || element.clientWidth; height = height || element.clientHeight; - cc.canvas = cc.$new("CANVAS"); - cc.canvas.addClass("gameCanvas"); - cc.canvas.setAttribute("width", width || 480); - cc.canvas.setAttribute("height", height || 320); - cc.container = element; - element.appendChild(cc.canvas); - cc.container.style.width = (width || 480) + "px"; - cc.container.style.height = (height || 320) + "px"; - cc.container.style.margin = "0 auto"; + localCanvas = cc.canvas = cc.$new("CANVAS"); + localCanvas.addClass("gameCanvas"); + localCanvas.setAttribute("width", width || 480); + localCanvas.setAttribute("height", height || 320); + localContainer = cc.container = element; + localConStyle = localContainer.style; + element.appendChild(localCanvas); + localConStyle.width = (width || 480) + "px"; + localConStyle.height = (height || 320) + "px"; + localConStyle.margin = "0 auto"; } - cc.container.style.position = 'relative'; - cc.container.style.overflow = 'hidden'; - cc.container.top = '100%'; + localConStyle.position = 'relative'; + localConStyle.overflow = 'hidden'; + localContainer.top = '100%'; if(cc.__renderDoesnotSupport) return; if (cc.Browser.supportWebGL) - cc.renderContext = cc.webglContext = cc.create3DContext(cc.canvas,{'stencil': true, 'preserveDrawingBuffer': true, 'alpha': false }); + cc.renderContext = cc.webglContext = cc.create3DContext(localCanvas,{'stencil': true, 'preserveDrawingBuffer': true, 'alpha': false }); if(cc.renderContext){ cc.renderContextType = cc.WEBGL; window.gl = cc.renderContext; // global variable declared in CCMacro.js cc.drawingUtil = new cc.DrawingPrimitiveWebGL(cc.renderContext); cc.TextureCache.getInstance()._initializingRenderer(); } else { - cc.renderContext = cc.canvas.getContext("2d"); + cc.renderContext = localCanvas.getContext("2d"); cc.mainRenderContextBackup = cc.renderContext; cc.renderContextType = cc.CANVAS; - cc.renderContext.translate(0, cc.canvas.height); + cc.renderContext.translate(0, localCanvas.height); cc.drawingUtil = new cc.DrawingPrimitiveCanvas(cc.renderContext); } - cc.originalCanvasSize = cc.size(cc.canvas.width, cc.canvas.height); - cc.gameDiv = cc.container; + cc.originalCanvasSize = cc.size(localCanvas.width, localCanvas.height); + cc.gameDiv = localContainer; cc.log(cc.ENGINE_VERSION); cc.Configuration.getInstance(); @@ -250,13 +253,30 @@ cc.setup = function (el, width, height) { } function handleVisibilityChange() { - if (!document[hidden]) + var audioEngine = cc.AudioEngine.getInstance(); + if (!document[hidden]){ cc.Director.getInstance()._resetLastUpdate(); + audioEngine.resumeAllEffects(); + audioEngine.resumeMusic(); + } else{ + audioEngine.pauseAllEffects(); + audioEngine.pauseMusic(); + } } if (typeof document.addEventListener === "undefined" || typeof hidden === "undefined") { cc.isAddedHiddenEvent = false; + window.addEventListener("focus", function () { + var audioEngine = cc.AudioEngine.getInstance(); + audioEngine.resumeAllEffects(); + audioEngine.resumeMusic(); + }, false); + window.addEventListener("blur", function () { + var audioEngine = cc.AudioEngine.getInstance(); + audioEngine.pauseAllEffects(); + audioEngine.pauseMusic(); + }, false); } else { cc.isAddedHiddenEvent = true; document.addEventListener(visibilityChange, handleVisibilityChange, false); @@ -275,7 +295,8 @@ cc._addUserSelectStatus = function(){ cc._addBottomTag = function () { var bottom = document.createElement("div"); bottom.id = "bottom"; - bottom.style.border = bottom.style.margin = bottom.style.padding = bottom.style.height = bottom.style.lineHeight = bottom.style.fontSize = "0px"; + var bStyle = bottom.style; + bStyle.border = bStyle.margin = bStyle.padding = bStyle.height = bStyle.lineHeight = bStyle.fontSize = "0px"; document.body.appendChild(bottom); window.location.href="#bottom"; }; diff --git a/cocos2d/platform/CCClass.js b/cocos2d/platform/CCClass.js index 56f54b31b1..054c2e41c6 100644 --- a/cocos2d/platform/CCClass.js +++ b/cocos2d/platform/CCClass.js @@ -151,10 +151,10 @@ ClassManager.getNewInstanceId=function(){ // The dummy Class constructor function Class() { + this.__instanceId = ClassManager.getNewInstanceId(); // All construction is actually done in the init method if (this.ctor) this.ctor.apply(this, arguments); - this.__instanceId = ClassManager.getNewInstanceId(); } Class.id = classId; diff --git a/cocos2d/platform/CCCommon.js b/cocos2d/platform/CCCommon.js index ff912822e6..53d96b8403 100644 --- a/cocos2d/platform/CCCommon.js +++ b/cocos2d/platform/CCCommon.js @@ -100,7 +100,7 @@ cc._logToWebPage = function (message) { logList = document.createElement("ul"); logDiv.appendChild(logList); logList.setAttribute("id", "logInfoList"); - logList.style.height = "200px"; + logList.style.height = cc.canvas.height + "px"; logList.style.color = "#fff"; logList.style.textAlign = "left"; logList.style.listStyle = "disc outside"; diff --git a/samples b/samples index 9f6535025b..7e3e716682 160000 --- a/samples +++ b/samples @@ -1 +1 @@ -Subproject commit 9f6535025b9a65d8fdcbba839f6f5254b3c84004 +Subproject commit 7e3e71668226fff45df140c007854b459b5a25aa