package
{ // start package
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip
{ // start public class Main
var speed:Number = 5;
var handChar:HandChar = new HandChar(); // define MovieClip "handChar"
var leftKeyDown:Boolean = false; // move left
var upKeyDown:Boolean = false; // jump
var rightKeyDown:Boolean = false; // move right
var hKeyDown:Boolean = false; // scissor attack
var jKeyDown:Boolean = false; // paper attack
var kKeyDown:Boolean = false; // rock attack
var mainJumping:Boolean = false;
var jumpSpeedLimit:int = 10;
var jumpSpeed:Number = 0;
public function Main()
{ // start public function Main (constructor)
addEventListener(Event.ENTER_FRAME, moveHand);
addEventListener(Event.ENTER_FRAME, boundaries);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
handChar.handCharobject.addEventListener (Event.ENTER_FRAME, endKeyDown);
createHand();
} // end public function Main (constructor)
private function createHand():void
{ // start private function createHand
handChar.x = 200;
handChar.y = 145;
handChar.scaleX = 1;
handChar.scaleX = 1;
addChild(handChar);
} // end private function createHand
private function endKeyDown (event:Event):void
{
if(handChar.handCharobject.currentLabel == "endofclip")
{
hKeyDown = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
}
if(handChar.handCharobject.currentLabel == "endofclip")
{
jKeyDown = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
}
if(handChar.handCharobject.currentLabel == "endofclip")
{
kKeyDown = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
}
}
private function checkKeysDown (event:KeyboardEvent):void
{ // start private function checkKeysDown
if(event.keyCode == 37 || event.keyCode == 65)
{
leftKeyDown = true;
}
if(event.keyCode == 38 || event.keyCode == 87)
{
upKeyDown = true;
}
if(event.keyCode == 39 || event.keyCode == 68)
{
rightKeyDown = true;
}
if(event.keyCode == 72)
{
hKeyDown = true;
stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
}
if(event.keyCode == 74)
{
jKeyDown = true;
stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
}
if(event.keyCode == 75)
{
kKeyDown = true;
stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
}
} // end private function checkKeysDown
private function checkKeysUp (event:KeyboardEvent):void
{ // start private function checkKeysUp
if(event.keyCode == 37 || event.keyCode == 65) // Right Arrow or D key
{
leftKeyDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87) // Up Arrow or W key
{
upKeyDown = false;
}
if(event.keyCode == 39 || event.keyCode == 68) // Left Arrow or A key
{
rightKeyDown = false;
}
} // end private function checkKeysUp
private function moveHand (e:Event) : void
{ // start private function moveHand
var idle:Boolean = true;
if(upKeyDown)
{
if (!mainJumping)
{
handChar.gotoAndStop(5);
jumpSpeed=jumpSpeedLimit;
mainJumping = true;
}
idle = false;
}
if(leftKeyDown && !mainJumping)
{
handChar.x -= speed;
handChar.scaleX = -1;
handChar.gotoAndPlay(1);
idle = false;
}
if(rightKeyDown && !mainJumping)
{
handChar.x += speed;
handChar.scaleX = 1;
handChar.gotoAndPlay(1);
idle = false;
}
if(hKeyDown)
{
handChar.gotoAndStop("scissor");
idle = false;
}
if(jKeyDown)
{
handChar.gotoAndStop("paper");
idle = false;
}
if(kKeyDown)
{
handChar.gotoAndStop("rock");
idle = false;
}
if(mainJumping)
{
if(leftKeyDown)
{
handChar.x -= speed;
handChar.scaleX = -1;
}
if(rightKeyDown)
{
handChar.x += speed;
handChar.scaleX = 1;
}
idle = false;
}
if(idle)handChar.gotoAndPlay(3);
handChar.y -= jumpSpeed;
jumpSpeed -= 2;
while(Ground.hitTestPoint(handChar.x, handChar.y-3, true))
{
handChar.y -= 1;
jumpSpeed = 0;
mainJumping = false;
}
} // end private function Movehand
private function boundaries (e:Event):void
{ // start private function boundaries
if(handChar.x > stage.stageWidth - handChar.width/2)
{
handChar.x = stage.stageWidth - handChar.width/2;
}
if(handChar.x < 15)
{
handChar.x = 15;
}
} // end private function boundaries
}
} // end package