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
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="pack.*" layout="absolute" creationComplete="initApp()" width="640" height="480">
- <mx:Script>
- public function initApp():void
- {
- canvas.init();
- }
- </mx:Script>
- <Arkanoid id="canvas" width="100%" height="100%" themeColor="#ffffff" />
- </mx:Application>
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
- package pack{
- import flash.display.*;
- import flash.events.*;
- import flash.utils.*;
- import mx.core.UIComponent;
- public class Arkanoid extends UIComponent
- {
- // Definir los parámetros del juego
- var INITIAL_BALL_SPEED = 5;
- var BLOCK_FIELD_Y = 50;
- var BLOCK_GUTTER_X = 2;
- var BLOCK_GUTTER_Y = 2;
- // Sprites
- // Variables del estado del juego
- // Insertar gráficos en el archivo .swf (solo funciona en Flex)
- [Embed(source="graficos/block1.jpg")]
- [Embed(source="graficos/block1.jpg")]
- [Embed(source="graficos/block1.jpg")]
- // Meter las imagenes en un array para acceder más facilmente
- public function init():void
- {
- // set up ball
- ball.graphics.beginFill(0xee00ee);
- ball.graphics.drawCircle(0, 0, BALL_RADIUS);
- ball.graphics.endFill();
- addChild(ball);
- resetBall(); // places the ball in a starting location
- // set up paddle sprite
- paddle.graphics.beginFill(0x000000);
- paddle.graphics.drawRect(-PADDLE_WIDTH/2, -PADDLE_HEIGHT/2, PADDLE_WIDTH, PADDLE_HEIGHT);
- paddle.graphics.endFill();
- paddle.y = height - PADDLE_DIST_FROM_BOTTOM;
- addChild(paddle);
- // Figure out some metrics for placing the blocks
- + (NUM_BLOCK_COLS-1)*BLOCK_GUTTER_X;
- // Create and position the blocks
- {
- {
- // Create a new Block object
- // Create sprite, position it, and add it to the display list.
- sprite.x = block_field_x + x * (reference_block.width + BLOCK_GUTTER_X);
- sprite.y = BLOCK_FIELD_Y + y * (reference_block.height + BLOCK_GUTTER_Y);
- addChild(sprite);
- // Add the Block to our list
- blocks.push(sprite);
- // This piece of code causes us to cycle through the available block images.
- current_block_index++;
- if (current_block_index >= allBlocks.length)
- current_block_index = 0;
- }
- }
- // Create a Timer object, tell it to call our onTick method, and start it
- ticker.start();
- }
- {
- // Place the paddle (based on mouse location)
- paddle.x = mouseX;
- // Check paddle collision with ball
- if (ball.hitTestObject(paddle))
- {
- // Adjust X momentum of ball based on where on the paddle we hit
- * INITIAL_BALL_SPEED;
- // Blend this new momentum with the old
- ball_dx = (ball_dx + new_ball_dx) / 2;
- ball_dy = -ball_dy;
- }
- // Check collision with every block
- {
- if (block == null) continue;
- // Check collision
- if (ball.hitTestObject(block))
- {
- // We collided, remove this sprite
- removeChild(block);
- blocks[i] = null;
- // Figure out which way the ball should bounce
- // Don't bounce in a direction that the ball is already going
- if (ball_distance_norm_x * ball_dx > 0)
- ball_dy = -ball_dy;
- else if (ball_distance_norm_y * ball_dy > 0)
- ball_dx = -ball_dx;
- // If we get here then either bounce is valid, pick one based on ball loc
- else if (ball_distance_norm_x > ball_distance_norm_y)
- ball_dx = -ball_dx;
- else
- ball_dy = -ball_dy;
- // Increase ball speed by 2.5% to make things interesting!
- ball_dx *= 1.025;
- ball_dy *= 1.025;
- }
- }
- // Check collision with sides of screen
- // Check if ball was lost
- if (ball.y - BALL_RADIUS > height)
- {
- if (lives > 0) lives -= 1;
- resetBall();
- // todo: check for game over
- }
- // Move ball
- ball.x += ball_dx;
- ball.y += ball_dy;
- }
- public function resetBall():void
- {
- ball.x = .25 * width;
- ball.y = .5 * height;
- ball_dx = INITIAL_BALL_SPEED * .70;
- ball_dy = INITIAL_BALL_SPEED * .70;
- }
- }
- }
Enviado por miguelSantirso hace over 2 years — modificado por última vez hace less than a minute