Chapter 1: The basics
Chapter 2: Structure
Chapter 3: Graphics and collisions
Next chapter should be available by: 20.09.2011
Chapter 2: Structure
Chapter 3: Graphics and collisions
Next chapter should be available by: 20.09.2011
package{
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
public class Ball extends Sprite{
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE,go);
}
}}
private const RADIUS:int = 12;
private const COLOR:uint = 0x01A6B2;
graphics.lineStyle(2, COLOR, 1);
graphics.beginFill(COLOR);
graphics.drawCircle(0, 0, RADIUS);
ball = new Ball();
addChild(ball);
ball.x = stage.stageWidth * .5;
ball.y = stage.stageHeight * .5;
private var ball:Ball;
private const COLOR2:uint = 0x45FCFF;
private const BEVEL:BevelFilter = new BevelFilter(4, 90, COLOR2, 1, COLOR2, 1, 10, 10, 1, 1, BitmapFilterType.INNER, true);
private const GLOW:GlowFilter = new GlowFilter(0xFFFFFF, .6, 0, 0, 5, 1, true);
filters = [BEVEL, GLOW];
stage.addEventListener(KeyboardEvent.KEY_DOWN, releaseBall);
private function releaseBall(e:KeyboardEvent):void{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, releaseBall);
addEventListener(Event.ENTER_FRAME, enterFrame);
}
private var speedX:Number;
private var speedY:Number;
private var angle:Number;
private var ballSpeed:int=5;
angle = Math.random() > .5?90:270;
private function enterFrame(e:Event):void {
speedX = Math.cos(angle * (Math.PI / 180)) * ballSpeed;
speedY = Math.sin(angle * (Math.PI / 180)) * ballSpeed;
ball.y += speedY;
ball.x += speedX;
}
private function ballHit(target:DisplayObject):Boolean {
if (ball.x + speedX >= target.x) {
if (ball.x + speedX <= target.x + target.width) {
return true;
}
}
return false;
}
if (speedY > 0 ) {
if ((paddle.y - ball.y) <= speedY && paddle.y >= ball.y) {
if (ballHit(paddle)) {
angle += 180;
ball.y = paddle.y - 1 - ball.height * .5;
ballSpeed += 1;
}
}
}
else if ((ball.y - ball.height*.5) -speedY < 0) {
angle-= 180;
ball.y = 0 + ball.height * .5;
}
package {
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class MainMenu extends Sprite{
private var pongButton:Sprite;
public function MainMenu():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
pongButton = item("Play", 20, 30, launchGame, 0xFF0000);
addChild(pongButton);
}
private function launchGame(e:MouseEvent):void {
}
private function item(buttonText:String, X:int, Y:int, Funct:Function, txtColor:uint = 0xFFFFFF):Sprite {
var item:Sprite = new Sprite();
item.graphics.beginFill(0);
item.graphics.lineStyle(1, txtColor, .5);
item.graphics.drawRect(0, 0, 250, 30);
var myText:TextField = new TextField();
myText.selectable = false;
myText.width = 250;
myText.height = 30;
item.addChild(myText);
myText.autoSize = "center";
myText.text = buttonText;
myText.textColor = txtColor;
item.addEventListener(MouseEvent.CLICK, Funct);
item.x = X;
item.y = Y;
return item;
}
}}
package {
import flash.display.Sprite;
import flash.events.Event;
public class PongGame extends Sprite{
private var paddle:Paddle; public function PongGame():void { addEventListener(Event.ADDED_TO_STAGE, go); } private function go(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, go); paddle = new Paddle();
addChild(paddle); } }
}
private function buildMenu():void {
menu = new MainMenu();
addChild(menu);
}
package { import flash.events.Event; public class CustomEvents extends Event{ public static const LAUNCH_GAME:String = "launch_game";
public function CustomEvents(e:String):void { super(e); } }
}
menu.addEventListener(CustomEvents.LAUNCH_GAME, startGame, false, 0, true);
dispatchEvent(new CustomEvents(CustomEvents.LAUNCH_GAME));
private var game:PongGame;
private function startGame(e:CustomEvents):void { removeChild(menu); menu.removeEventListener(CustomEvents.LAUNCH_GAME, startGame); menu = null; game = new PongGame(); addChild(game); }
addEventListener(Event.REMOVED_FROM_STAGE, die);
private function die(e:Event):void { removeEventListener(Event.REMOVED_FROM_STAGE, die); pongButton.removeEventListener(MouseEvent.CLICK, launchGame); removeChild(pongButton); pongButton = null; }
import flash.stuff.stuff;
/*This allows us to use external classes in our code.
FD auto imports the required classes, so don't worry about it for now.*//** Some text... */
/*The parts between /* */ are comments. They don't affect our code but are really useful. Commenting out your code is a good practice. */ public class Main extends Sprite {public function Main():void {code
}
}
/*We have a class called 'Main', which extends the base class 'Sprite'. We'll get into details later. The function with the name of our class is the constructor. Its code will run as soon as an instance of the class is created.*/
(cute, isn't it?)package{ public class Assets { [Embed (source = '../lib/paddle.png')] public static const Pad:Class; public function Assets() : void { } }}
package{import flash.display.Bitmap;import flash.display.Sprite; public class Paddle extends Sprite { private var pic:Bitmap = new Assets.Pad(); public function Paddle():void{ } }}
public function Paddle() : void { addEventListener(Event.ADDED_TO_STAGE, go);}Listeners allow our objects to respond to a determined situation.
private function go(e:Event) : void { removeEventListener(Event.ADDED_TO_STAGE, go); //We don't need our object to listent to that anymore, se we remove it. addChild(pic); }
package{import flash.display.Bitmap;import flash.display.Sprite;import flash.events.Event; public class Paddle extends Sprite { private var pic:Bitmap = new Assets.Pad(); public function Paddle() : void { addEventListener(Event.ADDED_TO_STAGE, go); } private function go(e:Event) : void { removeEventListener(Event.ADDED_TO_STAGE, go); addChild(pic); } }}
private var paddle:Paddle;
addChild(paddle);
package {
import flash.display.Sprite; import flash.events.Event; /** * ... * @author Senekis */ public class Main extends Sprite { private var paddle:Paddle; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point paddle = new Paddle(); addChild(paddle); } }}
y = stage.stageHeight - pic.height;x = stage.stageWidth * .5 - pic.width * .5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
private function keyDownHandler(e:KeyboardEvent):void { if (e.keyCode == 37 || e.keyCode == 65) { trace('Paddle is moving left'); } }
private var movingLeft:Boolean;
if (!movingLeft) {
movingLeft = true;
}
addEventListener(Event.ENTER_FRAME, enterFrame, false, 0, true);
private function enterFrame(e:Event):void { if (movingLeft) { x -= speed; } }
private var speed:int = 10;
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);
private function keyUpHandler(e:KeyboardEvent):void { if (e.keyCode == 37 || e.keyCode == 65) { if (movingLeft) { movingLeft = false; } }
private function enterFrame(e:Event):void { if (movingLeft) { if (x - speed >= 0) { x -= speed; } else { x = 0; } }
private function keyDownHandler(e:KeyboardEvent):void { if (e.keyCode == 37 || e.keyCode == 65) { if (!movingLeft) { movingLeft = true; } } else if (e.keyCode == 39 || e.keyCode == 68) { if (!movingRight) { movingRight = true; } } }
private var movingRight:Boolean;
private function keyUpHandler(e:KeyboardEvent):void { if (e.keyCode == 37 || e.keyCode == 65) { if (movingLeft) { movingLeft = false; } } else if (e.keyCode == 39 || e.keyCode == 68) { if (movingRight) { movingRight = false; } } }
private function enterFrame(e:Event):void { if (movingLeft) { if (x - speed >= 0) { x -= speed; } else { x = 0; } } else if (movingRight) { if (x + speed + pic.width <= 600) { x += speed; } else { x = 600 - pic.width; } } }
private function die(e:Event):void { removeChild(pic); pic = null; stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler); removeEventListener(Event.ENTER_FRAME, enterFrame); removeEventListener(Event.REMOVED_FROM_STAGE, die); }
addEventListener(Event.REMOVED_FROM_STAGE, die, false, 0, true);