0

所以,我有一个游戏,你必须活着 45 秒,但是如果你死了,它会进入场景 4,你可以点击重新开始场景 3(这是你尽量不死的地方)。我的问题是我的计时器。我在场景 3 中声明了计时器,但它似乎在场景 1 中开始,而且,当我死时,计时器不会重新启动或停止,所以我可以一遍又一遍地死去,只要我一直在玩一共45秒,我自动获胜。我不确定这个错误发生在哪里,所以我将粘贴我所有的代码。//场景一

import flash.events.Event;
addEventListener(Event.ENTER_FRAME,Intro);




function Intro(e:Event)

{
var mySound:Sound = new E(); 
mySound.play();




Asteroid1.y = Asteroid1.y + 20;
Asteroid2.y = Asteroid2.y + 24;
Asteroid3.y = Asteroid3.y + 16;
Asteroid4.y = Asteroid4.y + 28;
Asteroid5.y = Asteroid5.y + 17
Asteroid6.y = Asteroid5.y + 22
Asteroid1.x = Asteroid1.x + 19;
Asteroid2.x = Asteroid2.x + 18;
Asteroid3.x = Asteroid3.x + 14;
Asteroid4.x = Asteroid4.x + 17;
Asteroid5.x = Asteroid5.x + 21
Asteroid6.x = Asteroid6.x + 20
if(Asteroid1.y > 768){
Asteroid1.y = -40;
}
if(Asteroid2.y > 768){
   Asteroid2.y = -40;
}
if(Asteroid2.x > 1024){
   Asteroid2.x = -40;
}
if (Asteroid1.x > 1024){
   Asteroid1.x = -40
   }
   if(Asteroid3.x > 1024){
       Asteroid3.x = -20;
   }
   if (Asteroid3.y > 768){
       Asteroid3.y = -20;
   }
   if (Asteroid4.y > 768){
       Asteroid4.y = -20;
   }
   if(Asteroid4.x > 1024){
       Asteroid4.x = -20;


   }
   if (Asteroid5.y > 768) {
       Asteroid5.y = -30;
   }
   if (Asteroid5.x > 1024) {
       Asteroid5.x = -40;
   }
    if (Asteroid6.y > 768){
        Asteroid6.y = -30;
    }
    if (Asteroid6.x > 1024) {
        Asteroid6.x = -30;
    }
}

//Scene 2

import flash.events.Event;
addEventListener(Event.ENTER_FRAME,IntroS2);

function IntroS2(e:Event) {


Asteroid5.y = Asteroid5.y + 20;
Asteroid6.y = Asteroid6.y + 18;
Asteroid7.y = Asteroid7.y + 16;
Asteroid8.y = Asteroid8.y + 22;
Asteroid9B.y = Asteroid9B.y + 17
Asteroid5.x = Asteroid5.x + 20;
Asteroid6.x = Asteroid6.x + 22;
Asteroid7.x = Asteroid7.x + 14;
Asteroid8.x = Asteroid8.x + 18;
Asteroid9B.x = Asteroid9B.x + 16
if (Asteroid5.y > 768) {
   Asteroid5.y = -40;
}
if (Asteroid6.y > 768) {
   Asteroid6.y = -40;
}
if (Asteroid5.x > 1024) {
   Asteroid5.x = -40;
}
if (Asteroid6.x > 1024) {
   Asteroid6.x = -20;
}
if (Asteroid7.x > 1024) {
   Asteroid7.x = -20;
}
if (Asteroid7.y > 768) {
   Asteroid7.y = -20;
}
if (Asteroid8.y > 768) {
   Asteroid8.y = -20;
}
if (Asteroid8.x > 1024) {
   Asteroid8.x = -20;
   }
   if (Asteroid9B.x > 1024){
       Asteroid9B.x = -30;
   }
   if (Asteroid9B.Y >768){
       Asteroid9B.y = -20;
   }
}
//Scene 2 last frame

stop();


stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);

function _onStageMouseDown(e:MouseEvent):void
{ 
removeEventListener(Event.ENTER_FRAME,IntroS2);
 gotoAndStop(1, "Scene 5");

}

//Scene 3
import flash.events.Event;
addEventListener(Event.ENTER_FRAME,ms3);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
myTimer.addEventListener(TimerEvent.TIMER, handleTimerTick);

stage.removeEventListener(MouseEvent.CLICK, _onStageMouseDown1);
stage.removeEventListener(MouseEvent.CLICK, _onStageMouseDown);
Mouse.hide();

//TIMER
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, onEnterFrame);
myTimer.start(); 
//TIMER




function ms3(e:Event){
Mouse.hide();

Planet3.y = mouseY;
Planet3.x = mouseX;

Asteroid9.x = Asteroid9.x + 26;
Asteroid9.y = Asteroid9.y + 21;

if (Asteroid9.x > 1024){
Asteroid9.x = -40
}
if (Asteroid9.y > 768){
Asteroid9.y = -40
}

Asteroid10.x = Asteroid10.x + 22;
Asteroid10.y = Asteroid10.y + 23;

if (Asteroid10.x > 1024){
Asteroid10.x = -40
}
if (Asteroid10.y > 768){
Asteroid10.y = -40
}


Asteroid11.x = Asteroid11.x + 18;
Asteroid11.y = Asteroid11.y + 21;

if (Asteroid11.x > 1024){
Asteroid11.x = -40
}
if (Asteroid11.y > 768){
Asteroid11.y = -40
}


Asteroid12.x = Asteroid12.x + 24;
Asteroid12.y = Asteroid12.y + 22;

if (Asteroid12.x > 1024){
Asteroid12.x = -40
}
if (Asteroid12.y > 768){
Asteroid12.y = -40
}

// Hit Test
if(Planet3.hitTestObject(Asteroid9)||
Planet3.hitTestObject(Asteroid10)||
Planet3.hitTestObject(Asteroid11)||
Planet3.hitTestObject(Asteroid12))
{


gotoAndStop(1, "Scene 4");
}
}


//TIMER SECTION


function resetTimer():void
{
// check if timer already initialize
if(myTimer != null)
{
// just reset
myTimer.stop();
myTimer.reset();
}
else
{
// initialize timer and set a 45 sec delay;
myTimer = new Timer(4500,1);
myTimer.addEventListener(TimerEvent.TIMER, handleTimerTick);
}    

myTimer.start();

}

function handleTimerTick(event:TimerEvent):void
{ 
// stop and null timer;
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, handleTimerTick);
myTimer = null;

// goto my 4th scene 
gotoAndStop(1, "Scene 4");
}

// whenever a hit test is performed and a timer reset is needed just call
resetTimer();


function onEnterFrame(e:Event):void {

var startTime:int=getTimer();


var currentTime:int=getTimer();
trace(currentTime);




if (currentTime>45000){
gotoAndStop(1, "Scene 6");

}
}

这些都是真正重要的场景,4-6大部分只是动画和一些鼠标点击等。场景4-6中没有引用计时器。有任何想法吗?如果需要,我会编辑额外的信息。

4

1 回答 1

0

1 - getTimer() 计算自 Flash 运行时执行开始以来的时间,因此您的条件应该是这样的:

if ( (currentTime - startTime) > 45000){
gotoAndStop(1, "Scene 6");
}

但是你需要从 enterFrame 中删除这一行,并让它运行一次,当你的游戏开始时

var startTime:int=getTimer();

2 - 4500ms = 4.5 秒,而不是 45 秒

 // initialize timer and set a 45 sec delay;
 myTimer = new Timer(4500,1);
于 2013-02-17T06:55:13.097 回答