¿Eres nuevo? ¡Lee el FAQ y ponte al día!
2k visitas

Arkanoid súper sencillo en AS 3.0. Compilable con mxmlc (compilador Flex)

1. Archivo mxml

Lo primero es crear un archivo arkanoid.mxml que indicará al compilador lo que tiene que compilar y el punto de entrada del programa:

MXML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="pack.*" layout="absolute" creationComplete="initApp()" width="640" height="480">
  3.  
  4.     <mx:Script>
  5.  
  6.            public function initApp():void
  7.            {
  8.                canvas.init();
  9.            }
  10.  
  11.    </mx:Script>
  12.  
  13. <Arkanoid id="canvas" width="100%" height="100%" themeColor="#ffffff" />    
  14. </mx:Application>
  15.  

­

2. Archivo .as

Lo siguiente es crear el archivo Arkanoid.as. Es importante crearlo en una carpeta llamada pack que esté en el mismo directorio que el archivo Arkanoid.mxml.

ActionScript 3
  1. package pack{
  2.     import flash.display.*;
  3.     import flash.events.*;
  4.     import flash.utils.*;
  5.  
  6.     import mx.core.UIComponent;
  7.  
  8.     public class Arkanoid extends UIComponent
  9.     {
  10.         // Definir los parámetros del juego
  11.         var NUM_BLOCK_ROWS:int = 5;
  12.         var NUM_BLOCK_COLS:int = 10;
  13.  
  14.         var BALL_RADIUS:int    = 6;
  15.         var INITIAL_BALL_SPEED = 5;
  16.  
  17.         var BLOCK_FIELD_Y      = 50;
  18.         var BLOCK_GUTTER_X     = 2;
  19.         var BLOCK_GUTTER_Y     = 2;
  20.  
  21.         var PADDLE_HEIGHT:int  = 15;
  22.         var PADDLE_WIDTH:int   = 50;
  23.         var PADDLE_DIST_FROM_BOTTOM:int = 25;
  24.  
  25.         // Sprites
  26.         private var blocks:Array = new Array(0);
  27.         private var ball:Sprite = new Sprite();
  28.         private var paddle:Sprite = new Sprite();
  29.  
  30.         // Variables del estado del juego      
  31.         private var ball_dx:Number;
  32.         private var ball_dy:Number;
  33.         private var ball_speed:Number;
  34.         private var lives:int = 3;
  35.  
  36.         // Insertar gráficos en el archivo .swf (solo funciona en Flex)
  37.         [Embed(source="graficos/block1.jpg")]
  38.         private var GoldBlock:Class;
  39.         [Embed(source="graficos/block1.jpg")]
  40.         private var RedBlock:Class;
  41.         [Embed(source="graficos/block1.jpg")]
  42.         private var BlueBlock:Class;
  43.  
  44.         // Meter las imagenes en un array para acceder más facilmente
  45.         private var allBlocks:Array = new Array(GoldBlock, RedBlock, BlueBlock);
  46.  
  47.         public function init():void
  48.         {
  49.  
  50.             // set up ball
  51.             ball.graphics.beginFill(0xee00ee);
  52.             ball.graphics.drawCircle(0, 0, BALL_RADIUS);
  53.             ball.graphics.endFill();
  54.             addChild(ball);
  55.             resetBall();            // places the ball in a starting location
  56.  
  57.             // set up paddle sprite
  58.             paddle.graphics.beginFill(0x000000);
  59.             paddle.graphics.drawRect(-PADDLE_WIDTH/2, -PADDLE_HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT);
  60.             paddle.graphics.endFill();
  61.             paddle.y = height - PADDLE_DIST_FROM_BOTTOM;
  62.             addChild(paddle);
  63.  
  64.             // Figure out some metrics for placing the blocks
  65.             var reference_block:DisplayObject = new GoldBlock();
  66.  
  67.             var block_field_width:int = NUM_BLOCK_COLS * reference_block.width
  68.                 + (NUM_BLOCK_COLS-1)*BLOCK_GUTTER_X;
  69.             var block_field_x:int = (width - block_field_width) / 2;
  70.  
  71.             var current_block_index:int = 0;
  72.  
  73.             // Create and position the blocks
  74.             for (var x:int = 0; x < NUM_BLOCK_COLS; x++)
  75.             {
  76.                 for (var y:int = 0; y < NUM_BLOCK_ROWS; y++)
  77.                 {
  78.                     // Create a new Block object
  79.                     var current_image:Class = allBlocks[current_block_index];
  80.                     var sprite:DisplayObject = new current_image();
  81.  
  82.                     // Create sprite, position it, and add it to the display list.
  83.                     sprite.x = block_field_x + x * (reference_block.width + BLOCK_GUTTER_X);
  84.                     sprite.y = BLOCK_FIELD_Y + y * (reference_block.height + BLOCK_GUTTER_Y);
  85.  
  86.                     addChild(sprite);
  87.  
  88.                     // Add the Block to our list
  89.                     blocks.push(sprite);
  90.  
  91.                     // This piece of code causes us to cycle through the available block images.
  92.                     current_block_index++;
  93.                     if (current_block_index >= allBlocks.length)
  94.                         current_block_index = 0;
  95.                 }
  96.             }
  97.  
  98.             // Create a Timer object, tell it to call our onTick method, and start it
  99.             var ticker = new Timer(10);
  100.             ticker.addEventListener(TimerEvent.TIMER, onTick);
  101.             ticker.start();
  102.         }
  103.  
  104.         public function onTick(evt:TimerEvent):void    
  105.         {
  106.             // Place the paddle (based on mouse location)
  107.             paddle.x = mouseX;
  108.  
  109.             // Check paddle collision with ball
  110.             if (ball.hitTestObject(paddle))
  111.             {
  112.                 // Adjust X momentum of ball based on where on the paddle we hit
  113.                 var new_ball_dx:Number = (ball.x - paddle.x) / (PADDLE_WIDTH/2.0)
  114.                     * INITIAL_BALL_SPEED;
  115.  
  116.                 // Blend this new momentum with the old
  117.                 ball_dx = (ball_dx + new_ball_dx) / 2;
  118.                 ball_dy = -ball_dy;
  119.             }
  120.  
  121.             // Check collision with every block
  122.             for (var i:int = 0; i < blocks.length; i++)
  123.             {
  124.                 var block:DisplayObject = blocks[i];
  125.                 if (block == null) continue;
  126.  
  127.                 // Check collision
  128.                 if (ball.hitTestObject(block))
  129.                 {
  130.                     // We collided, remove this sprite
  131.                     removeChild(block);
  132.                     blocks[i] = null;
  133.  
  134.                     // Figure out which way the ball should bounce
  135.                     var ball_distance_norm_x:int = (ball.x - block.x) / block.width;
  136.                     var ball_distance_norm_y:int = (ball.y - block.y) / block.height;
  137.  
  138.                     // Don't bounce in a direction that the ball is already going
  139.                     if (ball_distance_norm_x * ball_dx > 0)
  140.                         ball_dy = -ball_dy;
  141.                     else if (ball_distance_norm_y * ball_dy > 0)
  142.                         ball_dx = -ball_dx;
  143.  
  144.                     // If we get here then either bounce is valid, pick one based on ball loc
  145.                     else if (ball_distance_norm_x > ball_distance_norm_y)
  146.                         ball_dx = -ball_dx;
  147.                     else
  148.                         ball_dy = -ball_dy;
  149.  
  150.                     // Increase ball speed by 2.5% to make things interesting!
  151.                     ball_dx *= 1.025;
  152.                     ball_dy *= 1.025;
  153.                 }
  154.             }
  155.  
  156.             // Check collision with sides of screen
  157.             if (ball.x - BALL_RADIUS < 0) ball_dx = Math.abs(ball_dx);
  158.             if (ball.x + BALL_RADIUS > width) ball_dx = -Math.abs(ball_dx);
  159.             if (ball.y - BALL_RADIUS < 0) ball_dy = Math.abs(ball_dy);
  160.  
  161.             // Check if ball was lost
  162.             if (ball.y - BALL_RADIUS > height)
  163.             {
  164.                 if (lives > 0) lives -= 1;
  165.                 resetBall();
  166.                 // todo: check for game over
  167.             }
  168.  
  169.             // Move ball
  170.             ball.x += ball_dx;
  171.             ball.y += ball_dy;
  172.  
  173.         }
  174.  
  175.         public function resetBall():void
  176.         {
  177.             ball.x = .25 * width;
  178.             ball.y = .5 * height;
  179.             ball_dx = INITIAL_BALL_SPEED * .70;
  180.             ball_dy = INITIAL_BALL_SPEED * .70;
  181.         }
  182.     }
  183. }
  184.  

­

Etiquetas: ActionScript 3.0 MXML Simple arkanoid flex

Insertar: