如何创建一个sprite的多个实例

时间:2013-05-23 09:05:12

标签: ios objective-c cocos2d-iphone ccsprite

我正在制作一个CCSprite(它应该代表一个炸弹),就像双击一样。

- (void)handleDoubleTap:(UITapGestureRecognizer *)doubletapRecognizer
{
    [self placeBomb];
}

-(void)placeBomb
{
    NSLog(@"Bomb placed");
    _circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
    CGPoint circle0position = ccp(_cat.position.x , _cat.position.y);
    CGPoint c0TileCoordt = [self tileCoordForPosition:circle0position];
    CGPoint c0TileCoord = [self positionForTileCoord:c0TileCoordt];//Supereffective way to get rid of decimals
    _circle.position = c0TileCoord;
    [self addChild:_circle];
    id fade = [CCScaleTo actionWithDuration:3.5 scale:0];
    [_circle runAction:fade];
    [self performSelector:@selector(explosion) withObject:nil afterDelay:3];
}

然后我运行-(void)explosion - 惊讶 - 使其爆炸,然后再次调用checkForDamage方法,该方法在爆炸的生命周期内测试爆炸区域内的碰撞。

问题是我不想限制炸弹(精灵)的数量,但是当我现在放置两颗炸弹时,第一颗炸弹的爆炸发生在第二颗炸弹的位置,我显然可以不要责怪,因为这就是我要求它做的事情。爆炸是基于_circle.position爆炸块大约有三百行,所以我不打扰你在(Heres pastebin link)张贴它,但作为一个例子,我将发布checkForDamage。

-(void)checkForDamage //Gets called with CCTime on explosion
{
    bool playerHit = NO;
    CGPoint bombPos = [self tileCoordForPosition:_circle.position];

    for (int i = 0; i <= explosionLenght; i++)
    {        
        CGPoint playerPosF = [self tileCoordForPosition:_cat.position];

        int bombX    = (bombPos.x + i);
        int bombY    = (bombPos.y + i);
        int bombNegX = (bombPos.x - i);
        int bombNegY = (bombPos.y - i);

        CGPoint centre = bombPos;
        CGPoint top    = ccp(centre.x, bombY);
        CGPoint left   = ccp(bombNegX, centre.y);
        CGPoint bottom = ccp(centre.x, bombNegY);
        CGPoint right  = ccp(bombX, centre.y);

        if (CGPointEqualToPoint(top, playerPosF) || 
            CGPointEqualToPoint(left, playerPosF) ||
            CGPointEqualToPoint(bottom, playerPosF) || 
            CGPointEqualToPoint(right, playerPosF))
        {
            playerHit = YES;

            NSLog(@"Player hit, BOOL:%i",playerHit);
            [self unschedule:@selector(checkForDamage)];
            break;
        }
    }

    [self performSelector:@selector(stopCheckDamage) withObject:nil afterDelay:2];    
}

如何创建_circle炸弹的多个实例,以便我可以同时进行多次爆炸并检查是否有损坏。我尝试使用for循环并使用标记创建精灵,但我不知道如何通过标记全局访问和执行sprite上的内容。我想为每次爆炸制作一个不同的精灵,比如

if (explosion1exists) {explosion with circle1}, if (expl1 + expl2 exists) {expl with circle3}

但是,由于我最多会放置20到30枚炸弹,这种情况非常无效,我认为必须有更好的方法。任何方法有效地做到这一点?

0 个答案:

没有答案