forked from: forked from: forked from: forked from: forked from: forked from: forked from: forked from: forked from: forked from: flash on 2009-12-3
TIGSource collaborative game expriment #1
/**
* Copyright toastie ( http://wonderfl.net/user/toastie )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/56yP
*/
// forked from toastie's forked from: forked from: forked from: forked from: forked from: forked from: forked from: forked from: forked from: flash on 2009-12-3
// forked from scarybug's forked from: forked from: forked from: forked from: forked from: forked from: forked from: forked from: flash on 2009-12-3
// forked from toastie's forked from: forked from: forked from: forked from: forked from: forked from: forked from: flash on 2009-12-3
// forked from scarybug's forked from: forked from: forked from: forked from: forked from: forked from: flash on 2009-12-3
// forked from toastie's forked from: forked from: forked from: forked from: forked from: flash on 2009-12-3
// forked from toastie's forked from: forked from: forked from: forked from: flash on 2009-12-3
// forked from fucrate's forked from: forked from: forked from: flash on 2009-12-3
// forked from toastie's forked from: forked from: flash on 2009-12-3
// forked from scarybug's forked from: flash on 2009-12-3
// forked from toastie's flash on 2009-12-3
// TIGSource collaborative game expriment #1
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
[SWF(width=465, height=465, frameRate=60)]
public class FlashTest extends Sprite {
private var player:Sprite;
private var enemies:Array;
private var inputArray:Array;
private var player_speed:Number;
public function FlashTest() {
initInput();
initGame();
}
private function initInput() : void {
inputArray = new Array();
for(var i:uint =0;i < 256; i++) {
inputArray.push(false);
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
this.addEventListener(Event.ENTER_FRAME, updateGame);
}
public function onKeyDown(e:KeyboardEvent) : void {
inputArray[e.keyCode] = true;
}
public function onKeyUp(e:KeyboardEvent) : void {
inputArray[e.keyCode] = false;
}
public function collision():void{
for each(var enemy:Enemy in enemies)
{
if(enemy.hitTestPoint(player.x, player.y))
{
enemy.flag();
}
}
}
public function updateGame(e:Event) : void {
updateEnemies();
collision();
if(inputArray[37]) {
player.rotation -= 4;
}
if(inputArray[39]) {
player.rotation += 4;
}
if(inputArray[38]) {
player.x -= Math.sin(Math.PI -player.rotation*0.0174532925);
player.y -= Math.cos(Math.PI -player.rotation*0.0174532925);
}
//go backwards
if(inputArray[40]) {
player.x += Math.sin(Math.PI -player.rotation*0.0174532925);
player.y += Math.cos(Math.PI -player.rotation*0.0174532925);
}
if(inputArray[37] || inputArray[38] || inputArray[39] || inputArray[40]){
graphics.beginFill(0x00ff00);
graphics.drawCircle(player.x,player.y,2);
graphics.endFill();
}
}
private function updateEnemies() : void {
var all_flagged:Boolean = true;
for(var i:uint=0; i < enemies.length; i++) {
enemies[i].move();
if(!enemies[i].is_flagged){
all_flagged = false;
}
if(enemies[i].y > 470)
enemies[i].y = -10;
if(enemies[i].y < -10)
enemies[i].y = 470;
if(enemies[i].x > 470)
enemies[i].x = -10;
if(enemies[i].x < -10)
enemies[i].x = 470;
}
if(all_flagged){
unflagAll();
}
}
private function unflagAll():void{
player_speed += 0.0174532925;
for(var i:uint=0; i < enemies.length; i++) {
enemies[i].unflag();
}
}
private function initGame() : void {
player = cratePlayerGraphic();
player_speed = 0.0174532925;
addChild(player);
player.x = 465/2;
player.y = 465/2;
player.rotation = 180;
enemies = new Array();
for(var i:uint=0; i < 15; i++) {
var newEnemy:Sprite = new Enemy(this);
enemies.push(newEnemy);
addChild(newEnemy);
newEnemy.x = Math.random() * 465;
newEnemy.y = Math.random() * 465;
newEnemy.rotation = Math.random() * 360;
}
}
private function cratePlayerGraphic() : Sprite {
var retSprite:Sprite = new Sprite();
retSprite.graphics.beginFill(0x00ff00);
retSprite.graphics.drawCircle(0,0,10);
retSprite.graphics.endFill();
retSprite.graphics.beginFill(0x0000ff);
retSprite.graphics.drawRect(-1,0,2,10);
retSprite.graphics.endFill();
return retSprite;
}
}
}
import flash.display.Sprite;
internal class Enemy extends Sprite {
//x and y velocities
public var vx:Number=0;
public var vy:Number=0;
public var is_flagged:Boolean = false;
public var turnDeviation:Number = 0;
private var speed:Number;
private var parentSprite:Sprite;
public function Enemy (parentSprite:Sprite){
this.parentSprite = parentSprite;
super();
speed = 0.0174532925;
graphics.beginFill(0xff0000);
graphics.drawCircle(0,0,15);
graphics.endFill();
graphics.beginFill(0x0000ff);
graphics.drawRect(-1,0,2,10);
graphics.endFill();
}
public function updateVelocities():void{
vx = Math.sin(Math.PI -this.rotation*0.0174532925);
vy = Math.cos(Math.PI -this.rotation*0.0174532925);
}
public function move():void{
turnDeviation += (-1 + (Math.random() * 2));
if(turnDeviation > 4)
turnDeviation = 4;
if(turnDeviation < -4)
turnDeviation = -4;
rotation += turnDeviation;
updateVelocities();
x-=vx;
y-=vy;
parentSprite.graphics.beginFill(0xff0000);
parentSprite.graphics.drawCircle(x,y,2);
parentSprite.graphics.endFill();
}
public function flag() : void {
if(!is_flagged){
is_flagged = true;
graphics.beginFill(0x0000ff);
graphics.drawCircle(0,0,15);
graphics.endFill();
this.scaleX -=0.1;
this.scaleY -=0.1;
graphics.beginFill(0x00ff00);
graphics.drawRect(-1,0,2,10);
graphics.endFill();
}
}
public function unflag() : void {
if(is_flagged){
speed += 0.0174532925;
is_flagged = false
graphics.beginFill(0xff0000);
graphics.drawCircle(0,0,15);
graphics.endFill();
graphics.beginFill(0x0000ff);
graphics.drawRect(-1,0,2,10);
graphics.endFill();
}
}
}