无法将变量刷新为初始数

时间:2013-12-09 20:30:13

标签: actionscript-3 flash variables actionscript adobe

我正在做一个简单的游戏并且它正常工作,但现在唯一的问题是我的时间值(var nummer:Number = 300 :)当我完成或者游戏失败时我不会重置为300

import flash.utils.Timer;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.TimerEvent;


var femMinutter:Timer = new Timer(1000, 300);
var tid:Timer = new Timer(5000);
var rightArrow:Boolean;
var leftArrow:Boolean;
var upArrow:Boolean;
var downArrow:Boolean;
var speed:int = 10;
var score:int = 0
var ferdig:MovieClip = new Sluttskjerm();
var startSpillet:MovieClip = new Startskjerm();
var feiletSpillet:MovieClip = new Feilskjerm();
var nummer:Number = 300;

stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.removeEventListener(Event.ENTER_FRAME, everyFrame);
femMinutter.addEventListener(TimerEvent.TIMER, tellNed);
tid.addEventListener(TimerEvent.TIMER, flyttRandom);

knpStart.addEventListener(MouseEvent.CLICK, startSpill);
addChild(startSpillet);
startSpillet.x = 275;
startSpillet.y = 150;

troll.x = 500;
troll.y = 300;
meg.x = 100;
meg.y = 300;
function tellNed (evt:TimerEvent):void
{
txtTid.text = String((nummer)-femMinutter.currentCount);
if (txtTid.text == "0")
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.removeEventListener(Event.ENTER_FRAME, everyFrame);
troll.x = 500;
troll.y = 300;
meg.x = 100;
meg.y = 300;
addChild(feiletSpillet);
feiletSpillet.x = 275;
feiletSpillet.y = 150;
tid.stop();
femMinutter.stop();
nummer = 300;
}
}
function startSpill(event:MouseEvent):void
{

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
tid.start();
femMinutter.start();
if (contains(ferdig))
{
    removeChild(ferdig);
}
if (contains(startSpillet))
{
    removeChild(startSpillet);
}
if (contains(feiletSpillet))
{
    removeChild(feiletSpillet);
}
}

function flyttRandom (evt:TimerEvent):void
{
troll.x = Math.random() * (550 - 0) + 0;
troll.y = Math.random() * (350 - 0) + 0;
}
function keyPressed(event:KeyboardEvent):void 
{
if (event.keyCode == Keyboard.RIGHT)
{
    rightArrow = true;
}
if (event.keyCode == Keyboard.LEFT) 
{
    leftArrow = true;
}
if (event.keyCode == Keyboard.UP) 
{
    upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) 
{
    downArrow = true;
}   
if (meg.hitTestObject(troll) == true)
{
    score = score+1;
    txtScore.text = score.toString();
    troll.x = Math.random() * (550 - 0) + 0;
    troll.y = Math.random() * (350 - 0) + 0;
    troll.scaleX = troll.scaleX * 0.9;
    troll.scaleY = troll.scaleX * 0.9;
    if (score == 2)
    {
        femMinutter.stop();
        tid.stop();
        stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
        stage.removeEventListener(Event.ENTER_FRAME, everyFrame);
        troll.x = 500;
        troll.y = 300;
        meg.x = 100;
        meg.y = 300;
        addChild(ferdig);
        ferdig.x = 275;
        ferdig.y = 150;
        score = 0;
        troll.height = 134;
        troll.width = 138;
        nummer = 300;
    }
}   
}
function keyReleased(event:KeyboardEvent):void 
{
if (event.keyCode == Keyboard.RIGHT) 
{
    rightArrow = false;
}
if (event.keyCode == Keyboard.LEFT) 
{
    leftArrow = false;
}
if (event.keyCode == Keyboard.UP) 
{
    upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) 
{
    downArrow = false;
}
}
function everyFrame(event:Event):void 
{
if (rightArrow) 
{
    meg.x += speed;
}
if (leftArrow) 
{
    meg.x -= speed;
}
if (upArrow) 
{
    meg.y -= speed;
}
if (downArrow) 
{
    meg.y += speed;
}   
}

如你所见,我已添加“nummer = 300;”失败和赢得屏幕。如果我在3秒钟内获胜,时钟将显示297,并在下一轮而不是最初的300开始于297。

1 个答案:

答案 0 :(得分:0)

Arg ....冗余代码使我的眼睛流血%-X

但是,你还没有重置你的计时器(femMinutter.reset())。这意味着

femMinutter.currentCount

即使你的比赛结束,

也会继续增加。