Sunday, January 9, 2011

Grav's source code part II

Here's the last part: gameScreen.as.

Oh, and by the way: work on the new game is progressing, but rather slowly. I keep running into errors and mistakes, and sometimes it just behaves in completely nonsensical ways... I guess I only have myself and my inexperience to blame. But then I look at this code and I realize I already learned a lot compared to when I wrote this. :)


edit: So, blogger ruined the layout, but if you just paste it into your favorite actionscript editor and autoformat it, there'll be no problem, right?


package asfiles{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import com.newgrounds.API;
import com.newgrounds.APIEvent;
import com.newgrounds.Medal;
import asfiles.*;
public class gameScreen extends Sprite {
var Player;
private var theItem:Shape=new Shape ;
private var sidebalk:Sprite=new Sprite ;
private var gravity:Number=3;
private var xspeed:Number=0;
private var yspeed:Number=0;
private var maxspeed:Number=15;
private var xpos:Number=200;
private var ypos:Number=200;
private var rotateto:Number;
private var rotatespeed:Number;
private var left:Number=15;
private var top:Number=15;
private var right:Number=385;
private var bottom:Number=385;
private var jumped:Boolean=false;
private var split:String="horizontal";
private var timer:Timer=new Timer(10000,0);
private var itemTimer:Timer=new Timer(3000,0);
private var endTimer:Timer=new Timer(60000,1);
private var showTime:Timer=new Timer(1000,60);
private var blocka:Shape=new Shape ;
private var blockb:Shape=new Shape ;
private var gravitydirection1:Number;
private var gravitydirection2:Number;
private var inblocka:Boolean=false;
private var inblockb:Boolean=false;
private var itemscought:Number=0;
private var itemCombo:Number=0;
private var bestcombo:Number=0;
private var score:Number=0;
private var timeboard:TextField=new TextField ;
private var scoreboard:TextField=new TextField ;
private var comboboard:TextField = new TextField
private var bestcomboboard:TextField=new TextField
private var timeleft:int=60;
private var timeFont:TextFormat=new TextFormat ;
private var scoreFont:TextFormat=new TextFormat ;
private var multiplierFont:TextFormat=new TextFormat ;
private var multipliers:Array=new Array ;
private var oldx:Number;
private var oldy:Number;
private var bloopsound:*;
private var bloopchannel:SoundChannel;
private var timelefttext:TextField = new TextField
private var timeleftfont:TextFormat = new TextFormat
var medals:Array=API.getMedals();
private var unlockfired:Boolean=false;
public function gameScreen() {
super();
MakePlayer();
MakeSidebalk();
MakeItem();
Player.addEventListener(Event.ADDED_TO_STAGE,playerToStage);
bloopsound = new bloopwav();
}
public function playerToStage(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,playerMovement);
addEventListener(Event.ENTER_FRAME,GravityMovement,false);
timer.start();
timer.addEventListener(TimerEvent.TIMER,gravCreateTimer);
itemTimer.start();
itemTimer.addEventListener(TimerEvent.TIMER,moveItemTimer);
endTimer.start();
endTimer.addEventListener(TimerEvent.TIMER,endListener);
showTime.start();
showTime.addEventListener(TimerEvent.TIMER,showTimefunc);
gravCreate();
}
public function GravityMovement(evt:Event) {
// calculate the movement of the player
if (! (Player.hitTestObject(blocka)&&Player.hitTestObject(blockb))) {//don't turn in the middle
if (Player.rotation!=rotateto) {
if (Player.rotation<0) { Player.rotation+=360; } else if (Player.rotation>=360) {
Player.rotation-=360;
}
if (rotateto==0) {//zk omlaag
if (Player.rotation>=180) {
Player.rotation+=360-Player.rotation/3;
} else {
Player.rotation-=Player.rotation/3;
}
}
if (rotateto==90) {//zk links
if (Player.rotation<90) { Player.rotation+=(90-Player.rotation)/3; } else if (Player.rotation>=270) {
Player.rotation+=450-Player.rotation/3;
} else if (Player.rotation>90&&Player.rotation<270) { Player.rotation-=Player.rotation-90/3; } } if (rotateto==180) {//zk omhoog if (Player.rotation>180) {
Player.rotation-=(Player.rotation-180)/3;
} else if (Player.rotation<180) { Player.rotation+=(180-Player.rotation)/3; } } if (rotateto==270) {// zk rechts if (Player.rotation>=270) {
Player.rotation-=(Player.rotation-270)/3;
}
if (Player.rotation<90) { Player.rotation-=(Player.rotation+90)/3; } if (Player.rotation<270&&Player.rotation>=90) {
Player.rotation+=(Player.rotation-90)/3;
}
}
}
}
xpos+=xspeed;
ypos+=yspeed;
if (xpos>right) {
xpos=right;
xspeed=0;
} else if (xposbottom) {
ypos=bottom;
yspeed=0;
jumped=false;
} else if (ypos < top) { ypos=top; yspeed=0; } if (Player.hitTestObject(blocka)) { switch (gravitydirection1) { case 0 : xspeed-=.5; rotateto=90; break; case 1 : yspeed-=.5; rotateto=180; break; case 2 : xspeed+=.5; rotateto=270; break; case 3 : yspeed+=.5; rotateto=0; break; } inblocka=true; } if (Player.hitTestObject(blockb)) { switch (gravitydirection2) { case 0 : xspeed-=.5; rotateto=90; break; case 1 : yspeed-=.5; rotateto=180; break; case 2 : xspeed+=.5; rotateto=270; break; case 3 : yspeed+=.5; rotateto=0; break; } inblockb=true; } if (Player.hitTestObject(blocka)&&Player.hitTestObject(blockb)) { if (split=="horizontal"&&gravitydirection1==0&&gravitydirection2==2||split=="vertical"&&gravitydirection1==3&&gravitydirection2==1) { Player.rotation-=10; if (Player.rotation<0) { Player.rotation+=360; } inblocka=false; inblockb=false; } else if (split=="horizontal"&&gravitydirection1==2&&gravitydirection2==0||split=="vertical"&&gravitydirection1==1&&gravitydirection2==3) { Player.rotation+=10; if (Player.rotation) { Player.rotation-=360; } inblocka=false; inblockb=false; } } if (Player.hitTestObject(theItem)) { oldx=theItem.x; oldy=theItem.y; moveItem(); itemscought++; itemCombo++; if (itemCombo>bestcombo) {
bestcombo=itemCombo;
}
score+=itemCombo;
scoreboard.text=String("Score: "+score);
scoreboard.setTextFormat(scoreFont);
comboboard.text=String("Combo: " + itemCombo)
comboboard.setTextFormat(scoreFont)
bestcomboboard.text=String("Best combo: "+bestcombo)
bestcomboboard.setTextFormat(scoreFont)
bestcomboboard.wordWrap=true
multipliers[itemscought]=new TextField ;
multipliers[itemscought].text="X"+String(itemCombo);
floatmultiplier(multipliers[itemscought]);
bloopchannel=bloopsound.play();
}
Player.x=xpos;
Player.y=ypos;
if ((!medals[0].unlocked)&&score>=300) {
if (! unlockfired==true) {
unlockfired=true;
API.unlockMedal("300 points!");
}
}
}
public function playerMovement(e:KeyboardEvent) {
// listen for keyboard input
switch (e.keyCode) {
case 37 :
case 65 :
if (xspeed- maxspeed) {
if (inblocka==true&&gravitydirection1==3||inblockb==true&&gravitydirection2==3) {
yspeed-=6;
} else {
yspeed-=3;
}
}
break;
case 39 :
case 68 :
if (xspeed>- maxspeed) {
if (inblocka==true&&gravitydirection1==0||inblockb==true&&gravitydirection2==0) {
xspeed+=6;
} else {
xspeed+=3;
}
}
break;
case 40 :
case 83 :
if (yspeed<=maxspeed) { if (inblocka==true&&gravitydirection1==1||inblockb==true&&gravitydirection2==1) { yspeed+=6; } else { yspeed+=3; } } break; } } public function gravCreate() { //create the gravityfields if (Math.random()>.5) {
split="horizontal";
} else {
split="vertical";
}
blocka.graphics.clear();
blockb.graphics.clear();
gravityDirection();
if (split=="horizontal") {
blocka.graphics.drawRect(0,0,400,200);
blockb.graphics.drawRect(0,200,400,200);
} else if (split=="vertical") {
blocka.graphics.drawRect(0,0,200,400);
blockb.graphics.drawRect(200,0,200,400);
}
}
private function gravCreateTimer(e:TimerEvent) {
gravCreate();
}
private function moveItemTimer(e:TimerEvent) {
moveItem();
itemCombo=0;
}
private function gravityDirection() {
gravitydirection1=Math.random()*4;
gravitydirection2=Math.random()*4;
if (gravitydirection1<1) { gravitydirection1=0; blocka.graphics.beginFill(0xFA4141,.25); trace("red"); } else if (gravitydirection1<2&&gravitydirection1>=1) {
gravitydirection1=1;
blocka.graphics.beginFill(0xFAF91F,.25);
trace("yellow");
} else if (gravitydirection1<3&&gravitydirection1>2) {
gravitydirection1=2;
blocka.graphics.beginFill(0x214DF8,.25);
trace("blue");
} else {
gravitydirection1=3;
blocka.graphics.beginFill(0x23F523,.25);
trace("green");
}
if (gravitydirection2<1) { gravitydirection2=0; blockb.graphics.beginFill(0xFA4141,.25); } else if (gravitydirection2<2&&gravitydirection2>=1) {
gravitydirection2=1;
blockb.graphics.beginFill(0xFAF91F,.25);
} else if (gravitydirection2<3&&gravitydirection2>2) {
gravitydirection2=2;
blockb.graphics.beginFill(0x214DF8,.25);
} else {
gravitydirection2=3;
blockb.graphics.beginFill(0x23F523,.25);
}
if (gravitydirection1==gravitydirection2) {
gravityDirection();//make sure both field dont have the same direction of gravity
}
}
public function moveItem() {
itemTimer.reset();
itemTimer.start();
theItem.x=Math.random()*390+5;//don't go over the edges
theItem.y=Math.random()*390+5;
}
public function floatmultiplier(mtp) {
var multip;
multip=mtp;
multip.x=oldx;
multip.y=oldy;
multip.selectable=false;
multip.setTextFormat(multiplierFont);
var floattimer:Timer=new Timer(150,15);
floattimer.start();
addChild(multip);
floattimer.addEventListener(TimerEvent.TIMER,floatlistener);
floattimer.addEventListener(TimerEvent.TIMER_COMPLETE,floatstop);
function floatlistener(e:TimerEvent) {
multip.alpha-=0.07;
multip.y-=0.5;
}
function floatstop(e:TimerEvent) {
removeChild(multip);
}
}
public function MakePlayer() {
Player=new Shape ;
Player.graphics.beginFill(0x000000,1);
Player.graphics.drawCircle(0,0,15);
Player.graphics.beginFill(0xFFFFFF,1);
Player.graphics.drawCircle(0,7,3);
Player.graphics.drawCircle(-6,-5.5,2);
Player.graphics.drawCircle(6,-5.5,2);
Player.x=xpos;
Player.y=ypos;
addChild(Player);
addChild(blocka);
addChild(blockb);
}
public function MakeSidebalk() {
sidebalk.graphics.beginFill(0x000000,1);
sidebalk.graphics.drawRect(400,0,150,400);
scoreboard.width=150;
scoreboard.height=150;
scoreboard.x=412;
scoreboard.y=125;
scoreboard.textColor=0xFFFFFF;
scoreboard.text=String("Score: "+score);
comboboard.width=150
comboboard.height=150
comboboard.x=412
comboboard.y=175
comboboard.textColor=0xFFFFFF
comboboard.text=String("Combo: "+itemCombo)
bestcomboboard.width=150
bestcomboboard.height=150
bestcomboboard.x=412
bestcomboboard.y=225
bestcomboboard.textColor=0xFFFFFF
bestcomboboard.text=String("Best combo: "+bestcombo)
bestcomboboard.wordWrap=true
timeboard.width=100;
timeboard.height=75;
timeboard.x=425;
timeboard.y=25;
timeboard.textColor=0xFFFFFF;
timeboard.text=String(timeleft);
timeboard.autoSize=TextFieldAutoSize.LEFT;
timeboard.wordWrap=true;
timelefttext.x=425
timelefttext.y=4
timelefttext.text="Time Left:"
timelefttext.autoSize=TextFieldAutoSize.LEFT;
timeFont.font="arial";
timeFont.size="70";
timeFont.color=0xFFFFFF;
scoreFont.font="arial";
scoreFont.size="25";
scoreFont.color=0xFFFFFF;
timeleftfont.font = "arial"
timeleftfont.size = "20"
timeleftfont.color=0xFFFFFF
timeboard.setTextFormat(timeFont);
scoreboard.setTextFormat(scoreFont);
comboboard.setTextFormat(scoreFont)
bestcomboboard.setTextFormat(scoreFont)
timelefttext.setTextFormat(timeleftfont)
scoreboard.selectable=false;
timeboard.selectable=false;
timelefttext.selectable = false
addChild(sidebalk);
sidebalk.addChild(scoreboard);
sidebalk.addChild(timeboard);
sidebalk.addChild(timelefttext)
sidebalk.addChild(comboboard)
sidebalk.addChild(bestcomboboard)
}
public function MakeItem() {
theItem.graphics.beginFill(0xFFFFFF,1);
theItem.graphics.drawCircle(0,0,7);
theItem.graphics.beginFill(0x000000,1);
theItem.graphics.drawCircle(0,0,5);
theItem.graphics.beginFill(0x24367D);
theItem.graphics.drawCircle(0,0,3);
multiplierFont.font="arial";
multiplierFont.size="25";
multiplierFont.color=0x000000;
addChild(theItem);
moveItem();
}
public function showTimefunc(e:Event) {
timeleft--;
timeboard.text=String(timeleft);
timeboard.setTextFormat(timeFont);
}
public function endListener(event:TimerEvent) {
theEnd();
}
public function theEnd() {
//the end
timer.stop();
itemTimer.stop();
endTimer.stop();
removeEventListener(KeyboardEvent.KEY_DOWN,playerMovement);
removeEventListener(Event.ENTER_FRAME,GravityMovement,false);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,playerMovement);
removeChild(Player);
removeChild(sidebalk);
removeChild(blocka);
removeChild(blockb);
removeChild(theItem);
var gameover=new Gameover(score,bestcombo);
addChild(gameover);
}
}
}

No comments:

Post a Comment