我正在创建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次点击将苹果设置为隐形如果点击苹果顶部没有任何反应,我需要点击苹果中心隐藏它,为什么我有这个问题?
答案 0 :(得分:1)
如果你认真对待游戏开发,你应该考虑代码膨胀。在您的情况下,您不需要任何apples
数组 - 您的显示列表,礼貌的Flash Player,已经能够在排序的链表结构中包含苹果 - 对于您打算使用它的类型更有效 - 删除和添加苹果。如果您专门用于容纳苹果的容器精灵,您将获得两个好处:1)您获得免费的苹果显示列表和2)如果您为此精灵(仅包含苹果)注册鼠标单击事件监听器,则每次点击在苹果上,event.target
会反映出来。
仔细查看更改的代码:
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
mcApples = new Sprite();
mcApples.addEventListener(MouseEvent.CLICK, onClickApple);
mcGameStage.addChild(mcApples);
mcGameStage.addEventListener(Event.ENTER_FRAME, update);
}
private function update(evt:Event)
{
//Spawn new apple
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;
mcApples.addChild(newApple);
}
//Move Apples
for (var i = 0; i < mcApples.numChildren;)
{
var apple = mcApples.getChildAt(i);
apple.y += gravity;
if(apple.y > C.APPLE_END_Y)
{
apple.parent.removeChild(apple);
continue;
}
i++;
}
}
function onClickApple(event: MouseEvent):void
{
var apple : Apple = event.target;
apple.parent.removeChild(apple);
score += C.SCORE_PER_APPLE;
}
因此,重要的变化及其影响是:
mcApples
,其中添加和删除苹果。mcApples
容器中的苹果。因此,不需要拼接数组,也不需要Apple
类中的显式函数引用或为每个新苹果添加事件侦听器。
作为额外的建议,你应该小心地将你的逻辑绑定到enter frame
事件 - 帧速率越快,通话频率越快 - 通常没有问题,但有时不是你想要的。我会将update
调用绑定到计时器事件; - )
答案 1 :(得分:0)
private function update(evt:Event)
{
//Spawn new apples
if (Math.random() < randomChance)
{
var newApple = new Apple(appleClick);
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[iz].destruct();
apples.splice(i,1);
}
}
//txtScore.text = String(score);
}
function appleClick(pApple:Apple):void{
for (var iz = apples.length-1; iz >= 0; iz--)
{
if (apples[iz] == pApple)
{
//Register hit
score += C.SCORE_PER_APPLE;
mcGameStage.removeChild(apples[iz]);
apples[iz].destruct();
apples.splice(iz,1);
break;
}
}
}
...
public class Apple extends Group or something
{
private var m_fCallback:Function;
public function Apple(pCallback:Function):void
{
addEventListener(MouseEvent.CLICK, onClick);
m_fCallback = pCallback;
}
private function destruct():void
{
removeEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(pEvent:MouseEvent):void
{
m_fCallback(this);
}
}
答案 2 :(得分:0)
我没有看到 onClick()添加了任何事件监听器。请务必检查您的事件监听器是否已添加,并在 onClick()功能中添加跟踪或警报,以便确认代码是否已实际到达。