@@ -167,62 +167,65 @@ cc.isAddedHiddenEvent = false;
167
167
*/
168
168
cc . setup = function ( el , width , height ) {
169
169
var element = cc . $ ( el ) || cc . $ ( '#' + el ) ;
170
+ var localCanvas , localContainer , localConStyle ;
170
171
if ( element . tagName == "CANVAS" ) {
171
172
width = width || element . width ;
172
173
height = height || element . height ;
173
174
174
175
//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 ) ;
185
187
} else { //we must make a new canvas and place into this element
186
188
if ( element . tagName != "DIV" ) {
187
189
cc . log ( "Warning: target element is not a DIV or CANVAS" ) ;
188
190
}
189
191
width = width || element . clientWidth ;
190
192
height = height || element . clientHeight ;
191
193
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" ;
201
204
}
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%' ;
205
208
206
209
if ( cc . __renderDoesnotSupport )
207
210
return ;
208
211
209
212
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 } ) ;
211
214
if ( cc . renderContext ) {
212
215
cc . renderContextType = cc . WEBGL ;
213
216
window . gl = cc . renderContext ; // global variable declared in CCMacro.js
214
217
cc . drawingUtil = new cc . DrawingPrimitiveWebGL ( cc . renderContext ) ;
215
218
cc . TextureCache . getInstance ( ) . _initializingRenderer ( ) ;
216
219
} else {
217
- cc . renderContext = cc . canvas . getContext ( "2d" ) ;
220
+ cc . renderContext = localCanvas . getContext ( "2d" ) ;
218
221
cc . mainRenderContextBackup = cc . renderContext ;
219
222
cc . renderContextType = cc . CANVAS ;
220
- cc . renderContext . translate ( 0 , cc . canvas . height ) ;
223
+ cc . renderContext . translate ( 0 , localCanvas . height ) ;
221
224
cc . drawingUtil = new cc . DrawingPrimitiveCanvas ( cc . renderContext ) ;
222
225
}
223
226
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 ;
226
229
227
230
cc . log ( cc . ENGINE_VERSION ) ;
228
231
cc . Configuration . getInstance ( ) ;
@@ -250,13 +253,30 @@ cc.setup = function (el, width, height) {
250
253
}
251
254
252
255
function handleVisibilityChange ( ) {
253
- if ( ! document [ hidden ] )
256
+ var audioEngine = cc . AudioEngine . getInstance ( ) ;
257
+ if ( ! document [ hidden ] ) {
254
258
cc . Director . getInstance ( ) . _resetLastUpdate ( ) ;
259
+ audioEngine . resumeAllEffects ( ) ;
260
+ audioEngine . resumeMusic ( ) ;
261
+ } else {
262
+ audioEngine . pauseAllEffects ( ) ;
263
+ audioEngine . pauseMusic ( ) ;
264
+ }
255
265
}
256
266
257
267
if ( typeof document . addEventListener === "undefined" ||
258
268
typeof hidden === "undefined" ) {
259
269
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 ) ;
260
280
} else {
261
281
cc . isAddedHiddenEvent = true ;
262
282
document . addEventListener ( visibilityChange , handleVisibilityChange , false ) ;
@@ -275,7 +295,8 @@ cc._addUserSelectStatus = function(){
275
295
cc . _addBottomTag = function ( ) {
276
296
var bottom = document . createElement ( "div" ) ;
277
297
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" ;
279
300
document . body . appendChild ( bottom ) ;
280
301
window . location . href = "#bottom" ;
281
302
} ;
0 commit comments