我对Objective-C和Cocos2D很陌生,所以很容易,我知道这是基本的东西。
我正在使用一个阵列在屏幕上随机放置4个金币精灵,我正在使用另一个精灵(一条龙)飞来飞去收集硬币。显然,我希望硬币消失,另一个随机出现(比如在Snake中收集苹果)。我的问题是我不知道如何引用各个精灵。我已经为更新方法添加了一些处理但是a)我认为它不合适而且b)它没有做任何事情。请帮忙。我想我需要利用:
[self removeChild:sprite cleanup:YES];
但我不确定如何。这就是我用ring ::
填充屏幕的方式@implementation MainScene {
CCSprite *_hero;
CCPhysicsNode *_physicsNode;
CCSprite *_goldRing;
NSMutableArray *_goldRings;
}
-(void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
_goldRing.physicsBody.collisionType = @"Level";
_goldRing.physicsBody.sensor = TRUE;
_physicsNode.collisionDelegate = self;
_hero.physicsBody.collisionType = @"hero";
CGFloat random = ((double)arc4random() / ARC4RANDOM_MAX);
_meteorites = [NSMutableArray array];
[self spawnNewGoldRing];
[self spawnNewGoldRing];
[self spawnNewGoldRing];
[self spawnNewGoldRing];
}
-(void)spawnNewGoldRing {
CGFloat randomX = ((double)arc4random() / ARC4RANDOM_MAX);
CGFloat randomY = ((double)arc4random() / ARC4RANDOM_MAX);
CGFloat rangeX = 200;
CGFloat rangeY = 300;
CCNode *goldRing = [CCBReader load:@"goldRing"];
goldRing.position = ccp(randomX * rangeX, (randomY * rangeY)+100);
[_physicsNode addChild:goldRing];
[_goldRings addObject:goldRing];
}
- (void)update:(CCTime)delta {
_hero.position = ccp(_hero.position.x, _hero.position.y);
if(_hero.position.x <= _hero.contentSize.width/2 * -1.5) {
_hero.position = ccp(_ground.contentSize.width + _hero.contentSize.width/2, _hero.position.y);
NSMutableArray *collectedGoldRings = nil;
for (CCNode *goldRing in _goldRings) {
CGPoint goldRingWorldPosition = [_physicsNode convertToWorldSpace:goldRing.position];
CGPoint goldRingScreenPosition = [self convertToNodeSpace:goldRingWorldPosition];
if (goldRingScreenPosition.x < -goldRing.contentSize.width) {
if (!collectedGoldRings) {
collectedGoldRings = [NSMutableArray array];
}
[collectedGoldRings addObject:goldRing];
}
}
for (CCNode *goldRingToRemove in collectedGoldRings) {
[goldRingToRemove removeFromParent];
[_goldRings removeObject:goldRingToRemove];
// for each removed goldRing, add a new one
[self spawnNewGoldRing];
}
}
}
要问如何计算这些数据以显示分数会不会太多?
非常感谢您的帮助。
编辑*
NSLog上的金戒指阵列
"<goldRing = 0x9b756b0 | Rect = (0.00,0.00,24.00,23.50) | tag = | atlasIndex = -1>"
)
2014-05-08 10:24:32.201 dragonCollector2[10165:60b] Gold Ring Loaded
2014-05-08 10:24:32.201 dragonCollector2[10165:60b] Array:(
"<goldRing = 0x9b77e50 | Rect = (0.00,0.00,24.00,23.50) | tag = | atlasIndex = -1>"
答案 0 :(得分:1)
正如我所见,您正在使用spritebuilder,因此您正在使用新版本的Cocos2D。 在此版本中,您可以跟踪两种类型节点之间的冲突。在你的情况下,硬币和英雄。
这很简单:
1)设置coin.physicsBody.collisionType = @"coin";
2)设置hero.physicsBody.collisionType = @"hero";
3)实施此方法:
-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair hero:(CCNode *)nodeA coin:(CCNode *)nodeB{
//this method will be automatically called when the hero hits a coin.
//now call your method to remove the coin and call the method to add another coin.
}
要记录得分,只需制作一个int变量int score;
。在didLoadFromCCB方法中,将其初始化为0,score=0;
并在此碰撞方法中,只需执行score++;
并执行NSLog(@"Your score:%i",score);
希望这有帮助。
答案 1 :(得分:0)
对于这样的事情,你不需要使用物理学。当你的英雄将触摸硬币时,只需更新用户分数(可能是整数)和硬币位置。 在您的更新方法中执行类似
的操作for(CCSprite *gold in _goldRings){
if(CGRectIntersectsRect(_hero.boundingBox,gold.boundingBox)){
//touched a coin
//do whatever u want
//do not remove the coin from the array, just change it's position!
_userPoints += 1;
gold.position=ccp(arc4random()%200 , arc4random()%300);
break;
}
}