【问题标题】:how can we set tag for a CCSprite in cocos2D我们如何在 cocos2D 中为 CCSprite 设置标签
【发布时间】:2014-09-23 11:45:10
【问题描述】:

我是 cocos2D 的新手。我想为 CCSprite 设置标签,但这在 cococs2D 版本 3 中显示错误,而我在 Stackoverflow [set tag] 属性上看到了答案,但它在 cocos2d 版本 3 中不起作用。 我要求的是我创建了两个不同的怪物,我想在 Collision 委托中找出哪个怪物发生了碰撞。 让我告诉你我是如何创造怪物的。

CCSprite *monster = [CCSprite spriteWithImageNamed:@"xyz.png"];
int minY = monster.contentSize.height / 2;
int maxY = self.contentSize.height - monster.contentSize.height / 2;
int rangeY = maxY - minY;
int randomY = (arc4random() % rangeY) + minY;

// 2
monster.position = CGPointMake(self.contentSize.width + monster.contentSize.width/2, randomY);
monster.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, monster.contentSize} cornerRadius:0];
monster.physicsBody.collisionGroup = @"monsterGroup";
monster.physicsBody.collisionType  = @"monsterCollision";
[_physicsWorld addChild:monster z:1];

// 3
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int randomDuration = (arc4random() % rangeDuration) + minDuration;

// 4
CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(-monster.contentSize.width/2, randomY)];
CCAction *actionRemove = [CCActionRemove action];
[monster runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];




- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair monsterCollision:(CCNode *)monster projectileCollision:(CCNode *)projectile
{


    [monster removeFromParent];
    [projectile removeFromParent];

    score=score+1;

    return YES;
}

这里我想通过标签或图像获取 CCsprite,但我不确定如何识别碰撞委托中的 CCsprite。

【问题讨论】:

  • 标签在版本 3 中已弃用。改为使用 name 属性(适用于所有节点)。您可以 getChildByName、removeChildByName ... 等。

标签: ios objective-c cocos2d-iphone


【解决方案1】:

我不确定,但我认为您可以在添加精灵时为其设置名称属性:

[self addChild:(CCNode *) z:(NSInteger) name:(NSString *)]

然后通过

得到它

getChildByName

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多