2014-09-23 22 views
2

我是cocos2D的新手。我想爲CCSprite設置標籤,但是這顯示我在cococs2D版本3中出現錯誤,而我看到了Stackoverflow [set tag]屬性的應答,但它在cocos2d第3版中無法使用。 我需要的是我創建了兩個不同的怪物,我想在碰撞代表中找出哪個怪物已經相撞。 讓我告訴你我是如何創造怪物。我們如何在cocos2D中爲CCSprite設置標籤

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碰撞委託。

+2

標籤得到它是在版本3中不推薦使用。而是使用name屬性(適用於所有節點)。你可以getChildByName,removeChildByName等等。 – YvesLeBorg 2014-09-23 11:58:46

回答

1

我不知道,但我認爲你可以像加時,設置名稱屬性精靈:

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

再後來

getChildByName