我是 box2d 新手。我想创建 Sprite 实例(像小翅膀这样的硬币)并使其与玩家碰撞。是否有一个 Sprite 主体定义就足够了,或者我必须为所有实例 Sprite 单独创建一个 Sprite 主体 我需要精确的碰撞效果,例如小翅膀硬币收藏。任何帮助请告诉我。谢谢
最佳答案
如果我必须解决这个问题,我会创建一个类(例如 Coin),然后创建它的多个实例并使用数组存储它们。以下是创建新硬币并将其存储在数组中的代码:
//in the constructor function of the playing screen:
coins = new Array();
//the constructor function of the Coin should contain the startX and startY coordinate
var newCoin= new Coin(100, 50);
coins.push( newCoin );
addChild( newCoin );
您可以使用 exampleArray.pop 删除数组的元素 ---> 这将删除最后一个元素 或 exampleArray.splice(x,z) x 是您删除的元素,z 是您删除的元素数量。 exampleArray.splice(5,2) 将从第 5 个元素开始删除 2 个元素。数组的第一个元素编号为 0,第二个元素编号为 1,依此类推...
您可以像这样检查碰撞:
for each (var coin in coins)
{
if (coin.hitTestObject(player))
{
//do something
}
}
关于ios - 像小翅膀一样的硬币收藏品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8296555/