我希望能够为 CCSpriteBatchNode 识别一组对象,而且还能识别该组的子组。做类似的事情
CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
for (GameObject *tempObject in listOfGameObjects) {
if ([tempObject tag] == kBottleTagValue) {
//make bottle yellow
if ([tempObject tag] == kBrokenBottleTagValue) {
//also make bottle smaller
}
}
}
在这个例子中,所有的瓶子都会变成黄色,如果瓶子也被标记为坏了,它就会变小。所以破瓶子需要用 kBottleTagValue 和 kBrokenBottleTagValue 来标记。有没有办法做到这一点?因为当我尝试只添加两个标签时它失败了。
最佳答案
是的,您可以使用位掩码来做到这一点。 例如定义你的标签:
enum
{
kBottleTagValue = 1 << 0;
kBrokenBottleTagValue = 1 << 1;
};
然后标记你的 Sprite :
[yoursprite setTag:kBottleTagValue|kBrokenBottleTagValue];
最后你可以通过做类似的事情来检查你的 Sprite 是否是一个破瓶子:
CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
for (GameObject *tempObject in listOfGameObjects)
{
if ([tempObject tag] & kBottleTagValue)
{
//make bottle yellow
if ([tempObject tag] & kBrokenBottleTagValue)
{
//also make bottle smaller
}
}
}
希望对你有帮助。
关于iphone - 在 Cocos2d 中是否可以为单个对象使用多个标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7433869/