简体   繁体   中英

Flash As3 Variable Issue

I have a variable on frame 1 that starts at 3000 and keeps decreasing. This variable determines the gap of time between the spawn of an object. so basically the objects spawn quicker and quicker as the this number decreases. Everything works fine except the fact when i make the character die and the main timeline goes to frame 2 to show the death screen. When clicked "play again" on that frame that makes you come back to frame 1. the objects keep spawning at whatever speed they were spawning before death. Even though the variable resets to 3000 when "play again" clicked..

I traced the variable and it does truely reset. but for some reason those spawning objects follow the previous variable.

Heres the code on the first Frame:

import flash.events.Event;

stop(); 

var num1:Number = 0;
var scrollSpeed:Number = 3000;
var playerState:Number = 1;
var alive:Boolean = true;
var Redbars:Array = new Array();
var Bluebars:Array = new Array();

var scoreCount:Number = 10;

addEventListener(Event.ENTER_FRAME, update);



function update(e:Event){

trace(scrollSpeed);

scoreCount += 1;
scoreText.text = (scoreCount).toString();

//SCROLL SPEED-------------------

if(scrollSpeed > 300)
{
scrollSpeed -= 1;
}
//--------------------------------

CheckForCollisionBlue();
CheckForCollisionRed();

//trace(alive);

if(player.currentFrame == 1){
    playerState = 1;
}


//Check if lost
if(player.hitTestObject(leftHitBox)){
    gotoAndStop(2);
    scoreEnd.text = (scoreCount).toString();
}


//If dead Delete Object
if(currentFrame == 2){
        deleteBlue();
        deleteRed();

}




}



////
//Timer////////////////////////////////


var myTimer:Timer = new Timer(scrollSpeed,10000000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{


//Generate Random number
num1 = randomRange();   
trace(num1);


//IF NUM1 = 1------------------------------------------
if(num1 == 1){

//Create a Red Bar
var redBar = new Jump_Cube();
Redbars.push(redBar); //add enemy to the array
redBar.x = -33;
redBar.y = 99;
redBar.width = 12.5;
redBar.height = 20.45;

//Update the Bar
for(var i:int=0; i < Redbars.length; i++){
    if(currentFrame == 1)
{
    addChild(Redbars[i]);
}
}       
}


//IF NUM1 = 2------------------------------------------
if(num1 == 2){

//Create a Blue Bar
var blueBar = new Jump_Cube2();
Bluebars.push(blueBar); //add enemy to the array
blueBar.x = -26.8;
blueBar.y = 10;
blueBar.width = 12.25;
blueBar.height = 31.90;

//Update the Bar
for(var j:int=0; j < Bluebars.length; j++){
        if(currentFrame == 1)
        {
    addChild(Bluebars[j]);
        }
       }

myTimer.delay = scrollSpeed;

}



}
myTimer.start();



//--------------------------------------------------------------------

//Check for Collision------------------------------
function CheckForCollisionBlue(){
for(var i:int=0; i < Bluebars.length; i++){
        if( player.hitTestObject(Bluebars[i]) ){
            //Collision
            trace("Didnt Hit Blue");
            player.x -= 5;
            player.y += 1;
        }
}
}

//Check for Collision------------------------------
function CheckForCollisionRed(){
for(var i:int=0; i < Redbars.length; i++){
        if( player.hitTestObject(Redbars[i]) ){
            //Collision
            trace("Didnt Hit Red");
            player.x -= 5;
            player.y += 1;
        }
 }
}












function randomRange():Number 
{
return (Math.floor(Math.random() * (2 - 1 + 1)) + 1);
}

///////////BUTTONS------------------
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;



redButton.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchEndRed);
function onTouchEndRed(e:TouchEvent):void{
player.gotoAndPlay(2);
playerState = 2;
}

blueButton.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchEndBlue);
function onTouchEndBlue(e:TouchEvent):void{
player.gotoAndPlay(19);
playerState = 3;
}




///////--CLEARING STAGE
//--------------------------------------------------------------------

//delete Blue------------------------------
function deleteBlue(){
for(var i:int=0; i < Bluebars.length; i++){
        removeChild(Bluebars[i]);
        }

}

//delete Red------------------------------
function deleteRed(){
for(var i:int=0; i < Redbars.length; i++){
       removeChild(Redbars[i]);
        }

 }

Heres the code on the second frame:

stop();

alive = false;

againButton.addEventListener(TouchEvent.TOUCH_END, again);
function again(e:TouchEvent):void{
gotoAndStop(1);
playerState = 1;

scrollSpeed = 3000;

deleteBlue();
deleteRed();


}

You have to put in a removeEventListener(Event.ENTER_FRAME, update); inside your onTouchEndRed function. And probably also inside of this 'if' conditional: if(player.hitTestObject(leftHitBox)){...

Simply stop programming using frames. Use classes instead. AS3 is based on classes. It'll take you a lot of benefits. This link may help you to start: http://www.adobe.com/devnet/actionscript/getting_started.html

Additionally, FlashPlayer executes frame's code each time you jump to another frame. Thats why your variables are reinitializing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM