2014-03-22 10:12:47 -07:00
/ *
* SpriteContainer
* Visit http : //createjs.com/ for documentation, updates and examples.
*
* Copyright ( c ) 2010 gskinner . com , inc .
2014-11-18 15:26:26 -08:00
*
2014-03-22 10:12:47 -07:00
* Permission is hereby granted , free of charge , to any person
* obtaining a copy of this software and associated documentation
* files ( the "Software" ) , to deal in the Software without
* restriction , including without limitation the rights to use ,
* copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the
* Software is furnished to do so , subject to the following
* conditions :
2014-11-18 15:26:26 -08:00
*
2014-03-22 10:12:47 -07:00
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software .
2014-11-18 15:26:26 -08:00
*
2014-03-22 10:12:47 -07:00
* THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND ,
* EXPRESS OR IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY , FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT . IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER LIABILITY ,
* WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING
* FROM , OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE .
* /
// namespace:
this . createjs = this . createjs || { } ;
( function ( ) {
2014-11-18 15:26:26 -08:00
"use strict" ;
2014-03-22 10:12:47 -07:00
/ * *
2014-11-18 15:26:26 -08:00
* A SpriteContainer is a nestable display list that enables aggressively optimized rendering of bitmap content .
* In order to accomplish these optimizations , SpriteContainer enforces a few restrictions on its content .
*
* Restrictions :
* - only Sprite , SpriteContainer , BitmapText and DOMElement are allowed to be added as children .
* - a spriteSheet MUST be either be passed into the constructor or defined on the first child added .
* - all children ( with the exception of DOMElement ) MUST use the same spriteSheet .
*
* < h4 > Example < / h 4 >
*
* var data = {
* images : [ "sprites.jpg" ] ,
* frames : { width : 50 , height : 50 } ,
* animations : { run : [ 0 , 4 ] , jump : [ 5 , 8 , "run" ] }
* } ;
* var spriteSheet = new createjs . SpriteSheet ( data ) ;
* var container = new createjs . SpriteContainer ( spriteSheet ) ;
* container . addChild ( spriteInstance , spriteInstance2 ) ;
* container . x = 100 ;
*
* < strong > Note : < / s t r o n g > S p r i t e C o n t a i n e r i s n o t i n c l u d e d i n t h e m i n i f i e d v e r s i o n o f E a s e l J S .
*
* @ class SpriteContainer
* @ extends Container
* @ constructor
* @ param { SpriteSheet } [ spriteSheet ] The spriteSheet to use for this SpriteContainer and its children .
2014-03-22 10:12:47 -07:00
* * /
2014-11-18 15:26:26 -08:00
function SpriteContainer ( spriteSheet ) {
this . Container _constructor ( ) ;
// public properties:
/ * *
* The SpriteSheet that this container enforces use of .
* @ property spriteSheet
* @ type { SpriteSheet }
* @ readonly
* * /
2014-03-22 10:12:47 -07:00
this . spriteSheet = spriteSheet ;
2014-11-18 15:26:26 -08:00
}
var p = createjs . extend ( SpriteContainer , createjs . Container ) ;
2014-03-22 10:12:47 -07:00
2014-11-18 15:26:26 -08:00
2014-03-22 10:12:47 -07:00
2014-11-18 15:26:26 -08:00
// public methods:
2014-03-22 10:12:47 -07:00
/ * *
* Adds a child to the top of the display list .
* Only children of type SpriteContainer , Sprite , Bitmap , BitmapText , or DOMElement are allowed .
* The child must have the same spritesheet as this container ( unless it ' s a DOMElement ) .
* If a spritesheet hasn 't been defined, this container uses this child' s spritesheet .
*
* < h4 > Example < / h 4 >
* container . addChild ( bitmapInstance ) ;
*
* You can also add multiple children at once :
*
* container . addChild ( bitmapInstance , shapeInstance , textInstance ) ;
*
* @ method addChild
* @ param { DisplayObject } child The display object to add .
* @ return { DisplayObject } The child that was added , or the last child if multiple children were added .
* * /
p . addChild = function ( child ) {
if ( child == null ) { return child ; }
if ( arguments . length > 1 ) {
return this . addChildAt . apply ( this , Array . prototype . slice . call ( arguments ) . concat ( [ this . children . length ] ) ) ;
} else {
return this . addChildAt ( child , this . children . length ) ;
}
} ;
/ * *
* Adds a child to the display list at the specified index , bumping children at equal or greater indexes up one , and
* setting its parent to this Container .
* Only children of type SpriteContainer , Sprite , Bitmap , BitmapText , or DOMElement are allowed .
* The child must have the same spritesheet as this container ( unless it ' s a DOMElement ) .
* If a spritesheet hasn 't been defined, this container uses this child' s spritesheet .
2014-11-18 15:26:26 -08:00
*
2014-03-22 10:12:47 -07:00
* < h4 > Example < / h 4 >
* addChildAt ( child1 , index ) ;
*
* You can also add multiple children , such as :
*
* addChildAt ( child1 , child2 , ... , index ) ;
*
* The index must be between 0 and numChildren . For example , to add myShape under otherShape in the display list ,
* you could use :
*
* container . addChildAt ( myShape , container . getChildIndex ( otherShape ) ) ;
*
* This would also bump otherShape ' s index up by one . Fails silently if the index is out of range .
*
* @ method addChildAt
* @ param { DisplayObject } child The display object to add .
* @ param { Number } index The index to add the child at .
* @ return { DisplayObject } Returns the last child that was added , or the last child if multiple children were added .
* * /
p . addChildAt = function ( child , index ) {
var l = arguments . length ;
var indx = arguments [ l - 1 ] ; // can't use the same name as the index param or it replaces arguments[1]
if ( indx < 0 || indx > this . children . length ) { return arguments [ l - 2 ] ; }
if ( l > 2 ) {
for ( var i = 0 ; i < l - 1 ; i ++ ) { this . addChildAt ( arguments [ i ] , indx + i ) ; }
return arguments [ l - 2 ] ;
}
if ( child . _spritestage _compatibility >= 1 ) {
// The child is compatible with SpriteStage/SpriteContainer.
} else {
console && console . log ( "Error: You can only add children of type SpriteContainer, Sprite, BitmapText, or DOMElement [" + child . toString ( ) + "]" ) ;
return child ;
}
if ( child . _spritestage _compatibility <= 4 ) {
var spriteSheet = child . spriteSheet ;
2014-11-18 15:26:26 -08:00
if ( ( ! spriteSheet || ! spriteSheet . _images || spriteSheet . _images . length > 1 ) || ( this . spriteSheet && this . spriteSheet !== spriteSheet ) ) {
2014-03-22 10:12:47 -07:00
console && console . log ( "Error: A child's spriteSheet must be equal to its parent spriteSheet and only use one image. [" + child . toString ( ) + "]" ) ;
return child ;
}
this . spriteSheet = spriteSheet ;
}
if ( child . parent ) { child . parent . removeChild ( child ) ; }
child . parent = this ;
this . children . splice ( index , 0 , child ) ;
return child ;
} ;
/ * *
* Returns a string representation of this object .
* @ method toString
* @ return { String } a string representation of the instance .
* * /
p . toString = function ( ) {
return "[SpriteContainer (name=" + this . name + ")]" ;
} ;
2014-11-18 15:26:26 -08:00
createjs . SpriteContainer = createjs . promote ( SpriteContainer , "Container" ) ;
} ( ) ) ;