11
CRIANDO UM JOGO NO ESTILO PONG – ACTIONSCRIPT 3 Para este exercício, inicialmente vãos criar dois arquivos: um arquivo ActionScript 3.0 e um arquivo ActionScript file.

Criando Um Jogo No Estilo Pong

Embed Size (px)

DESCRIPTION

Tutorial Jogo Pong - Flash CS5

Citation preview

Page 1: Criando Um Jogo No Estilo Pong

CRIANDO UM JOGO NO ESTILO PONG – ACTIONSCRIPT 3

Para este exercício, inicialmente vãos criar dois arquivos: um arquivo ActionScript 3.0 e um arquivo ActionScript file.

Page 2: Criando Um Jogo No Estilo Pong

Salve o arquivo ActionScript 3.0 como Pong Game e o arquivo ActionScript file como Main. Mantenha os dois arquivos numa mesma pasta.

Agora vá nas Propriedades do arquivo Pong Game.fla. No campo de texto Document Class insira o nome Main.

Page 3: Criando Um Jogo No Estilo Pong

Desenhe um retângulo de 20x160 (largura e altura). Centralize este retângulo à esquerda do Palco (Ctrl+K). Duplique este retângo (Alt+clique e arraste do mouse) e posicione-o doa outro lado do Palco. Centralize este retângulo à esquerda (Ctrl+K).

Selecione o primeiro retângulo e converta-o em Símbolo (F8). Insira o nome playermc, em Type >> Movie Clip e Registration >> Center. Na aba Advanced marque a opção Export for ActionScript.

Page 4: Criando Um Jogo No Estilo Pong

Em Instance name escreva . playe.

Converta o segundo retângulo em Movie Clip (F8). Use as opções mostradas na imagem abaixo.

Page 5: Criando Um Jogo No Estilo Pong

Em Instance name coloque o nome computer.

Usando a ferramenta Oval tool, desenhe uma bola e centralize-a (Ctrl+K) no Palco.

Page 6: Criando Um Jogo No Estilo Pong

Converta a bola em Movie clip (F8) usando as opções exibidas na imagem abaixo.

Em Instance name coloque o nome ball.

Page 7: Criando Um Jogo No Estilo Pong

Selecione a ferramenta Text tool. Vá em Propriedades e selecione as opções: Text engine: Classic text, Text type: Dynamic text, Fonte: Arial, Tamanho: 40 e cor a sua escolha.

No palco, desenhe duas caixas de texto que servirão como placar dos jogadores.

Salve o seu arquivo.

Selecione o arquivo Main.as e digite o aqrquivo abaixo:

package {//imports

Page 8: Criando Um Jogo No Estilo Pong

import flash.display.MovieClip;import flash.events.KeyboardEvent;import flash.ui.Keyboard;import flash.events.Event;

public class Main extends MovieClip {//constantsconst ballspeed:int = 10;const playerspeed:int = 7;const computerspeed:int = 10;const computerIntelligence:int = 7;//intelligence is 7 out of 10

//global variablesvar vx:int = -ballspeed; // x component of velocity of ball (velocity is speed with direction)var vy:int = ballspeed; // y component of velocity of ballvar v1:int = 0; // initial velocity of playervar v2:int = 0; // initial velocity of computervar playerScore:int = 0;var computerScore:int = 0;

public function Main() {init();

}//this function will add all event listenersfunction init():void {

stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);addEventListener(Event.ENTER_FRAME,onEnterFrame);

}// this function resets the gamefunction reset():void {

player.y = stage.stageHeight/2;computer.y = stage.stageHeight/2;ball.x = stage.stageWidth/2;ball.y = stage.stageHeight/2;if(Math.abs(Math.random()*2) > 1){vx = -ballspeed;}else{vx = ballspeed;}if(Math.abs(Math.random()*2) > 1){vy = -ballspeed;}else{vy = ballspeed;}

}//this function sets the velocity of player when key is pressedfunction onKeyDown(event:KeyboardEvent):void {

if(event.keyCode == Keyboard.UP) {v1 = -playerspeed;

}else if(event.keyCode == Keyboard.DOWN) {v1 = playerspeed;

}}//this function sets the velocity of player to 0 if key is releasedfunction onKeyUp(event:KeyboardEvent):void {

if(event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {

Page 9: Criando Um Jogo No Estilo Pong

v1 = 0;}

}

//This function is executed when a frame changesfunction onEnterFrame(event:Event):void {

//variable declerationvar pHalfHeight = player.height/2; // half height of player(used for collisions)

var pHalfWidth = player.width/2; // half width of player (used for collisions) var bHalfHeight = ball.height/2; // half height of ball(used for collisions) var bHalfWidth = ball.width/2; // half width of ball (used for collisions)

//moving the playerplayer.y += v1;//limiting the motion of player (it should not move beyond the stageheight)if(player.y + pHalfHeight > stage.stageHeight) {

player.y = stage.stageHeight - pHalfHeight;}else if(player.y - pHalfHeight < 0) {

player.y = 0 + pHalfHeight;}

//moving the ballball.x += vx;ball.y += vy;

//moving the computer automaticallyif(Math.abs(Math.random()*10) < computerIntelligence){

var d:int = computer.y - ball.y;if(Math.abs(d) > pHalfHeight){

if(d>0) {v2 = -computerspeed;

}else{v2 = computerspeed;

}}

}computer.y += v2;//limiting the motion of computer (it should not move beyond the stageheight)if(computer.y + pHalfHeight > stage.stageHeight) {

computer.y = stage.stageHeight - pHalfHeight;}else if(computer.y - pHalfHeight < 0) {

computer.y = 0 + pHalfHeight;}

//collision with horizontal wallsif(ball.y + bHalfHeight >= stage.stageHeight || ball.y - bHalfHeight <= 0) {

vy *= -1;}

//collision with player and computerif(ball.x - bHalfWidth <= player.x + pHalfWidth) {

if(Math.abs(ball.y - player.y) <= pHalfHeight) {vx = ballspeed;if(v1!=0){

vy = 2*v1;}}

Page 10: Criando Um Jogo No Estilo Pong

}else if(ball.x + bHalfWidth >= computer.x - pHalfWidth) {if(Math.abs(ball.y - computer.y) <= pHalfHeight) {vx = -ballspeed;if(v2!=0){

vy = v2;}}

}

//collision with vertical walls & updating scoresif(ball.x + bHalfWidth >= stage.stageWidth) {

playerScore += 1;reset();

}else if(ball.x - bHalfWidth <= 0) {computerScore += 1;reset();

}

//display the score on the textfieldtxtPlayer.text = String(playerScore);txtComputer.text = String(computerScore);

}}

}