我的变量不会增加

时间:2013-12-09 10:53:22

标签: actionscript-3 variables actionscript global-variables increment

我的代码:

import flash.utils.Timer;
import flash.events.KeyboardEvent;
import flash.events.Event;

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



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


txtScore.text = score.toString();


function flyttRandom (evt:TimerEvent):void
    {
    troll.x = Math.random() * (550 - 0) + 0;
    troll.y = Math.random() * (350 - 0) + 0;
    }
tid.start();
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;
    troll.x = Math.random() * (550 - 0) + 0;
    troll.y = Math.random() * (350 - 0) + 0;
    }   
}
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;
    }
}

所以我试图在flash中创建这个简单的游戏,每当对象“meg”击中对象“troll”时,变量得分将增加1,我的代码运行良好,我没有错误,但我有一个问题,变量在击中时不会增加。看来我已经尝试了一段时间但没有成功,而且我的闪光技能很低,我无能为力。

1 个答案:

答案 0 :(得分:0)

您是否有兴趣看到“txtScore”的增量?看起来您将TextField设置为一次,但在分数增加时从不重置它。

尝试:

if (meg.hitTestObject(troll) == true)
{
    score = score+1;
    txtScore.text = score.toString();           // show the new score
    troll.x = Math.random() * (550 - 0) + 0;
    troll.y = Math.random() * (350 - 0) + 0;
}