我仍然在为我的游戏工作并且差不多完成了。 我现在遇到的问题是我写的倒数计时器不能正常工作。 我有一个30秒的倒计时写在它自己的班级,它在那里工作正常。 一旦我将其粘贴到我的游戏代码中,它就会从30到7计算,然后在2到3秒内完成游戏。 我很确定它与帧速率覆盖计时器有关,但我不能为我的生活弄清楚如何阻止它。 我知道这不是程序,这是我没有考虑到的事情,但是经过整整一个周末试图解决这个问题后,我很难解决问题。
这是代码......
public class MyGame extends MovieClip {
public function MyGame() {
const BG_SPEED:int = 5;
const BG_MIN:int = -550;
const BG_MAX:int = 0;
const PBG_SPEED:int = 3;
var bg:BackGround = new BackGround;
var paraBg:ParaBg = new ParaBg;
var toybox:TargetBox = new TargetBox;
var toy:Toy = new Toy;
var cheer:Cheer = new Cheer;
var eightBit:EightBit = new EightBit;
var countDown:Number = 30;
var myTimer:Timer = new Timer(1000);
var myText:TextField = new TextField;
var myTextFormat:TextFormat = new TextFormat;
var font:Font = new Font;
var score:int = 0;
var toy1:Toy1 = new Toy1;
var toy2:Toy2 = new Toy2;
var toy3:Toy3 = new Toy3;
var toy4:Toy4 = new Toy4;
var toy5:Toy5 = new Toy5;
var toy6:Toy6 = new Toy6;
var toy7:Toy7 = new Toy7;
var toy8:Toy8 = new Toy8;
var toy9:Toy9 = new Toy9;
var toy10:Toy10 = new Toy10;
var toy11:Toy11 = new Toy11;
var toy12:Toy12 = new Toy12;
var toy13:Toy13 = new Toy13;
var toy14:Toy14 = new Toy14;
var toy15:Toy15 = new Toy15;
var toy16:Toy16 = new Toy16;
var toy17:Toy17 = new Toy17;
var toy18:Toy18 = new Toy18;
var toy19:Toy19 = new Toy19;
var toy20:Toy20 = new Toy20;
var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
eightBit.play(0, 9999);
addChildAt(paraBg, 0);
addChildAt(bg, 1);
addChild(toy);
toy.x = 306;
toy.y = 133;
addChild(toybox);
toybox.x = 295;
toybox.y = 90;
bg.addEventListener(Event.ENTER_FRAME, bgScroll);
myText.text = countDown.toString();
myTimer.start();
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
for (var i:int = 0; i < toyArray.length; i++)
{
addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);
}
function bgScroll (e:Event)
{
if (stage.mouseX > 600 && bg.x > BG_MIN)
{
bg.x -= BG_SPEED;
paraBg.x -= PBG_SPEED;
for (var i:int=0; i< toyArray.length; i++)
{
(toyArray[i] as MovieClip).x -=BG_SPEED
}
}
else if (stage.mouseX < 50 && bg.x < BG_MAX)
{
bg.x += BG_SPEED;
paraBg.x += PBG_SPEED;
for (var j:int=0; j< toyArray.length; j++)
{
(toyArray[j] as MovieClip).x +=BG_SPEED
}
}
for (var k:int = 0; k < toyArray.length; k++)
{
toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
}
stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
function arrayGrab(e:MouseEvent)
{
e.target.startDrag();
}
function arrayDrop(e:MouseEvent)
{
e.target.stopDrag();
}
function collision (e:Event)
{
for (var l:int=0; l< toyArray.length; l++)
{
if (toyArray[l].hitTestObject(toy))
{
removeChild(toyArray[l]);
toyArray[l].x=100000;
toybox.gotoAndPlay(2);
cheer.play(1, 1);
score = score + 10;
trace(score);
}
}
}
e.target.addEventListener(Event.ENTER_FRAME, collision);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
function displayText():void
{ myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 200, myText.x = 400, myText.y = 20;
myTextFormat.size = 30, myTextFormat.font = font.fontName;
myText.setTextFormat(myTextFormat);
}
function displayText2():void
{ myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 200, myText.x = 200, myText.y = 20;
myTextFormat.size = 30, myTextFormat.font = font.fontName;
myText.setTextFormat(myTextFormat);
}
function countdown(e:TimerEvent):void
{
if (countDown > 0)
{
countDown--;
}
myText.text = countDown.toString();
if (countDown == 0)
{
myText.text = "GAME OVER!";
}
displayText();
}
displayText();
}
}
}
}
我尝试使用不同的结果移动计时器监听器,但都不需要。
任何想法我做错了什么?
答案 0 :(得分:0)
您的问题与嵌套函数有关。
您有一种名为bgScroll
的方法,它正在运行每个框架。在此方法中,您将事件侦听器附加到Timer
。所以每一帧,你都要为timer事件添加一个新的监听器。由于countdown
方法嵌套在bgScroll
方法中,因此每个帧都会创建该方法的新实例并附加到计时器。
所以你的countdown
方法最终在第一帧被调用1次,第二帧被调用2次,第三帧被调用3次等等。所以它基本上是以指数方式倒计时。
要解决此问题,不要使用NEST FUNCTIONS !!! - 尤其是在每帧运行的方法中。这有内存泄漏。
分析bgScroll
内的所有方法,使它们在类范围内。另外,打破bgscroll
,使它也在类范围内。
例如,您的代码看起来应该更像这样:(这不是您确切的代码应该是什么,只是说明我的意思是通过打破您的方法所以一切都没有嵌套)
public class MyGame extends MovieClip
{
const BG_SPEED:int = 5;
const BG_MIN:int = -550;
const BG_MAX:int = 0;
const PBG_SPEED:int = 3;
var bg:BackGround = new BackGround;
var paraBg:ParaBg = new ParaBg;
var toybox:TargetBox = new TargetBox;
var toy:Toy = new Toy;
var cheer:Cheer = new Cheer;
var eightBit:EightBit = new EightBit;
var countDown:Number = 30;
var myTimer:Timer = new Timer(1000);
var myText:TextField = new TextField;
var myTextFormat:TextFormat = new TextFormat;
var font:Font = new Font;
var score:int = 0;
var toy1:Toy1 = new Toy1;
var toy2:Toy2 = new Toy2;
var toy3:Toy3 = new Toy3;
var toy4:Toy4 = new Toy4;
var toy5:Toy5 = new Toy5;
var toy6:Toy6 = new Toy6;
var toy7:Toy7 = new Toy7;
var toy8:Toy8 = new Toy8;
var toy9:Toy9 = new Toy9;
var toy10:Toy10 = new Toy10;
var toy11:Toy11 = new Toy11;
var toy12:Toy12 = new Toy12;
var toy13:Toy13 = new Toy13;
var toy14:Toy14 = new Toy14;
var toy15:Toy15 = new Toy15;
var toy16:Toy16 = new Toy16;
var toy17:Toy17 = new Toy17;
var toy18:Toy18 = new Toy18;
var toy19:Toy19 = new Toy19;
var toy20:Toy20 = new Toy20;
var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
public function MyGame()
{
eightBit.play(0, 9999);
addChildAt(paraBg, 0);
addChildAt(bg, 1);
addChild(toy);
toy.x = 306;
toy.y = 133;
addChild(toybox);
toybox.x = 295;
toybox.y = 90;
bg.addEventListener(Event.ENTER_FRAME, bgScroll);
myText.text = countDown.toString();
myTimer.start();
for (var i:int = 0; i < toyArray.length; i++)
{
addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);
}
stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
e.target.addEventListener(Event.ENTER_FRAME, collision);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
}
function addToys(xpos:int, ypos:int)
{
addChild(toyArray[i]);
toyArray[i].x = xpos;
toyArray[i].y = ypos;
}
function bgScroll(e:Event)
{
if (stage.mouseX > 600 && bg.x > BG_MIN)
{
bg.x -= BG_SPEED;
paraBg.x -= PBG_SPEED;
for (var i:int = 0; i < toyArray.length; i++)
{
(toyArray[i] as MovieClip).x -= BG_SPEED
}
}
else if (stage.mouseX < 50 && bg.x < BG_MAX)
{
bg.x += BG_SPEED;
paraBg.x += PBG_SPEED;
for (var j:int = 0; j < toyArray.length; j++)
{
(toyArray[j] as MovieClip).x += BG_SPEED
}
}
for (var k:int = 0; k < toyArray.length; k++)
{
toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
}
displayText();
}
function arrayGrab(e:MouseEvent)
{
e.target.startDrag();
}
function arrayDrop(e:MouseEvent)
{
e.target.stopDrag();
}
function collision(e:Event)
{
for (var l:int = 0; l < toyArray.length; l++)
{
if (toyArray[l].hitTestObject(toy))
{
removeChild(toyArray[l]);
toyArray[l].x = 100000;
toybox.gotoAndPlay(2);
cheer.play(1, 1);
score = score + 10;
trace(score);
}
}
}
function displayText():void
{
myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 200, myText.x = 400, myText.y = 20;
myTextFormat.size = 30, myTextFormat.font = font.fontName;
myText.setTextFormat(myTextFormat);
}
function displayText2():void
{
myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
addChild(myText);
myText.width = 500, myText.height = 200, myText.x = 200, myText.y = 20;
myTextFormat.size = 30, myTextFormat.font = font.fontName;
myText.setTextFormat(myTextFormat);
}
function countdown(e:TimerEvent):void
{
if (countDown > 0)
{
countDown--;
}
myText.text = countDown.toString();
if (countDown == 0)
{
myText.text = "GAME OVER!";
}
displayText();
}
}