Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flash.events.TimerEvent;
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- stage.addEventListener(Event.ENTER_FRAME,direcao);
- stage.addEventListener(Event.ENTER_FRAME,cpu);
- var posAcerto:Number;
- var percentCenter:Number;
- var ajusteBola:Number;
- var bolaX:int = 5;
- var bolaY:int = 5;
- var maxBolaX:int = 10;
- var maxBolaY:int = 10;
- var maxVelBola = 10;
- var pontosPlayer:int = 0;
- var pontosCPU:int = 0;
- // TESTA COLISÃO E ALTERA DIREÇÃO DA BOLA
- function direcao(evt:Event){
- if (player.hitTestObject(bola)){
- bolaX *= (-1);
- player.gotoAndPlay(2);
- /*
- posAcerto = bola.y - (player.y+(player.height/2)-bola.height);
- percentCenter = posAcerto/player.height/2;
- ajusteBola = percentCenter * 10;
- */
- /*
- É necessário dividir a barra em pelo menos 3 seções diferentes para
- alterar o ângulo no qual a bola reflete (alterando Y da bola).
- /\
- ||
- || _______
- || _______________CENTRO da barra
- || _______
- ||
- ||
- \/
- Dividindo a barra em três seções:
- player.height/3 (125.4/3) (41.8)
- as divisões da barra então são:
- A = do topo até: player.y - (player.height/2);
- B = meio, pegando 20.9 pra baixo e pra cima, a partir do centro da barra
- C = de player.y + (player.height/2) até a parte inferior da barra;
- if (bola.y < (player.y - (player.height/2))){
- // bolaY tem que diminuir
- }
- if (bola.y >= (player.y - (player.height/2)) && bola.y <= (player.y - (player.height/2))){
- // bolaY mantem igual
- }
- if (bola.y > (player.y + (player.height/2))){
- // bolaY tem que aumentar
- }
- Porcentagem do centro:
- Suponto 125.4 como a altura da barra:
- 125.4 / 100 = [1.254 para 1%]
- percentCenter
- ajusteBola
- posAcerto
- */
- if (bola.y > player.y){
- percentCenter = (bola.y - player.y) * (player.height/100);
- ajusteBola = (percentCenter/10);
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- else
- bolaY += ajusteBola;
- }
- if (bola.y < player.y){
- percentCenter = (bola.y - player.y) * (player.height/100);
- ajusteBola = (percentCenter/10);
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- else
- bolaY += ajusteBola;
- }
- if (bola.y == player.y){
- ajusteBola = 0;
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- else
- bolaY += ajusteBola;
- }
- /*
- if (bola.y > player.y){
- bolaY tem que aumentar
- bola.y - player.y
- 200 - 190 = 10
- 10 * 1.254 = 12.54 (12.54 graus, porque foi perto do centro);
- 200 - 130
- 70 * 1.254 = 87.78 (quase 90 graus, porque bateu na ponta);
- }
- if (bola.y < player.y){
- bolaY tem que diminuir
- player.y - bola.y
- 190 - 200 = -10
- 10 * 1.254 = -12.54;
- 130 - 200 = -70
- -70 * 1.254 = -87.78 (quase 90 graus, porque bateu na ponta);
- }
- if (bola.y == player.y){
- // nao altera
- (porcentagem do centro = 0)
- }
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- // se bolaY < que 15
- else
- bolaY += ajusteBola;
- */
- }
- if (inimigo.hitTestObject(bola)){
- inimigo.gotoAndPlay(2);
- //trace(bola.height);
- bolaX *= (-1);
- /*
- posAcerto = bola.y - (inimigo.y+(player.height/2)-bola.height);
- percentCenter = posAcerto/inimigo.height/2;
- ajusteBola = percentCenter * 10;
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- else
- bolaY += ajusteBola;
- */
- if (bola.y > inimigo.y){
- percentCenter = (bola.y - inimigo.y) * (inimigo.height/100);
- ajusteBola = (percentCenter/10);
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- else
- bolaY += ajusteBola;
- }
- if (bola.y < inimigo.y){
- percentCenter = (bola.y - inimigo.y) * (inimigo.height/100);
- ajusteBola = (percentCenter/10);
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- else
- bolaY += ajusteBola;
- }
- if (bola.y == inimigo.y){
- ajusteBola = 0;
- if (bolaY >= maxVelBola){
- bolaY = maxVelBola;
- }
- else
- bolaY += ajusteBola;
- }
- }
- // BOLA SAINDO //
- if (bola.x < 5){
- bola.x = 225;
- bola.y = 200;
- bolaY = 5;
- //trace("PONTO DA CPU!");
- pontosCPU++;
- }
- if (bola.x > 545){
- bola.x = 225;
- bola.y = 200;
- bolaY = 5;
- //trace("PONTO DO JOGADOR!");
- pontosPlayer++;
- }
- if (bola.y <= 0){
- bolaY *= (-1);
- }
- if (bola.y >= 400){
- bolaY *= (-1);
- }
- // MOVIMENTA BOLA //
- if (bolaX > maxBolaX){
- bolaX = maxBolaX;
- }
- bola.x += bolaX;
- if (bolaY > maxBolaY){
- bolaY = maxBolaY;
- }
- bola.y += bolaY;
- // movimenta player de acordo com a posição do mouse
- var velMax:Number = 7;
- var playerDelta:Number = player.y - mouseY;
- var movePlayer:Number = Math.min(Math.abs(playerDelta),velMax);
- if (playerDelta < 0){
- player.y += movePlayer;
- }
- else {
- player.y -= movePlayer;
- }
- // placares
- placarPlayer.text = pontosPlayer.toString();
- placarCPU.text = pontosCPU.toString();
- }
- function cpu(evt:Event):void{
- var pontoDeToque:Number = Math.random() % inimigo.height;
- var velMaxima:Number = 6;
- // Obtem a distancia que a barra se move para atingir a bola
- var bolaDelta:Number = inimigo.y + pontoDeToque - bola.y;
- // Computa a quantidade para mover a bola, ou o maximo que pode-se
- // tentar para atingí-la.
- var moveInimigo:Number = Math.min(Math.abs(bolaDelta), velMaxima);
- if (bolaDelta < 0){
- inimigo.y += moveInimigo;
- }
- else {
- inimigo.y -= moveInimigo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment