@@ -167,62 +167,65 @@ cc.isAddedHiddenEvent = false;
167167 */
168168cc . setup = function ( el , width , height ) {
169169 var element = cc . $ ( el ) || cc . $ ( '#' + el ) ;
170+ var localCanvas , localContainer , localConStyle ;
170171 if ( element . tagName == "CANVAS" ) {
171172 width = width || element . width ;
172173 height = height || element . height ;
173174
174175 //it is already a canvas, we wrap it around with a div
175- cc . container = cc . $new ( "DIV" ) ;
176- cc . canvas = element ;
177- cc . canvas . parentNode . insertBefore ( cc . container , cc . canvas ) ;
178- cc . canvas . appendTo ( cc . container ) ;
179- cc . container . style . width = ( width || 480 ) + "px" ;
180- cc . container . style . height = ( height || 320 ) + "px" ;
181- cc . container . setAttribute ( 'id' , 'Cocos2dGameContainer' ) ;
182- cc . container . style . margin = "0 auto" ;
183- cc . canvas . setAttribute ( "width" , width || 480 ) ;
184- cc . canvas . setAttribute ( "height" , height || 320 ) ;
176+ localContainer = cc . container = cc . $new ( "DIV" ) ;
177+ localConStyle = localContainer . style ;
178+ localCanvas = cc . canvas = element ;
179+ localCanvas . parentNode . insertBefore ( localContainer , localCanvas ) ;
180+ localCanvas . appendTo ( localContainer ) ;
181+ localConStyle . width = ( width || 480 ) + "px" ;
182+ localConStyle . height = ( height || 320 ) + "px" ;
183+ localContainer . setAttribute ( 'id' , 'Cocos2dGameContainer' ) ;
184+ localConStyle . margin = "0 auto" ;
185+ localCanvas . setAttribute ( "width" , width || 480 ) ;
186+ localCanvas . setAttribute ( "height" , height || 320 ) ;
185187 } else { //we must make a new canvas and place into this element
186188 if ( element . tagName != "DIV" ) {
187189 cc . log ( "Warning: target element is not a DIV or CANVAS" ) ;
188190 }
189191 width = width || element . clientWidth ;
190192 height = height || element . clientHeight ;
191193
192- cc . canvas = cc . $new ( "CANVAS" ) ;
193- cc . canvas . addClass ( "gameCanvas" ) ;
194- cc . canvas . setAttribute ( "width" , width || 480 ) ;
195- cc . canvas . setAttribute ( "height" , height || 320 ) ;
196- cc . container = element ;
197- element . appendChild ( cc . canvas ) ;
198- cc . container . style . width = ( width || 480 ) + "px" ;
199- cc . container . style . height = ( height || 320 ) + "px" ;
200- cc . container . style . margin = "0 auto" ;
194+ localCanvas = cc . canvas = cc . $new ( "CANVAS" ) ;
195+ localCanvas . addClass ( "gameCanvas" ) ;
196+ localCanvas . setAttribute ( "width" , width || 480 ) ;
197+ localCanvas . setAttribute ( "height" , height || 320 ) ;
198+ localContainer = cc . container = element ;
199+ localConStyle = localContainer . style ;
200+ element . appendChild ( localCanvas ) ;
201+ localConStyle . width = ( width || 480 ) + "px" ;
202+ localConStyle . height = ( height || 320 ) + "px" ;
203+ localConStyle . margin = "0 auto" ;
201204 }
202- cc . container . style . position = 'relative' ;
203- cc . container . style . overflow = 'hidden' ;
204- cc . container . top = '100%' ;
205+ localConStyle . position = 'relative' ;
206+ localConStyle . overflow = 'hidden' ;
207+ localContainer . top = '100%' ;
205208
206209 if ( cc . __renderDoesnotSupport )
207210 return ;
208211
209212 if ( cc . Browser . supportWebGL )
210- cc . renderContext = cc . webglContext = cc . create3DContext ( cc . canvas , { 'stencil' : true , 'preserveDrawingBuffer' : true , 'alpha' : false } ) ;
213+ cc . renderContext = cc . webglContext = cc . create3DContext ( localCanvas , { 'stencil' : true , 'preserveDrawingBuffer' : true , 'alpha' : false } ) ;
211214 if ( cc . renderContext ) {
212215 cc . renderContextType = cc . WEBGL ;
213216 window . gl = cc . renderContext ; // global variable declared in CCMacro.js
214217 cc . drawingUtil = new cc . DrawingPrimitiveWebGL ( cc . renderContext ) ;
215218 cc . TextureCache . getInstance ( ) . _initializingRenderer ( ) ;
216219 } else {
217- cc . renderContext = cc . canvas . getContext ( "2d" ) ;
220+ cc . renderContext = localCanvas . getContext ( "2d" ) ;
218221 cc . mainRenderContextBackup = cc . renderContext ;
219222 cc . renderContextType = cc . CANVAS ;
220- cc . renderContext . translate ( 0 , cc . canvas . height ) ;
223+ cc . renderContext . translate ( 0 , localCanvas . height ) ;
221224 cc . drawingUtil = new cc . DrawingPrimitiveCanvas ( cc . renderContext ) ;
222225 }
223226
224- cc . originalCanvasSize = cc . size ( cc . canvas . width , cc . canvas . height ) ;
225- cc . gameDiv = cc . container ;
227+ cc . originalCanvasSize = cc . size ( localCanvas . width , localCanvas . height ) ;
228+ cc . gameDiv = localContainer ;
226229
227230 cc . log ( cc . ENGINE_VERSION ) ;
228231 cc . Configuration . getInstance ( ) ;
@@ -250,13 +253,30 @@ cc.setup = function (el, width, height) {
250253 }
251254
252255 function handleVisibilityChange ( ) {
253- if ( ! document [ hidden ] )
256+ var audioEngine = cc . AudioEngine . getInstance ( ) ;
257+ if ( ! document [ hidden ] ) {
254258 cc . Director . getInstance ( ) . _resetLastUpdate ( ) ;
259+ audioEngine . resumeAllEffects ( ) ;
260+ audioEngine . resumeMusic ( ) ;
261+ } else {
262+ audioEngine . pauseAllEffects ( ) ;
263+ audioEngine . pauseMusic ( ) ;
264+ }
255265 }
256266
257267 if ( typeof document . addEventListener === "undefined" ||
258268 typeof hidden === "undefined" ) {
259269 cc . isAddedHiddenEvent = false ;
270+ window . addEventListener ( "focus" , function ( ) {
271+ var audioEngine = cc . AudioEngine . getInstance ( ) ;
272+ audioEngine . resumeAllEffects ( ) ;
273+ audioEngine . resumeMusic ( ) ;
274+ } , false ) ;
275+ window . addEventListener ( "blur" , function ( ) {
276+ var audioEngine = cc . AudioEngine . getInstance ( ) ;
277+ audioEngine . pauseAllEffects ( ) ;
278+ audioEngine . pauseMusic ( ) ;
279+ } , false ) ;
260280 } else {
261281 cc . isAddedHiddenEvent = true ;
262282 document . addEventListener ( visibilityChange , handleVisibilityChange , false ) ;
@@ -275,7 +295,8 @@ cc._addUserSelectStatus = function(){
275295cc . _addBottomTag = function ( ) {
276296 var bottom = document . createElement ( "div" ) ;
277297 bottom . id = "bottom" ;
278- bottom . style . border = bottom . style . margin = bottom . style . padding = bottom . style . height = bottom . style . lineHeight = bottom . style . fontSize = "0px" ;
298+ var bStyle = bottom . style ;
299+ bStyle . border = bStyle . margin = bStyle . padding = bStyle . height = bStyle . lineHeight = bStyle . fontSize = "0px" ;
279300 document . body . appendChild ( bottom ) ;
280301 window . location . href = "#bottom" ;
281302} ;
0 commit comments