Posts tagged: 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();

		}
	}
}