forked from: forked from: forked from: forked from: forked from: forked from: forked from: 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
http://forums.tigsource.com/index.php?topic=9549.0
TIGSource collaborative game expriment #1
♥2 |
Line 221 |
Modified 2009-12-04 03:05:43 |
MIT License
archived:2017-03-09 18:39:04
ActionScript3 source code
/**
* Copyright scarybug ( http://wonderfl.net/user/scarybug )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/9fmr
*/
// http://forums.tigsource.com/index.php?topic=9549.0
// TIGSource collaborative game expriment #1
package {
import flash.display.*;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.text.TextField;
[SWF(width=465, height=465, frameRate=60)]
public class CollabGame extends Sprite {
public var player:Player;
private var enemies:Array;
private var inputArray:Array;
private var drawing_area:Bitmap;
private var trail_image:Shape;
private var updateList:Array;
private var scoreLabel:TextField;
public var score:int = 0;
public var multiplier:int = 1;
public function CollabGame() {
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 {
scoreLabel.text = "LEVEL: "+multiplier+" | SCORE: "+score;
scoreLabel.autoSize = "left";
updateGameEntities();
updateEnemies();
collision();
if(inputArray[37]) {
player.rotation -= 4;
}
if(inputArray[39]) {
player.rotation += 4;
}
if(inputArray[38]) {
player.move();
player.speed = Math.abs(player.speed);
}
if(inputArray[40]) {
player.move();
player.speed = -1 * Math.abs(player.speed);
}
if(inputArray[38] || inputArray[40]){
trail_image.x = player.x;
trail_image.y = player.y;
drawing_area.bitmapData.draw(trail_image, trail_image.transform.matrix);
}
}
private function updateGameEntities() : void {
for(var i:uint=0; i < updateList .length; i++) {
updateList[i].update();
}
}
private function updateEnemies() : void {
var all_flagged:Boolean = true;
for(var i:uint=0; i < enemies.length; i++) {
if(!enemies[i].is_flagged){
all_flagged = false;
}
}
if(all_flagged){
multiplier++;
unflagAll();
}
}
private function unflagAll():void{
drawing_area.bitmapData.fillRect(new Rectangle(0, 0, 465, 465), 0xFFFFFF);
for(var i:uint=0; i < enemies.length; i++) {
enemies[i].unflag();
}
}
private function addGameEntity(entity:GameEntity) : void {
updateList.push(entity);
addChild(entity);
}
private function initGame() : void {
updateList = new Array();
player = new Player();
player.speed = 3;
drawing_area = new Bitmap(new BitmapData(465, 465, false));
addChild(drawing_area);
addGameEntity(player);
trail_image = new Shape();
trail_image.graphics.beginFill(0x00FF00);
trail_image.graphics.drawCircle(0, 0, 2);
trail_image.graphics.endFill();
player.x = 465/2;
player.y = 465/2;
player.rotation = 180;
enemies = new Array();
for(var i:uint=0; i < 15; i++) {
var newEnemy:Enemy = new Enemy(this);
enemies.push(newEnemy);
newEnemy.x = Math.random() * 465;
newEnemy.y = Math.random() * 465;
newEnemy.rotation = Math.random() * 360;
addGameEntity(newEnemy);
}
scoreLabel = new TextField();
scoreLabel.text = "SCORE: 0";
addChild(scoreLabel);
}
}
}
import flash.display.Sprite;
internal class GameEntity extends Sprite {
//x and y velocities
public var vx:Number=0;
public var vy:Number=0;
public var speed:Number = 1;
public function GameEntity (){
}
public function update() : void {
}
public function updateVelocities():void{
vx = speed*Math.sin(Math.PI -this.rotation*0.0174532925);
vy = speed*Math.cos(Math.PI -this.rotation*0.0174532925);
}
public function move():void{
updateVelocities();
x -= (vx);
y -= (vy);
if(y > 470){
y = -10;
}
if(y < -10){
y = 470;
}
if(x > 470){
x = -10;
}
if(x < -10){
x = 470;
}
}
}
internal class Player extends GameEntity {
public function Player(){
graphics.beginFill(0x00ff00);
graphics.drawCircle(0,0,10);
graphics.endFill();
graphics.beginFill(0x0000ff);
graphics.drawRect(-1,0,2,10);
graphics.endFill();
}
}
internal class Enemy extends GameEntity {
public var is_flagged:Boolean = false;
public var turnDeviation:Number = 0;
private var game:CollabGame;
public function Enemy (cg:CollabGame){
super();
game = cg;
speed = 1;
graphics.beginFill(0xff0000);
graphics.drawCircle(0,0,15);
graphics.endFill();
graphics.beginFill(0x0000ff);
graphics.drawRect(-1,0,2,10);
graphics.endFill();
}
public override function update() : void {
turnDeviation += (-1 + (Math.random() * 2));
if(turnDeviation > 4)
turnDeviation = 4;
if(turnDeviation < -4)
turnDeviation = -4;
rotation += turnDeviation;
move();
}
public function flag() : void {
if(!is_flagged){
game.score += game.multiplier;
is_flagged = true;
graphics.clear();
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 += 1;
is_flagged = false
graphics.beginFill(0xff0000);
graphics.drawCircle(0,0,15);
graphics.endFill();
graphics.beginFill(0x0000ff);
graphics.drawRect(-1,0,2,10);
graphics.endFill();
}
}
}