Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- //import flash.display.DisplayObject;
- import flash.display.*;
- import flash.events.*;
- import flash.geom.Rectangle;
- import flash.media.SoundChannel;
- import flash.media.Sound;
- public class char extends MovieClip {
- var abletojump:Boolean;
- var speed:Number = 7;
- var jumpspeed:Number = 0;
- var jumpheight:Number = 25;
- var fall:Number = 1;
- var maxgravity:Number = 15;
- var platforms:Number = 100;
- var jump:Boolean = true;
- var charx:Number;
- var chary:Number;
- var charw:Number;
- var charh:Number;
- var i:Number;
- var plat:DisplayObject;
- var door:DisplayObject;
- var ladder:DisplayObject;
- var ladderTop:DisplayObject;
- var hazard:DisplayObject;
- var chest:DisplayObject;
- var direction:String = "right";
- var startMc:startBtn = new startBtn();
- var climbing:Boolean = false;
- var alive:Boolean = true;
- var sfxChannel:SoundChannel = new SoundChannel();
- var jumpSound:Sound = new jumpStart();
- var landSound:Sound = new land();
- var clickSound:Sound = new click();
- var cashSound:Sound = new cash();
- var completeSound:Sound = new complete();
- var failSound:Sound = new failure();
- var chestCollected:Boolean = false;
- public function char():void {
- this.addEventListener(Event.ADDED_TO_STAGE, initiate);
- }
- function initiate(e:Event):void{
- stage.addEventListener(TouchEvent.TOUCH_BEGIN, levelStart);
- (root as MovieClip).addChild(startMc);
- startMc.x = 540;
- startMc.y = 960;
- this.gotoAndStop(1);
- }
- function levelStart(e:TouchEvent):void{
- sfxChannel = clickSound.play();
- stage.removeEventListener(TouchEvent.TOUCH_BEGIN, levelStart);
- (root as MovieClip).removeChild(startMc);
- addEventListener(Event.ENTER_FRAME, theenter);
- stage.addEventListener(TouchEvent.TOUCH_BEGIN, jumpHandler);
- }
- function theenter(evt:Event):void {
- charx = this.x;
- chary = this.y;
- charw = this.width;
- charh = this.height;
- this.x += speed;
- this.y += fall;
- if (direction == "right" && climbing == false) {
- if(abletojump == true){
- this.gotoAndStop("runRight");
- }
- speed = 7;
- }
- else if (direction == "left" && climbing == false) {
- if(abletojump == true){
- this.gotoAndStop("runLeft");
- }
- speed = -7;
- }
- if (climbing == true){
- fall = -5;
- abletojump = false;
- speed = 0;
- }
- if (abletojump == false && climbing == false && alive == true){
- this.gotoAndStop("jump" + direction)
- }
- if (fall>0) {
- abletojump = false;
- }
- if(climbing == false){
- fall++;
- }
- if (fall >= maxgravity) {
- fall = maxgravity;
- }
- if (jump == true) {
- if (jumpspeed > 0) {
- jumpspeed--;
- fall = 0;
- this.y -= jumpspeed;
- }
- }
- else {
- jumpspeed = jumpheight;
- }
- for (i = 0; i <= (root as MovieClip).levelContainer.numChildren -1; i++) {
- if((root as MovieClip).levelContainer.getChildAt(i).name == "finalDoor"){
- door = (root as MovieClip).levelContainer.getChildAt(i);
- if(this.hitTestObject(door) && abletojump == true && door.x + 5 > charx && door.x - 5 < charx && alive == true){
- levelComplete();
- }
- }
- if((root as MovieClip).levelContainer.getChildAt(i).name == "chest"){
- chest = (root as MovieClip).levelContainer.getChildAt(i);
- if(this.hitTestObject(chest) && abletojump == true && chest.x + chest.width/2 > charx && chest.x - chest.width/2 < charx && alive == true && chestCollected == false){
- (chest as MovieClip).play();
- chestCollected = true;
- sfxChannel = cashSound.play();
- }
- }
- if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "p"){
- plat = (root as MovieClip).levelContainer.getChildAt(i);
- }
- if (plat is MovieClip) {
- if (this.hitTestObject(plat) && climbing == false) {
- //if char bounces left
- if(charx + charw/2 < plat.x && chary + charh/2 > plat.y - plat.height/2 + 10 && chary - charh/2 < plat.y + plat.height/2 - 10){
- direction = "left";
- }
- //if char bounces right
- if(charx - charw/2 > plat.x && chary + charh/2 > plat.y - plat.height/2 + 10 && chary - charh/2 < plat.y + plat.height/2 - 10){
- direction = "right";
- }
- //land on top
- if(chary + charh/2 < plat.y - plat.height/2 + 10 && charx > plat.x - plat.width/2 && charx < plat.x + plat.width/2){
- if(alive == false){
- death();
- }
- if(alive == true && fall > 14){
- sfxChannel = landSound.play();
- }
- this.y = plat.y - plat.height / 2 - charh / 2;
- jumpspeed = jumpheight;
- fall = 0;
- jump = false;
- abletojump = true;
- }
- //hit from underneath
- if(chary - charh/2 > plat.y + plat.height/2 - 10 && charx > plat.x - plat.width/2 && charx < plat.x + plat.width/2 ){
- this.y = plat.y + plat.height / 2 + charh / 2;
- jumpspeed *= -1;
- jump = false;
- }
- }
- }
- if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "T"){
- ladderTop = (root as MovieClip).levelContainer.getChildAt(i);
- if(this.hitTestObject(ladderTop) && climbing == true && chary > ladderTop.y){
- climbing = false;
- }
- }
- if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "b"){
- hazard = (root as MovieClip).levelContainer.getChildAt(i);
- if(alive == true && this.hitTestObject(hazard) && charx < hazard.x + hazard.width/2 && charx > hazard.x - hazard.width/2 && chary < hazard.y + hazard.height/2 && chary > hazard.y - hazard.height/2){
- alive = false;
- sfxChannel = failSound.play();
- if(direction == "right"){
- this.gotoAndStop("dieLeft");
- }
- if(direction == "left"){
- this.gotoAndStop("dieRight");
- }
- }
- }
- else if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "l"){
- ladder = (root as MovieClip).levelContainer.getChildAt(i);
- if (ladder is MovieClip) {
- if (this.hitTestObject(ladder) && charx < ladder.x + 5 && charx > ladder.x - 5 && chary + charh/2 > ladder.y - ladder.height/2 + 5 && alive == true) {
- climbing = true;
- this.x = ladder.x;
- this.gotoAndStop("climb");
- }
- }
- }
- }
- }
- function jumpHandler(e:TouchEvent):void {
- if (abletojump == true) {
- sfxChannel = jumpSound.play();
- jump = true;
- abletojump = false;
- }
- }
- function death():void{
- removeEventListener(Event.ENTER_FRAME, theenter);
- stage.removeEventListener(TouchEvent.TOUCH_BEGIN, jumpHandler);
- (root as MovieClip).targetFrame = "deathScreen";
- (root as MovieClip).fadeMaker();
- }
- function levelComplete():void{
- removeEventListener(Event.ENTER_FRAME, theenter);
- stage.removeEventListener(TouchEvent.TOUCH_BEGIN, jumpHandler);
- sfxChannel = completeSound.play();
- this.gotoAndStop("stand" + direction);
- if(chestCollected == true){
- (root as MovieClip).mySharedObject.data.cash += 10;
- (root as MovieClip).mySharedObject.data.chestsOpened[(root as MovieClip).currentLevel -1] = 1;
- (root as MovieClip).mySharedObject.flush();
- }
- if((root as MovieClip).currentLevel == (root as MovieClip).mySharedObject.data.highestLevel){
- (root as MovieClip).mySharedObject.data.highestLevel += 1;
- (root as MovieClip).mySharedObject.flush();
- }
- (root as MovieClip).targetFrame = "Complete";
- (root as MovieClip).fadeMaker();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement