我的代码:
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,我的代码运行得很好,没有错误,但我的问题是,当它们击中时,变量不会增加。看到我已经尝试了一段时间,但没有成功,而且我的技能很低,我就不知所措了。
您有兴趣看到"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;
}