如何撤消或禁用我在前一帧中使用的代码?
我一直在使用Event.ENTER_FRAME
,我知道我可以使用stage.removeEventListener
撤消代码,但我的代码有点太难了,我不是一个很好的编码器,所以也许有一个简单的代码禁用前一帧中的所有代码,这可能吗?
这是我的代码:
stop();
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.TweenMax;
import com.greensock.TweenLite;
import flash.events.Event;
var myTween:TweenLite;
stage.addEventListener(MouseEvent.CLICK, rijden); // Add the button click
function rijden(e:MouseEvent):void {
trace('start car timer');
stage.removeEventListener(MouseEvent.CLICK, rijden); // unplug "rijden" handler
stage.addEventListener(MouseEvent.CLICK, spring); // Add the button click
var randNum:Number;
randNum = Math.round(Math.random() * 5 + 2000);
trace(randNum);
var timer:Timer = new Timer(randNum);
timer.addEventListener(TimerEvent.TIMER, randGen);
function randGen(e:TimerEvent):void {
var timerObj:Timer = e.target as Timer;
randNum = Math.round(Math.random() * 5 + 2000);
myTween = TweenLite.to(auto, 4, {x:666.15, y:375.6});
timerObj.delay = randNum;
trace('shootCar');
timer.stop();
}
timer.start();
}
var grav:Number = 7.5;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(Event.ENTER_FRAME, update);
function spring(e:MouseEvent):void {
trace('spring');
stage.removeEventListener(MouseEvent.CLICK, spring); // unplug "spring" handler
stage.addEventListener(MouseEvent.CLICK, rijden);
if(jumping != true){
TweenLite.to(man, 0.5, {rotation:360});
jumpPow = -50;
jumping = true;
}
}
function update(evt:Event):void{
if(jumping){
removeEventListener(Event.ENTER_FRAME, update);
man.y += jumpPow;
jumpPow += grav;
if(man.y >= 375){
jumping = false;
man.y = 375;
}
}
}
var autoWasHitByMan=false;
var manWasHitByEind=false;
var autoWasHitByStopauto=false;
addEventListener(Event.ENTER_FRAME, hit);
function hit(e:Event){
if(auto.hitTestObject(man) && !autoWasHitByMan){
trace("auto hits man");
autoWasHitByMan=true;
TweenLite.to(man, 4, {x:539.95, y:145, rotation:360});
this.setChildIndex(level, this.numChildren - 1);
TweenMax.to(level, 1, {scaleX:1, scaleY:3, ease:Elastic.easeOut});
this.setChildIndex(facebook, this.numChildren - 1);
var timer:Timer = new Timer(2000, 1); // delay = time between ticks in milliseconds
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
timer.start();
function onTimerComplete(e:TimerEvent):void
{
// remove listener
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
removeEventListener(Event.ENTER_FRAME, hit);
var myTween4:TweenMax = new TweenMax(level, 1, {scaleX:0, scaleY:0, ease:Linear.easeOut});// advance playhead to frame 2
gotoAndStop(2);
facebook.addEventListener(MouseEvent.CLICK, shareClickHandler);
function shareClickHandler(evt:MouseEvent):void
{
var varsShare:URLVariables = new URLVariables();
varsShare.u = 'https://play.google.com/store/apps/details?id=com.ea.games.nfs13_row';
varsShare.t = 'Try to beat me if you can';
var urlFacebookShare:URLRequest = new URLRequest('http://www.facebook.com/sharer.php');
urlFacebookShare.data = varsShare;
urlFacebookShare.method = URLRequestMethod.GET;
navigateToURL(urlFacebookShare, '');
}
removeEventListener(Event.ENTER_FRAME, shareClickHandler);
}
}
if(man.hitTestObject(eind) && !manWasHitByEind){
trace("man hits eind");
manWasHitByEind=true;
man.x = 241.3;
man.y = 375;
}
if(auto.hitTestObject(stopauto) && !autoWasHitByStopauto){
trace("auto hits stopauto");
autoWasHitByStopauto=true;
myTween.kill();
auto.x = 241;
auto.y = 375;
}
if(auto.hitTestObject(nextlevel) && autoWasHitByMan==false && lives > 1 ){
trace("boeoboeobo")
stage.removeEventListener(MouseEvent.CLICK, spring);
stage.removeEventListener(MouseEvent.CLICK, rijden);
stage.removeEventListener(Event.ENTER_FRAME, update);
stage.removeEventListener(Event.ENTER_FRAME, hit);
"this is the plce where the hit event has to be stoped I think because I get an error in the next level."
gotoAndPlay(1, "Scène 2")
}
}
答案 0 :(得分:0)
switch(currentLabel)
{
case frameLabelName:
//your code here
break;
case frameLabelName2:
//another code here
break;
}
我建议你这样做吗?没有!创建类,不要在框架内编程,以避免禁用代码。