当来自同一数组的 2 个对象命中时出现 HitTest 对象错误
HitTest Object Bug when 2 object from same array hit
大家好,所以我 运行 使用我的代码遇到了一个 Bug 我有一个名为 mainFish
的影片剪辑对象,它是使用数组 aFishArray
设置的,我在用 playerHook
检查 HitTest 的 for 循环。现在一切正常,但我遇到的问题是,当两条鱼同时上钩时,一条鱼钩挂在鱼钩上,另一条鱼钩留在屏幕上,我 运行进入我的声音对象重复一遍和其他错误。这是我用来追踪鱼的函数:
这是我在 ENTER_FRAME 游戏循环中的函数:
//Check if fish is null then run function
if (mainFish == null)
{
checkPlayerHitFish();
}else
{
trackFish();
}
和函数:
private function checkPlayerHitFish():void
{
//loop through all our fishes
for (var j:int = 0; j < aFishArray.length; j++)
{
//get current fish in j loop
var currentFish:mcMainFish = aFishArray[j];
//test if current fish is hitting current playerhook
if (currentFish.hitTestObject(playerHook))
{
//trace("hit initialized");
mainFish = currentFish;
//Stop the fish from moving
currentFish.stopFish();
//fishing reel sound
fishingReelSoundChannel;
fishReelSound = new fishingReel();
fishingReelSoundChannel = fishReelSound.play(0, 9999);
fishingReelBoolean = true;
}
}
}
和 trackFish 函数:
private function trackFish():void
{
mainFish.x = playerHook.x;
mainFish.y = playerHook.y + 15;
}
任何人都可以看看我是否做错了什么或如何解决这个问题?
问题是,虽然您只允许一条鱼被鱼钩移动,但您仍在检查所有鱼的命中率。这既会停止它们,又会创建一个新的声音文件。
避免这种情况的方法是在鱼上钩后停止检查鱼的碰撞。为此,您可以在钓到鱼后跳出循环:
private function checkPlayerHitFish():void
{
//loop through all our fishes
for (var j:int = 0; j < aFishArray.length; j++)
{
//get current fish in j loop
var currentFish:mcMainFish = aFishArray[j];
//test if current fish is hitting current playerhook
if (currentFish.hitTestObject(playerHook))
{
//trace("hit initialized");
mainFish = currentFish;
//Stop the fish from moving
currentFish.stopFish();
//fishing reel sound
fishingReelSoundChannel;
fishReelSound = new fishingReel();
fishingReelSoundChannel = fishReelSound.play(0, 9999);
fishingReelBoolean = true;
//break out of the loop
break;
}
}
}
注意底部附近的 break
。这将使您 "break" 退出循环,从而阻止任何进一步的迭代。这样它将在第一次成功的命中测试后停止检查碰撞,并且只对一条鱼做出反应。
大家好,所以我 运行 使用我的代码遇到了一个 Bug 我有一个名为 mainFish
的影片剪辑对象,它是使用数组 aFishArray
设置的,我在用 playerHook
检查 HitTest 的 for 循环。现在一切正常,但我遇到的问题是,当两条鱼同时上钩时,一条鱼钩挂在鱼钩上,另一条鱼钩留在屏幕上,我 运行进入我的声音对象重复一遍和其他错误。这是我用来追踪鱼的函数:
这是我在 ENTER_FRAME 游戏循环中的函数:
//Check if fish is null then run function
if (mainFish == null)
{
checkPlayerHitFish();
}else
{
trackFish();
}
和函数:
private function checkPlayerHitFish():void
{
//loop through all our fishes
for (var j:int = 0; j < aFishArray.length; j++)
{
//get current fish in j loop
var currentFish:mcMainFish = aFishArray[j];
//test if current fish is hitting current playerhook
if (currentFish.hitTestObject(playerHook))
{
//trace("hit initialized");
mainFish = currentFish;
//Stop the fish from moving
currentFish.stopFish();
//fishing reel sound
fishingReelSoundChannel;
fishReelSound = new fishingReel();
fishingReelSoundChannel = fishReelSound.play(0, 9999);
fishingReelBoolean = true;
}
}
}
和 trackFish 函数:
private function trackFish():void
{
mainFish.x = playerHook.x;
mainFish.y = playerHook.y + 15;
}
任何人都可以看看我是否做错了什么或如何解决这个问题?
问题是,虽然您只允许一条鱼被鱼钩移动,但您仍在检查所有鱼的命中率。这既会停止它们,又会创建一个新的声音文件。
避免这种情况的方法是在鱼上钩后停止检查鱼的碰撞。为此,您可以在钓到鱼后跳出循环:
private function checkPlayerHitFish():void
{
//loop through all our fishes
for (var j:int = 0; j < aFishArray.length; j++)
{
//get current fish in j loop
var currentFish:mcMainFish = aFishArray[j];
//test if current fish is hitting current playerhook
if (currentFish.hitTestObject(playerHook))
{
//trace("hit initialized");
mainFish = currentFish;
//Stop the fish from moving
currentFish.stopFish();
//fishing reel sound
fishingReelSoundChannel;
fishReelSound = new fishingReel();
fishingReelSoundChannel = fishReelSound.play(0, 9999);
fishingReelBoolean = true;
//break out of the loop
break;
}
}
}
注意底部附近的 break
。这将使您 "break" 退出循环,从而阻止任何进一步的迭代。这样它将在第一次成功的命中测试后停止检查碰撞,并且只对一条鱼做出反应。