Skip to content

Instantly share code, notes, and snippets.

@vrobel
Created January 30, 2013 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrobel/4674494 to your computer and use it in GitHub Desktop.
Save vrobel/4674494 to your computer and use it in GitHub Desktop.
Starling QuadBatch test
/**
* Created with IntelliJ IDEA.
* User: wrobel221
* Date: 10.10.12
* Time: 00:13
* To change this template use File | Settings | File Templates.
*/
package com.blackmoondev.panda.boot.test
{
import com.blackmoondev.ashes.debug.DebugFunctionCaller;
import com.blackmoondev.ashes.model.assets.STextureAtlasFactory;
import com.blackmoondev.panda.game.animations.PandaAnimation;
import com.blackmoondev.spritesheet.textures.provider.SAtlasProvider;
import feathers.controls.Header;
import feathers.controls.Label;
import feathers.controls.Screen;
import starling.core.Starling;
import starling.display.DisplayObject;
import starling.display.Image;
import starling.display.MovieClip;
import starling.display.QuadBatch;
import starling.events.Event;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import utils.array.getRandomElement;
import utils.number.randomIntegerWithinRange;
public class SQuadBatchTest extends Screen
{
[Embed(source="/../assets/panda.png", mimeType='image/png')]
public static const spritesheet : Class;
[Embed(source="/../assets/panda.xml", mimeType="application/octet-stream")]
public static const configXML : Class;
public function SQuadBatchTest() {
super();
this.addEventListener( Event.ADDED_TO_STAGE, addedToStageHandler );
}
private var debugFC : DebugFunctionCaller;
private var atlas : TextureAtlas;
private var _quadBatch : QuadBatch;
private var _images : Vector.<Image>;
private var header : Header;
public static const ANIMATIONS : Array = ['Panda_Fall', 'Panda_Ground', 'Panda_KilledDestroy', 'Panda_KilledFall', 'ParachuteDestroy', 'ShootEffect'];
private var _label : Label;
private function addedToStageHandler( event : Event ) : void {
this.removeEventListener( Event.ADDED_TO_STAGE, addedToStageHandler );
/**
* STARLING
*/
var atlasProvider : SAtlasProvider = new SAtlasProvider(
new STextureAtlasFactory(
configXML, spritesheet
) );
atlas = atlasProvider.atlas;
_quadBatch = new QuadBatch();
addChild( _quadBatch );
debugFC = new DebugFunctionCaller( Starling.current.nativeOverlay, 0, 100 );
debugFC.add( _quadBatch.reset, 'reset' );
debugFC.add( addMovieClips, '+1 mc', 1 );
debugFC.add( addMovieClips, '+10 mc', 10 );
debugFC.add( addMovieClips, '+100 mc', 100 );
// debugFC.add( addImages, '+1 img', 1 );
// debugFC.add( addImages, '+10 img', 10 );
// debugFC.add( addImages, '+100 img', 100 );
_images = new Vector.<Image>();
this.addEventListener(Event.ENTER_FRAME, update);
header = new Header();
addChild( header );
}
private function update( e : Event ) : void {
_quadBatch.reset();
batching();
}
private function updateTitle() : void {
header.title = 'images count: ' + _images.length;
}
private function batching() : void {
for each ( var image : Image in _images ) {
_quadBatch.addImage( image );
}
}
private function addMovieClips(count:int = 1) : void {
for ( var i : int = 0; i < count; i++ ) {
var textures : Vector.<Texture> = atlas.getTextures( getRandomElement(ANIMATIONS) );
var movieClip : MovieClip = new MovieClip( textures, 60 );
Starling.juggler.add( movieClip );
randomizeMCFrame( movieClip );
randomizePosition( movieClip );
_images.push( movieClip );
}
updateTitle()
}
private function randomizeMCFrame( mc : MovieClip ) : void {
mc.currentFrame = randomIntegerWithinRange( 0, mc.numFrames - 1 );
}
private function addImages(count:int = 1) : void {
for ( var i : int = 0; i < count; i++ ) {
var texture : Texture = atlas.getTexture('Panda_Fall0000');
var image : Image = new Image( texture );
randomizePosition( image );
_images.push( image );
}
updateTitle()
}
private function randomizePosition( object : DisplayObject ) : void {
object.x = randomIntegerWithinRange( 0, width - object.width );
object.y = randomIntegerWithinRange( 0, height - object.height );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment