Created
August 27, 2012 10:25
-
-
Save miguelSantirso/3487248 to your computer and use it in GitHub Desktop.
Super simple arkanoid in Flex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 NUM_BLOCK_ROWS:int = 5; | |
var NUM_BLOCK_COLS:int = 10; | |
var BALL_RADIUS:int = 6; | |
var INITIAL_BALL_SPEED = 5; | |
var BLOCK_FIELD_Y = 50; | |
var BLOCK_GUTTER_X = 2; | |
var BLOCK_GUTTER_Y = 2; | |
var PADDLE_HEIGHT:int = 15; | |
var PADDLE_WIDTH:int = 50; | |
var PADDLE_DIST_FROM_BOTTOM:int = 25; | |
// Sprites | |
private var blocks:Array = new Array(0); | |
private var ball:Sprite = new Sprite(); | |
private var paddle:Sprite = new Sprite(); | |
// Variables del estado del juego | |
private var ball_dx:Number; | |
private var ball_dy:Number; | |
private var ball_speed:Number; | |
private var lives:int = 3; | |
// Insertar gráficos en el archivo .swf (solo funciona en Flex) | |
[Embed(source="graficos/block1.jpg")] | |
private var GoldBlock:Class; | |
[Embed(source="graficos/block1.jpg")] | |
private var RedBlock:Class; | |
[Embed(source="graficos/block1.jpg")] | |
private var BlueBlock:Class; | |
// Meter las imagenes en un array para acceder más facilmente | |
private var allBlocks:Array = new Array(GoldBlock, RedBlock, BlueBlock); | |
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 | |
var reference_block:DisplayObject = new GoldBlock(); | |
var block_field_width:int = NUM_BLOCK_COLS * reference_block.width | |
+ (NUM_BLOCK_COLS-1)*BLOCK_GUTTER_X; | |
var block_field_x:int = (width - block_field_width) / 2; | |
var current_block_index:int = 0; | |
// Create and position the blocks | |
for (var x:int = 0; x < NUM_BLOCK_COLS; x++) | |
{ | |
for (var y:int = 0; y < NUM_BLOCK_ROWS; y++) | |
{ | |
// Create a new Block object | |
var current_image:Class = allBlocks[current_block_index]; | |
var sprite:DisplayObject = new current_image(); | |
// 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 | |
var ticker = new Timer(10); | |
ticker.addEventListener(TimerEvent.TIMER, onTick); | |
ticker.start(); | |
} | |
public function onTick(evt:TimerEvent):void | |
{ | |
// 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 | |
var new_ball_dx:Number = (ball.x - paddle.x) / (PADDLE_WIDTH/2.0) | |
* 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 | |
for (var i:int = 0; i < blocks.length; i++) | |
{ | |
var block:DisplayObject = blocks[i]; | |
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 | |
var ball_distance_norm_x:int = (ball.x - block.x) / block.width; | |
var ball_distance_norm_y:int = (ball.y - block.y) / block.height; | |
// 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 | |
if (ball.x - BALL_RADIUS < 0) ball_dx = Math.abs(ball_dx); | |
if (ball.x + BALL_RADIUS > width) ball_dx = -Math.abs(ball_dx); | |
if (ball.y - BALL_RADIUS < 0) ball_dy = Math.abs(ball_dy); | |
// 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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment