Escolar Documentos
Profissional Documentos
Cultura Documentos
Para este exerccio, inicialmente vos criar dois arquivos: um arquivo ActionScript 3.0 e um
arquivo ActionScript file.
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.
Converta o segundo retngulo em Movie Clip (F8). Use as opes mostradas na imagem
abaixo.
Usando a ferramenta Oval tool, desenhe uma bola e centralize-a (Ctrl+K) no Palco.
Converta a bola em Movie clip (F8) usando as opes exibidas na imagem abaixo.
No palco, desenhe duas caixas de texto que serviro como placar dos jogadores.
package {
//imports
import
import
import
import
flash.display.MovieClip;
flash.events.KeyboardEvent;
flash.ui.Keyboard;
flash.events.Event;
}
}
//This function is executed when a frame changes
function onEnterFrame(event:Event):void {
//variable decleration
var 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 player
player.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 ball
ball.x += vx;
ball.y += vy;
//moving the computer automatically
if(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 walls
if(ball.y + bHalfHeight >= stage.stageHeight || ball.y - bHalfHeight <= 0) {
vy *= -1;
}
//collision with player and computer
if(ball.x - bHalfWidth <= player.x + pHalfWidth) {
if(Math.abs(ball.y - player.y) <= pHalfHeight) {
vx = ballspeed;
if(v1!=0){
vy = 2*v1;
}
}
}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 scores
if(ball.x + bHalfWidth >= stage.stageWidth) {
playerScore += 1;
reset();
}else if(ball.x - bHalfWidth <= 0) {
computerScore += 1;
reset();
}
//display the score on the textfield
txtPlayer.text = String(playerScore);
txtComputer.text = String(computerScore);
}
}
}