Skip to content

C2D 10 Bunny Spawner

robsilv edited this page Apr 12, 2013 · 7 revisions

Following on from the last tutorial, we're going to improve it by introducing the concept of a BunnySpawner process.

Bunny Spawner screenshot

Although the last tutorial was cool... Part of it went against the grain in terms of Cadet scenes. Here's the offending section:

private function enterFrameHandler( event:Event ):void
{
	if (entityIndex < numEntities) {
		entityIndex ++;
		createBunny();
	}
	
	cadetScene.step();
}

private function createBunny():void
{
	// Add the BounceBehaviour to the scene
	var randomVelocity:Point = new Point(Math.random() * 10, (Math.random() * 10) - 5);
	var bounceBehaviour:BounceBehaviour = new BounceBehaviour();
	bounceBehaviour.velocity = randomVelocity;
	bounceBehaviour.boundsRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
	cadetScene.children.addItem(bounceBehaviour);
			
	// Add a Skin to the scene
	var skin:ImageSkin = new ImageSkin();
	skin.texture = textureComponent;
	cadetScene.children.addItem(skin);
			
	// Pass reference to skin to bounceBehaviour
	bounceBehaviour.transform = skin;
}

What's wrong with it? Well, part of the scene's functionality is being executed outside of the CadetScene. The code in enterFrameHandler is calling the createBunny method which exists in the root of your app. Ideally, this code should live inside the CadetScene along with everything else.

Create a new application in "src" named BunnyMarkSpawner, paste the following code into it and set it as your default application:

package
{
	import cadet.core.CadetScene;
	
	import cadet2D.components.renderers.Renderer2D;
	import cadet2D.components.textures.TextureComponent;
	
	import components.processes.BunnySpawner;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	[SWF( width="700", height="400", backgroundColor="0x002135", frameRate="60" )]
	public class BunnyMarkSpawner extends Sprite
	{
		[Embed(source = "assets/wabbit_alpha.png")]
		private var BunnyAsset:Class;
		
		private var cadetScene:CadetScene;
		
		public function BunnyMarkSpawner()
		{
			cadetScene = new CadetScene();
			
			var renderer:Renderer2D = new Renderer2D();
			renderer.viewportWidth = stage.stageWidth;
			renderer.viewportHeight = stage.stageHeight;
			cadetScene.children.addItem(renderer);
			renderer.enable(this);
			
			// Create the shared TextureComponent
			var textureComponent:TextureComponent = new TextureComponent();
			textureComponent.bitmapData = new BunnyAsset().bitmapData;
			cadetScene.children.addItem(textureComponent);
			
			// Create the BunnySpawner Process
			var bunnySpawner:BunnySpawner = new BunnySpawner();
			bunnySpawner.textureComponent = textureComponent;
			bunnySpawner.numEntities = 1000;
			bunnySpawner.boundsRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			cadetScene.children.addItem(bunnySpawner);
			
			addEventListener( Event.ENTER_FRAME, enterFrameHandler );			
		}
		
		private function enterFrameHandler( event:Event ):void
		{
			cadetScene.step();
		}
	}
}

You can see that the offending code has now disappeared from this scope and the BunnySpawner process has been introduced. Create the following class:

package components.processes
{
	import cadet.core.Component;
	import cadet.core.ISteppableComponent;
	
	import cadet2D.components.skins.ImageSkin;
	import cadet2D.components.textures.TextureComponent;
	
	import components.behaviours.BounceBehaviour;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BunnySpawner extends Component implements ISteppableComponent
	{
		public var numEntities			:int = 100;
		private var entityIndex			:uint;
		
		public var textureComponent		:TextureComponent;
		public var boundsRect			:Rectangle;
		
		public function BunnySpawner()
		{

		}
		
		private function createBunny():void
		{
			// Add the BounceBehaviour to the scene
			var randomVelocity:Point = new Point(Math.random() * 10, (Math.random() * 10) - 5);
			var bounceBehaviour:BounceBehaviour = new BounceBehaviour();
			bounceBehaviour.velocity = randomVelocity;
			bounceBehaviour.boundsRect = boundsRect;
			scene.children.addItem(bounceBehaviour);
			
			// Add a Skin to the scene
			var skin:ImageSkin = new ImageSkin();
			skin.texture = textureComponent;
			scene.children.addItem(skin);
			
			// Pass reference to skin to bounceBehaviour
			bounceBehaviour.transform = skin;			
		}
		
		public function step( dt:Number ):void
		{
			if (entityIndex < numEntities) {
				entityIndex ++;
				createBunny();
			}
		}
	}
}

As you can see, the createBunny() method and the responsibility for spawning bunnies every step() have been moved inside our new BunnySpawner process. This means we now have all of our functionality executing under the roof of the CadetScene. It also means that the BunnySpawner process can now be made available to the CadetEditor.

< Previous