As3 programando jogos
Pra quem gosta de programação, a vontade de desenvolver um jogo sempre vem a mente. As vezes você navega e acha um jogo legal, achando impossível de desenvolver um desses.
Abaixo um exemplo de jogo, você tem que fazer a defesa de seu território colocando canhões em pontos estratégicos, impedindo que o inimigo atravesse o terreno. Eu particularmente acho esse jogo muito legal.
link do jogo: http://www.minijuegos.com/juegos/jugar.php?id=7929
Olhando para esse jogo, você vai ter muitas dúvidas: como trilhar o caminho do inimigo, como fazer o canhão atirar no inimigo ou o hitTest para posicionar seus canhões em um campo pre-definido, essas e outras questões devem estar passando por sua cabeça.
No mesmo sentido do jogo acima, estou colocando um tutorial ensinando como fazer o motor básico desse jogo, com ele você pode criar diversos jogos, mesmo que não tenha interesse em criar um jogo, acho muito importante analisar e compreender o funcionamento.
Esse tutorial foi feito pelo pessoal da: flashgametuts.com estou somente espalhando.
Source: http://www.flashgametuts.com/obj/tuts/tower-defense-as3/pt7/tower-defense-as3-source.zip
1: var F:String = 'FINISH';2: var U:String = 'UP';3: var R:String = 'RIGHT';4: var D:String = 'DOWN';5: var L:String = 'LEFT';6:7: var startDir:String;//the direction the enemies go when they enter8: var finDir:String;//the direction the enemies go when they exit9: var startCoord:int;//the coordinates of the beginning of the road10: var lvlArray:Array = new Array();//this array will hold the formatting of the roads11:12: lvlArray = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14: 0,0,0,0,R,1,1,D,0,0,R,1,1,D,0,0,R,1,1,D,0,0,15: 0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,16: 0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,17: S,D,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,R,1,F,18: 0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,19: 0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,20: 0,R,1,1,U,0,0,R,1,1,U,0,0,R,1,1,U,0,0,0,0,0,21: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,22: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24: ];25:26: //the names of these variables explain what they do27: var currentLvl:int = 1;28: var gameOver:Boolean = false;29:30: var currentEnemy:int = 0;//the current enemy that we're creating from the array31: var enemyTime:int = 0;//how many frames have elapsed since the last enemy was created32: var enemyLimit:int = 12;//how many frames are allowed before another enemy is created33: var enemyArray:Array = new Array();//this array will tell the function when to create an enemy34: var enemiesLeft:int;//how many enemies are left on the field35: enemyArray = [//defining the array36: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],//1's will just represent an enemy to be created37: [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],//another row means another level38: [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],39: [100],40: [5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5,6,7,6,5],41: [250,250,250]42: ];43:44: var money:int=100;//how much money the player has to spend on turrets45: var lives:int=20;//how many lives the player has46:47: var rangeCircle:Shape = new Shape();48: rangeCircle.graphics.beginFill(0x006600,.5);49: rangeCircle.graphics.drawCircle(12.5,12.5,100);50: rangeCircle.graphics.endFill();51:52: function startGame():void{//we'll run this function every time a new level begins53: for(var i:int=0;i<enemyArray[currentLvl-1].length;i++){54: if(enemyArray[currentLvl-1][i] != 0){55: enemiesLeft ++;56: }57: }58: }59:60: var roadHolder:Sprite = new Sprite();//create an object that will hold all parts of the road61: addChild(roadHolder);//add it to the stage62: function makeRoad():void{63: var row:int = 0;//the current row we're working on64: var block;//this will act as the block that we're placing down65: for(var i:int=0;i<lvlArray.length;i++){//creating a loop that'll go through the level array66: if(lvlArray[i] == 0){//if the current index is set to 067: block = new EmptyBlock();//create a gray empty block68: block.graphics.beginFill(0x333333);69: block.graphics.drawRect(0,0,25,25);70: block.graphics.endFill();71: addChild(block);72: //and set the coordinates to be relative to the place in the array73: block.x= (i-row*22)*25;74: block.y = row*25;75: } else if(lvlArray[i] == 1){//if there is supposed to be a row76: //just add a box that will be a darker color and won't have any actions77: block = new Shape();78: block.graphics.beginFill(0x111111);79: block.graphics.drawRect(0,0,25,25);80: block.graphics.endFill();81: block.x= (i-row*22)*25;82: block.y = row*25;83: roadHolder.addChild(block);//add it to the roadHolder84: } else if(lvlArray[i] is String){//if it's a string, meaning a special block85: //then create a special block86: block = new DirectBlock(lvlArray[i],(i-row*22)*25,row*25);87: addChild(block);88: }89: for(var c:int = 1;c<=16;c++){90: if(i == c*22-1){91: //if 22 columns have gone by, then we move onto the next row92: row++;93: }94: }95: }96: }97:98: function makeTurret(xValue:int,yValue:int):void{//this will need to be told the x and y values99: var turret:Turret = new Turret();//creating a variable to hold the Turret100: //changing the coordinates101: turret.x = xValue+12.5;102: turret.y = yValue+12.5;103: addChild(turret);//add it to the stage104: }105:106: addEventListener(Event.ENTER_FRAME, eFrame);//adding an eFrame function107: function eFrame(e:Event):void{108: //if there aren't any levels left109: if(currentLvl > enemyArray.length){110: gameOver=true;//set the game to be over111:112: //reset all the stats113: currentLvl = 1;114: currentEnemy = 0;115: enemyTime = 0;116: enemyLimit = 12;117: enemiesLeft = 0;118:119: removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener120: removeChild(roadHolder);//remove the pieces of road121: gotoAndStop('win');//go to the win frame122: }123: if(lives<=0){//if the user runs out of lives124: gameOver=true;//set the game to be over125:126: //reset all the stats127: currentLvl = 1;128: currentEnemy = 0;129: enemyTime = 0;130: enemyLimit = 12;131: enemiesLeft = 0;132:133: removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener134: removeChild(roadHolder);//remove the pieces of road135: gotoAndStop('lose');//go to the lose frame136: }137: makeEnemies();//we'll just make some enemies138: if(enemiesLeft==0){//if there are no more enemies left139: currentLvl ++;//continue to the next level140: currentEnemy = 0;//reset the amount of enemies there are141: startGame();//restart the game142: }143: //Updating the text fields144: txtLevel.text = 'Level '+currentLvl;145: txtMoney.text = '$'+money;146: txtLives.text = 'Lives: '+lives;147: txtEnemiesLeft.text = 'Enemies Left: '+enemiesLeft;148: }149:150: function makeEnemies():void{//this function will add enemies to the field151: if(enemyTime < enemyLimit){//if it isn't time to make them yet152: enemyTime ++;//then keep on waiting153: } else {//otherwise154: var theCode:int = enemyArray[currentLvl-1][currentEnemy];//get the code from the array155: if(theCode != 0){//if it isn't an empty space156: var newEnemy:Enemy = new Enemy(theCode);//then create a new enemy and pass in the code157: enemyHolder.addChild(newEnemy);//and add it to the enemyholder158: }159: currentEnemy ++;//move on to the next enemy160: enemyTime = 0;//and reset the time161: }162: }163:164: //run these functions at the start165: makeRoad();166: var enemyHolder:Sprite = new Sprite();167: addChild(enemyHolder);168: startGame();
Resultado final:
[flash http://www.flashgametuts.com/obj/tuts/tower-defense-as3/pt7/source.swf w=420 h=420]
No site do pessoal tem mais tutoriais legais, são simples, mas são o alicerce de qualquer jogo. Boa Sorte!