forked from: flash on 2010-3-26
/**
* Copyright blings ( http://wonderfl.net/user/blings )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/1Hf2
*/
// forked from blings's flash on 2010-3-26
package {
import flash.display.*;
import flash.events.Event;
[SWF(width=300, height=450, frameRate=30)]
import flash.text.TextField;
public class FlashTest extends Sprite {
private var player:Player;
private var chance:Number;
private var speed:Number;
private var dungs:Array;
private var dungIndex:int;
private var scoreTxt:TextField;
private var score:uint;
public function FlashTest() {
dungs=new Array();
scoreTxt=new TextField();
player=new Player();
score=0;
addChild(player);
player.y=400;
player.addEventListener(GameEvent.DEAD, onPlayerDead);
chance=0.1;
speed=5;
addEventListener(Event.ENTER_FRAME, onFrame);
addChild(scoreTxt);
}
private function onFrame(e:Event):void
{
if(Math.random()<=chance) {
var d:Dung=new Dung(Math.random()*stage.stageWidth, speed);
d.id=dungs.length;
d.addEventListener(GameEvent.HIT_TEST, checkDungHit);
d.addEventListener(GameEvent.DEAD, dungDied);
addChild(d);
dungs.push(d);
++dungIndex;
}
chance+=0.0005;
speed+=0.05;
score+=chance*50;
scoreTxt.text=String(score);
}
private function checkDungHit(e:Event):void
{
var d:Dung=e.target as Dung;
var px:Number=player.x-d.x;
var py:Number=player.y-d.y;
if(px*px+py*py<=200) {
//d.die();
player.die();
}
}
private function dungDied(e:Event):void
{
var d:Dung=e.target as Dung;
d.removeEventListener(GameEvent.DEAD, dungDied);
d.removeEventListener(GameEvent.HIT_TEST, checkDungHit);
removeChild(d);
dungs.splice(d.id, 1);
}
private function onPlayerDead(e:Event):void
{
removeChild(player);
for(var i:int=0; i<dungs.length; ++i) {
dungs[i].die();
}
removeEventListener(Event.ENTER_FRAME, onFrame);
}
}
}
import flash.display.Sprite;
//this is dung. player must avoid;
class Dung extends Sprite {
private var _speed:Number;
public var id:int;
public function Dung(startX:int, speed:Number) {
this.x=startX;
_speed=speed;
y=-20;
//DRAW DUNG
with(this.graphics) {
beginFill(0xAA6600);
drawCircle(0, 0, 15);
endFill();
}
addEventListener(Event.ENTER_FRAME, onFrame);
}
private function onFrame(e:Event):void
{
y+=_speed;
if(y>500) {
die();
}
dispatchEvent(new Event(GameEvent.HIT_TEST));
}
public function die():void
{
removeEventListener(Event.ENTER_FRAME, onFrame);
dispatchEvent(new Event(GameEvent.DEAD));
}
}
import flash.events.Event;
class GameEvent {
public static const HIT_TEST:String="hitTest";
public static const DEAD:String="dead";
}
//this is player.
import flash.display.Sprite;
import flash.events.Event;
class Player extends Sprite {
private var _accel:Number;
public var health:int;
public var maxSpeed:Number;
public function Player() {
//DRAW PLAYER
with(this.graphics) {
beginFill(0);
drawRect(-10, -10, 20, 20);
}
//initalise;
_accel=0;
maxSpeed=10;
health=20;
addEventListener(Event.ENTER_FRAME, onFrame);
}
private function onFrame(e:Event):void
{
_accel=(this.stage.mouseX-x)*0.3;
//limits _accel;
if(Math.abs(_accel)>=maxSpeed) {
_accel=_accel<0 ? -maxSpeed : maxSpeed;
}
x+=_accel;
if(health<=0) die();
}
public function die():void
{
dispatchEvent(new Event(GameEvent.DEAD));
removeEventListener(Event.ENTER_FRAME, onFrame);
}
}