我正在创建 Flash 游戏,Idea 是从天上掉下来的物体(在这种情况下是苹果),玩家需要点击苹果,点击苹果后它必须被删除并且分数更新 10 分。
这是我生成苹果的部分代码:
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
apples = new Array();
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function update(evt:Event)
{
//Spawn new apples
if (Math.random() < randomChance)
{
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
newApple.y = C.APPLE_START_Y;
apples.push(newApple);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--)
{
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
//txtScore.text = String(score);
}
}
这里是应该通过鼠标点击删除苹果的代码。但它不起作用,我没有任何错误,只是没有删除苹果。请问你能帮帮我吗?
function onClick(evt:MouseEvent):void{
var apples = evt.target;
for (var iz = apples.length-1; iz >= 0; iz--)
{
//Register hit
score += C.SCORE_PER_APPLE;
mcGameStage.removeChild(apples[iz]);
apples.splice(iz,1);
}
}
更新
所以我的代码现在看起来像:
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
apples = new Array();
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function update(evt:Event)
{
//Spawn new apples
if (Math.random() < randomChance)
{
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
newApple.y = C.APPLE_START_Y;
apples.push(newApple);
newApple.addEventListener(MouseEvent.CLICK, onClick);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--)
{
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
txtScore.text = String(score);
}
function onClick(evt:MouseEvent):void{
var apples = evt.target;
apples.visible = false;
//mcGameStage.removeChild(apples);
score += C.SCORE_PER_APPLE;
}
}
}
我改用这个apples.visible = false;
,mcGameStage.removeChild(apples);
没关系。只是我误解了为什么我的分数没有更新?总是显示0。有时我不能通过1点击设置苹果不可见如果点击苹果顶部没有任何反应,我需要点击苹果的中心来隐藏它,为什么我有这个问题?