Category: AS3/Flash

Spawn and remove objects/enemies (AS3 for Flash, Lua for Corona, Javascript for Unity)

By , October 10, 2011 3:21 am

This is the first post in a series of new samples  that will show similar code translated into multiple engines, typically Flash(as3), Corona(Lua) and Unity(javascript), maybe even UDK too. This first example just spawns 10 squares(enemies) on the screen. If you click one, the square disappears. If you click the background, they all disappear. If you click the background when there are no squares, the squares will respawn.

Flash-AS3Flash source files

//create a table(array) to store the enemies
var enemyTable:Array = [];

//this function gets passed an enemy to remove
function removeEnemy(enemy){
	//loop through the table
	for( var i=0; i< enemyTable.length; i++ ){
		//find the item in the table that is the same as the enemy
		if(enemyTable[i] == enemy){
			//remove the enemy from the table
			enemyTable.splice(i,1);
			//remove the enemy
			removeChild(enemy);
			//break out of the loop
			break;
		}
	}
}

//listener that gets attached to each enemy
function touchListener(event:MouseEvent){
	//
	//
	//find out which enemy we are talking about
	var enemy = event.currentTarget;
	//need to remove event listener, it's  not automatically removed
	enemy.removeEventListener( MouseEvent.CLICK, touchListener);
	removeEnemy(enemy);
	//
	//
	//
}

//spawnEnemies function
function spawnEnemies(){
	//loop 10 times
	for(var count:int = 0; count<10; count++){
		//draw 10 rectangles at random locations
		var enemy = new Sprite();
		enemy.graphics.beginFill(0x8C8C8C);
		enemy.graphics.lineStyle(3, 0xB4B4B4);
		enemy.graphics.drawRect(0, 0, 50, 50);
		enemy.x = Math.random()*stage.stageWidth;
		enemy.y = Math.random()*stage.stageHeight;
		addChild(enemy);
		//add a touch event to each enemy
		enemy.addEventListener( MouseEvent.CLICK, touchListener )
		//add the enemy to the enemyTable
		enemyTable.push(enemy);
	}
}

//function called when you click the green bg
function removeAll( event:MouseEvent ){
	//
	//
	//if all the enemies are gone, respawn them
	if(enemyTable.length == 0){
		spawnEnemies();
	}else{
		//loop through the table
		for(var i:int=0;i<enemyTable.length; i++){
			//remove each enemy
			removeChild(enemyTable[i]);
		}
		//set the enemyTable to empty
		enemyTable = [];
	}
	//
}

//draw the bg
var bg:Sprite = new Sprite();
bg.graphics.beginFill(0x99ff00);
bg.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
addChild(bg);
bg.addEventListener( MouseEvent.CLICK, removeAll )
//spawn the enmies
spawnEnemies();

Corona-LuaCorona source files

--create a table(array) to store the enemies
local enemyTable = {};

--this function gets passed an enemy to remove
local function removeEnemy(enemy)
	--loop through the table
	for i=1,#enemyTable do
			--find the item in the table that is the same as the enemy
			if(enemyTable[i] == enemy) then
				--remove the enemy from the table
				table.remove (enemyTable,i )
				--remove the enemy
				enemy:removeSelf();
				--break out of the loop
				break;
			end
	end
end

--listener that gets attached to each enemy
local function touchListener( event  )
	--only do remove the enemy when the touch begins
	if(event.phase=="began") then
		--find out which enemy we are talking about
		local enemy = event.target;
			--don't need to remove event listener, it's automatically removed when you remove the object
			--enemy:removeEventListener( "touch", touchListener );
			removeEnemy(enemy);
			--by returning true it prevents the touch event from continuing to the bg
			 return true
	end
end

--spawnEnemies function
local function spawnEnemies()
	--loop 10 times
	for count = 1,10 do
		--draw 10 rectangles at random locations
		local enemy = display.newRect(math.random(display.contentWidth), math.random(display.contentHeight), 50, 50)
		enemy.strokeWidth = 3;
		enemy:setFillColor(140, 140, 140);
		enemy:setStrokeColor(180, 180, 180);
		--add a touch event to each enemy
		enemy:addEventListener( "touch", touchListener )
		--add the enemy to the enemyTable
		enemyTable[count] = enemy;
	end
end

--function called when you click the green bg
local function removeAll( event )
	--only remove all when the touch first starts
	if(event.phase=="began") then
		--if all the enemies are gone, respawn them
		if(#enemyTable == 0) then
			spawnEnemies();
		else
			--loop through the table
			for i=1,#enemyTable do
				--remove each enemy
				enemyTable[i]:removeSelf();
			end
			--set the enemyTable to empty
			enemyTable = {};
		end
	end
end

--draw the bg
local bg = display.newRect(0,0,display.contentWidth, display.contentHeight)
bg:setFillColor(153, 255, 0)
bg:addEventListener( "touch", removeAll )
--spawn the enmies
spawnEnemies();

Unity-JavascriptUnity source files

//create a table(array) to store the enemies
var enemyTable:Array = [];
var enemyPreFab:GameObject;

//this function gets passed an enemy to remove
function removeEnemy(enemy){
	print("removeEnemy()");
	print(enemy);
	//loop through the table
	for( var i=0; i< enemyTable.length; i++ ){
		//find the item in the table that is the same as the enemy
		if(enemyTable[i] == enemy){
			//remove the enemy from the table
			enemyTable.splice(i,1);
			//remove the enemy
			Destroy(enemy);
			//break out of the loop
			break;
		}
	}
}

//spawnEnemies function
function spawnEnemies(){
	//loop 10 times
	for(var count:int = 0; count<10; count++){
		//draw 10 rectangles at random locations
		var position:Vector3 = Vector3(Random.Range(0, 16), Random.Range(0, 10), 0);
		var enemy = Instantiate(enemyPreFab, position, Quaternion());
		//add the enemy to the enemyTable
		enemyTable.push(enemy);
	}

}

//function called when you click the green bg
function removeAll(){
	//
	//
	//if all the enemies are gone, respawn them
	if(enemyTable.length == 0){
		spawnEnemies();
	}else{
		//loop through the table
		for(var i:int=0;i<enemyTable.length; i++){
			//remove each enemy
			Destroy(enemyTable[i]);
		}
		//set the enemyTable to empty
		enemyTable = [];
	}
	//
}

function Start(){
	//spawn the enmies
	spawnEnemies();
}
function Update () {
	if(Input.GetMouseButtonUp(0)){
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit:RaycastHit;
		if (Physics.Raycast (ray, hit,100)) {
		    removeEnemy(hit.transform.gameObject);
		}else{
			removeAll();

		}
	}
}

Air 2.6 and iOS using FlashDevelop and CS5 on Windows 7

By , March 26, 2011 12:54 pm

So after searching for info on how to compile an iPhone app with Air 2.6 and CS5, I thought I would share my knowledge. My work flow is typically to code in Flash Develop and publish with Flash CS5. I have been using the iPhone packager in CS5 to compile apps but that is not an option with Air 2.6. It was really hard to find all the documentation for this process in one place so here are my findings/link.

You will need to use ADT (Air Developer Tool) to package your iPhone app, you will also use this tool for Android as well. You will not use the packager built into cs5 anymore. You can still work in CS5/FlashDevelop and you will still publish the swf there if that is what you typically do.
It was actually hard to find info on the Adobe site about ADT, but this was the best link: AIR Developer Tool (ADT) ADT is included with the Air 2.6 SDK. Below is a list of the steps needed to publish your 1st Air 2.6 iOS app.


I have covered steps 1 and 2 in great detail in the following Google document:
Flash to iPhone Steps Please Note: Step 1 and 2 will take a little while to do (plan for at least an hour if not more), thanks Apple!

1. You need to get an Apple Developer’s License. This costs $99usd for an individual or company. If you are a student at Champlain College, we are part of the iOS Developer University Program. Just shoot me an email letting me know that you want to sign up. Otherwise, you can get your developer’s license at http://developer.apple.com/.

2. You will need to create a development certificate and a development provisioning file at the iOS provisioning portal.

3. Download the Air 2.6 SDK
After downloading and unzipping the SDK, I saved it to here: C:\SDKs\AdobeAIRSDK

4. You will now need to set the PATH so windows will be able to find the Air 2.6 SDK. Please follow these instructions.

5. Open a command prompt: go to the start menu and then type cmd in the search field and click Enter. The command prompt should now be open. Check to see if you can access ADT (Air Developer Tool). Type: adt -version and click Enter. It should output: 2.6.0.19120.

6. More to come…

Sample descriptor file

Useful Links
Developing AIR applications for mobile devices
Creating your first AIR application for iOS
AIR application descriptor elements
Setting mobile application properties (descriptor file)