mardi 11 avril 2017

Button won't work when in next frame

I'm trying to make a reset button for my game but when it takes you to the frame where the button is, the button doesn't do anything when clicked! I'm working on an external actionscript file if that helps.

The codes I have so far:

package  
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.Timer;

public class codes extends MovieClip
{
    public function codes(){
        stop();
        chara.stop();
        stage.addEventListener (KeyboardEvent.KEY_DOWN, keydown);
        stage.addEventListener (KeyboardEvent.KEY_UP, keyup);
        stage.addEventListener (Event.ENTER_FRAME, gameloop);
        gametime.addEventListener(TimerEvent.TIMER, countdown);
        scoring.text=score.toString();
        timer.text=timeleft.toString();
        gametime.start();
        again.addEventListener (MouseEvent.CLICK, startagain);





    }
    var mspeed:Number=0;
    var sy:Number=0;
    var gv:Number=1;
    var jumped:Boolean=false; 
    var score:Number=0;
    var rmessage:Array = ["OK","GOOD", "GREAT", "SUPERB", "FANTASTIC", 
    "PERFECT"]
    var randomize:String;
    var timeleft:Number=20;
    var gametime = new Timer (1000,0);


function keydown (e:KeyboardEvent){
if (e.keyCode==Keyboard.LEFT){
    mspeed=-10;
    chara.gotoAndStop(2);
}
if (e.keyCode==Keyboard.RIGHT){
    mspeed=10;
    chara.gotoAndStop(1);
}
if (e.keyCode==Keyboard.SPACE) {
    if (!jumped){
        sy=-20;
        jumped=true;
    }

    }
    }

    function keyup (e:KeyboardEvent){
    if (e.keyCode==Keyboard.LEFT){
    mspeed=-0;
    }
    if (e.keyCode==Keyboard.RIGHT){

    mspeed=0;
    }

    }

function gameloop (e:Event) {
movechara();
jumpgravity();
pickitems();
exitlvl();
}

function pickitems(){

if (chara.hitTestObject(orb1)) {
    orb1.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    var pb:burst = new burst (this,randomize, chara.X=500,chara.Y=300);

}
    if (chara.hitTestObject(orb2)) {
    orb2.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    new burst (this,randomize, chara.X=500,chara.Y=300);

}
    if (chara.hitTestObject(orb3)) {
    orb3.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    new burst (this,randomize, chara.X=500,chara.Y=300);

}
    if (chara.hitTestObject(orb4)) {
    orb4.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    new burst (this,randomize, chara.X=500,chara.Y=300);
}
    if (chara.hitTestObject(orb5)) {
    orb5.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    new burst (this,randomize, chara.X=500,chara.Y=300);
}
    if (chara.hitTestObject(orb6)) {
    orb6.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    new burst (this,randomize, chara.X=500,chara.Y=300);
}
    if (chara.hitTestObject(orb7)) {
    orb7.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    new burst (this,randomize, chara.X=500,chara.Y=300);
}
    if (chara.hitTestObject(orb8)) {
    orb8.x=2000;
    score++;
    scoring.text=score.toString();
    randomize = rmessage[Math.floor(Math.random() * (rmessage.length-1))];
    new burst (this,randomize, chara.X=500,chara.Y=300);
}
    if(chara.hitTestObject(orb6)){
        key.visible=true;
    }

    if(chara.hitTestObject(key)){
        key.visible=false;
    }

    }

    function movechara (){
    chara.x+=mspeed;

    if (chara.x<0) {
    chara.x=0;
    }
    if (chara.x>1000) {
    chara.x=1000;
    }
   }

   function jumpgravity(){

sy+=gv;
if (!jump.hitTestPoint(chara.x,chara.y,true)) {
    chara.y+=sy;
    }


for (var i=0;i<20;i++) {
    if (jump.hitTestPoint (chara.x, chara.y, true)) {
    chara.y--;
    sy=0;
    jumped=false;
    }
   }

  }

function exitlvl (){
if(chara.hitTestObject(exitdoor)){
if(key.visible==false){
    gotoAndStop(2);
}
}
}


function countdown (e:TimerEvent):void {

timeleft--;
timer.text=timeleft.toString();

if (timeleft<1){
    gotoAndStop(3);
}
}

 function startagain(e:MouseEvent){
    gotoAndPlay(1);

}









}
}

I apologize if my code is a mess, I'm just starting out!

Aucun commentaire:

Enregistrer un commentaire