0

我对 Objective-C 和 Cocos2D 还很陌生,所以放轻松,我知道这是基本的东西。

我正在使用一个数组在屏幕上随机放置 4 个金币精灵,我正在使用另一个精灵(一条龙)飞来飞去收集金币。显然,我希望硬币消失,另一个随机出现(就像在 Snake 中收集苹果一样)。我的问题是我不知道如何引用各个精灵。我已经为更新方法添加了一些处理,但是 a) 我认为它不合适,并且 b) 它没有做任何事情。请帮忙。我想我需要利用:

[self removeChild:sprite cleanup:YES];

但我不确定如何。这就是我用环填充屏幕的方式::

@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>"
4

2 回答 2

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);

希望这可以帮助。

于 2014-05-05T23:38:47.493 回答
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;
    }
}   
于 2014-05-05T15:44:54.520 回答