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.

linha_de_forca

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 enter
  8: var finDir:String;//the direction the enemies go when they exit
  9: var startCoord:int;//the coordinates of the beginning of the road
 10: var lvlArray:Array = new Array();//this array will hold the formatting of the roads
 11:
 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 do
 27: var currentLvl:int = 1;
 28: var gameOver:Boolean = false;
 29:
 30: var currentEnemy:int = 0;//the current enemy that we're creating from the array
 31: var enemyTime:int = 0;//how many frames have elapsed since the last enemy was created
 32: var enemyLimit:int = 12;//how many frames are allowed before another enemy is created
 33: var enemyArray:Array = new Array();//this array will tell the function when to create an enemy
 34: var enemiesLeft:int;//how many enemies are left on the field
 35: enemyArray = [//defining the array
 36: [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 created
 37: [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 level
 38: [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 turrets
 45: var lives:int=20;//how many lives the player has
 46:
 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 begins
 53: 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 road
 61: addChild(roadHolder);//add it to the stage
 62: function makeRoad():void{
 63: var row:int = 0;//the current row we're working on
 64: var block;//this will act as the block that we're placing down
 65: for(var i:int=0;i<lvlArray.length;i++){//creating a loop that'll go through the level array
 66: if(lvlArray[i] == 0){//if the current index is set to 0
 67: block = new EmptyBlock();//create a gray empty block
 68: 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 array
 73: block.x= (i-row*22)*25;
 74: block.y = row*25;
 75: } else if(lvlArray[i] == 1){//if there is supposed to be a row
 76: //just add a box that will be a darker color and won't have any actions
 77: 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 roadHolder
 84: } else if(lvlArray[i] is String){//if it's a string, meaning a special block
 85: //then create a special block
 86: 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 row
 92: row++;
 93: }
 94: }
 95: }
 96: }
 97:
 98: function makeTurret(xValue:int,yValue:int):void{//this will need to be told the x and y values
 99: var turret:Turret = new Turret();//creating a variable to hold the Turret
100: //changing the coordinates
101: turret.x = xValue+12.5;
102: turret.y = yValue+12.5;
103: addChild(turret);//add it to the stage
104: }
105:
106: addEventListener(Event.ENTER_FRAME, eFrame);//adding an eFrame function
107: function eFrame(e:Event):void{
108: //if there aren't any levels left
109: if(currentLvl > enemyArray.length){
110: gameOver=true;//set the game to be over
111:
112: //reset all the stats
113: currentLvl = 1;
114: currentEnemy = 0;
115: enemyTime = 0;
116: enemyLimit = 12;
117: enemiesLeft = 0;
118:
119: removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener
120: removeChild(roadHolder);//remove the pieces of road
121: gotoAndStop('win');//go to the win frame
122: }
123: if(lives<=0){//if the user runs out of lives
124: gameOver=true;//set the game to be over
125:
126: //reset all the stats
127: currentLvl = 1;
128: currentEnemy = 0;
129: enemyTime = 0;
130: enemyLimit = 12;
131: enemiesLeft = 0;
132:
133: removeEventListener(Event.ENTER_FRAME, eFrame);//remove this listener
134: removeChild(roadHolder);//remove the pieces of road
135: gotoAndStop('lose');//go to the lose frame
136: }
137: makeEnemies();//we'll just make some enemies
138: if(enemiesLeft==0){//if there are no more enemies left
139: currentLvl ++;//continue to the next level
140: currentEnemy = 0;//reset the amount of enemies there are
141: startGame();//restart the game
142: }
143: //Updating the text fields
144: 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 field
151: if(enemyTime < enemyLimit){//if it isn't time to make them yet
152: enemyTime ++;//then keep on waiting
153: } else {//otherwise
154: var theCode:int = enemyArray[currentLvl-1][currentEnemy];//get the code from the array
155: if(theCode != 0){//if it isn't an empty space
156: var newEnemy:Enemy = new Enemy(theCode);//then create a new enemy and pass in the code
157: enemyHolder.addChild(newEnemy);//and add it to the enemyholder
158: }
159: currentEnemy ++;//move on to the next enemy
160: enemyTime = 0;//and reset the time
161: }
162: }
163:
164: //run these functions at the start
165: 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!

Digg This

Leave A Comment