2
\$\begingroup\$

I am creating an "explosion" of circle sprites from the character with this code, and I was wondering if there is a more effective way to do such things, because this just seems too stupid. (This is cocos2d)

int tileSize = 29;
explosionLenght += 1;
if (explosionLenght >= 7)
{
    explosionLenght = 1;
}

CCSprite *circle0 = [[CCSprite alloc]initWithFile:@"Circle.png"];
CCSprite *circle1 = [[CCSprite alloc]initWithFile:@"Circle.png"];
CCSprite *circle2 = [[CCSprite alloc]initWithFile:@"Circle.png"];
CCSprite *circle3 = [[CCSprite alloc]initWithFile:@"Circle.png"];
CCSprite *circle4 = [[CCSprite alloc]initWithFile:@"Circle.png"];




CGPoint circle0position = ccp(_cat.position.x , _cat.position.y);
CGPoint circle1position = ccp(_cat.position.x - tileSize , _cat.position.y);
CGPoint circle2position = ccp(_cat.position.x + tileSize , _cat.position.y);
CGPoint circle3position = ccp(_cat.position.x  , _cat.position.y -tileSize);
CGPoint circle4position = ccp(_cat.position.x  , _cat.position.y + tileSize);

CGPoint c0TileCoordt = [self tileCoordForPosition:circle0position];
CGPoint c1TileCoordt = [self tileCoordForPosition:circle1position];
CGPoint c2TileCoordt = [self tileCoordForPosition:circle2position];
CGPoint c3TileCoordt = [self tileCoordForPosition:circle3position];
CGPoint c4TileCoordt = [self tileCoordForPosition:circle4position];

CGPoint c0TileCoord = [self positionForTileCoord:c0TileCoordt];
CGPoint c1TileCoord = [self positionForTileCoord:c1TileCoordt];
CGPoint c2TileCoord = [self positionForTileCoord:c2TileCoordt];
CGPoint c3TileCoord = [self positionForTileCoord:c3TileCoordt];
CGPoint c4TileCoord = [self positionForTileCoord:c4TileCoordt];

circle0.position = c0TileCoord;
circle1.position = c1TileCoord;
circle2.position = c2TileCoord;
circle3.position = c3TileCoord;
circle4.position = c4TileCoord;

CGPoint centreTileCoord = ccp(circle0.position.x, circle0.position.y);
CGPoint leftTileCoord = ccp(circle1.position.x, circle1.position.y);
CGPoint bottomTileCoord = ccp(circle3.position.x, circle3.position.y);
CGPoint rightTileCoord = ccp(circle2.position.x, circle2.position.y);
CGPoint topTileCoord = ccp(circle4.position.x, circle4.position.y);

CGPoint cTileCoord = [self tileCoordForPosition:centreTileCoord];
CGPoint lTileCoord = [self tileCoordForPosition:leftTileCoord];
CGPoint bTileCoord = [self tileCoordForPosition:bottomTileCoord];
CGPoint rTileCoord = [self tileCoordForPosition:rightTileCoord];
CGPoint tTileCoord = [self tileCoordForPosition:topTileCoord];

if ([self isValidTileCoord:cTileCoord] && ![self isWallAtTileCoord:cTileCoord])
{
    [self addChild:circle0];
}
if ([self isValidTileCoord:lTileCoord] && ![self isWallAtTileCoord:lTileCoord])
{
    [self addChild:circle1];
    if (explosionLenght >= 2)
    {
        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
        circle.position = ccp(circle1.position.x - tileSize * 1, circle1.position.y);
        [self addChild:circle];
        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
        [circle runAction:fade];
        if (explosionLenght >= 3)
        {
            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
            circle.position = ccp(circle1.position.x - tileSize * 2, circle1.position.y);
            [self addChild:circle];
            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circle runAction:fade];
            if (explosionLenght >= 4)
            {
                CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                circle.position = ccp(circle1.position.x - tileSize * 3, circle1.position.y);
                [self addChild:circle];
                id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                [circle runAction:fade];
                if (explosionLenght >= 5)
                {
                    CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                    circle.position = ccp(circle1.position.x - tileSize * 4, circle1.position.y);
                    [self addChild:circle];
                    id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                    [circle runAction:fade];
                    if (explosionLenght >= 6)
                    {
                        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                        circle.position = ccp(circle1.position.x - tileSize * 5, circle1.position.y);
                        [self addChild:circle];
                        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                        [circle runAction:fade];
                        if (explosionLenght >= 7)
                        {
                            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                            circle.position = ccp(circle1.position.x - tileSize * 6, circle1.position.y);
                            [self addChild:circle];
                            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                            [circle runAction:fade];
                            if (explosionLenght >= 8)
                            {
                                explosionLenght = 1;
                            }
                        }
                    }
                }
            }
        }

    }






}
if ([self isValidTileCoord:bTileCoord] && ![self isWallAtTileCoord:bTileCoord])
{
    [self addChild:circle3];
    if (explosionLenght >= 2)
    {
        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
        circle.position = ccp(circle3.position.x , circle3.position.y- tileSize * 1);
        [self addChild:circle];
        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
        [circle runAction:fade];
        if (explosionLenght >= 3)
        {
            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
            circle.position = ccp(circle3.position.x, circle3.position.y - tileSize * 2);
            [self addChild:circle];
            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circle runAction:fade];
            if (explosionLenght >= 4)
            {
                CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                circle.position = ccp(circle3.position.x, circle3.position.y - tileSize * 3);
                [self addChild:circle];
                id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                [circle runAction:fade];
                if (explosionLenght >= 5)
                {
                    CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                    circle.position = ccp(circle3.position.x, circle3.position.y - tileSize * 4);
                    [self addChild:circle];
                    id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                    [circle runAction:fade];
                    if (explosionLenght >= 6)
                    {
                        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                        circle.position = ccp(circle3.position.x, circle3.position.y - tileSize * 5);
                        [self addChild:circle];
                        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                        [circle runAction:fade];
                        if (explosionLenght >= 7)
                        {
                            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                            circle.position = ccp(circle3.position.x , circle3.position.y- tileSize * 6);
                            [self addChild:circle];
                            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                            [circle runAction:fade];
                            if (explosionLenght >= 8)
                            {
                                explosionLenght = 1;
                            }
                        }
                    }
                }
            }
        }

    }


}
if ([self isValidTileCoord:rTileCoord] && ![self isWallAtTileCoord:rTileCoord])
{
    [self addChild:circle2];
    if (explosionLenght >= 2)
    {
        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
        circle.position = ccp(circle2.position.x + tileSize * 1, circle2.position.y);
        [self addChild:circle];
        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
        [circle runAction:fade];
        if (explosionLenght >= 3)
        {
            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
            circle.position = ccp(circle2.position.x + tileSize * 2, circle2.position.y);
            [self addChild:circle];
            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circle runAction:fade];
            if (explosionLenght >= 4)
            {
                CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                circle.position = ccp(circle2.position.x + tileSize * 3, circle2.position.y);
                [self addChild:circle];
                id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                [circle runAction:fade];
                if (explosionLenght >= 5)
                {
                    CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                    circle.position = ccp(circle2.position.x + tileSize * 4, circle2.position.y);
                    [self addChild:circle];
                    id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                    [circle runAction:fade];
                    if (explosionLenght >= 6)
                    {
                        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                        circle.position = ccp(circle2.position.x + tileSize * 5, circle2.position.y);
                        [self addChild:circle];
                        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                        [circle runAction:fade];
                        if (explosionLenght >= 7)
                        {
                            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                            circle.position = ccp(circle2.position.x + tileSize * 6, circle2.position.y);
                            [self addChild:circle];
                            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                            [circle runAction:fade];
                            if (explosionLenght >= 8)
                            {
                                explosionLenght = 1;
                            }
                        }
                    }
                }
            }
        }

    }

}
if ([self isValidTileCoord:tTileCoord] && ![self isWallAtTileCoord:tTileCoord])
{
    [self addChild:circle4];
    if (explosionLenght >= 2)
    {
        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
        circle.position = ccp(circle4.position.x , circle4.position.y+ tileSize * 1);
        [self addChild:circle];
        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
        [circle runAction:fade];
        if (explosionLenght >= 3)
        {
            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
            circle.position = ccp(circle4.position.x, circle4.position.y + tileSize * 2);
            [self addChild:circle];
            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circle runAction:fade];
            if (explosionLenght >= 4)
            {
                CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                circle.position = ccp(circle4.position.x, circle4.position.y + tileSize * 3);
                [self addChild:circle];
                id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                [circle runAction:fade];
                if (explosionLenght >= 5)
                {
                    CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                    circle.position = ccp(circle4.position.x, circle4.position.y + tileSize * 4);
                    [self addChild:circle];
                    id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                    [circle runAction:fade];
                    if (explosionLenght >= 6)
                    {
                        CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                        circle.position = ccp(circle4.position.x, circle4.position.y + tileSize * 5);
                        [self addChild:circle];
                        id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                        [circle runAction:fade];
                        if (explosionLenght >= 7)
                        {
                            CCSprite *circle = [[CCSprite alloc]initWithFile:@"Circle.png"];
                            circle.position = ccp(circle4.position.x , circle4.position.y- tileSize * 6);
                            [self addChild:circle];
                            id fade = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circle.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
                            [circle runAction:fade];
                            if (explosionLenght >= 8)
                            {
                                explosionLenght = 1;
                            }
                        }
                    }
                }
            }
        }

    }
}

int waitTime = 1;
int shrinkTime = 1;


id circle0done = [CCSequence actionOne:[CCMoveTo actionWithDuration:waitTime position:c0TileCoord] two:[CCScaleTo actionWithDuration:shrinkTime scale:0]];
id circle1done = [CCSequence actionOne:[CCMoveTo actionWithDuration:waitTime position:c1TileCoord] two:[CCScaleTo actionWithDuration:shrinkTime scale:0]];
id circle2done = [CCSequence actionOne:[CCMoveTo actionWithDuration:waitTime position:c2TileCoord] two:[CCScaleTo actionWithDuration:shrinkTime scale:0]];
id circle3done = [CCSequence actionOne:[CCMoveTo actionWithDuration:waitTime position:c3TileCoord] two:[CCScaleTo actionWithDuration:shrinkTime scale:0]];
id circle4done = [CCSequence actionOne:[CCMoveTo actionWithDuration:waitTime position:c4TileCoord] two:[CCScaleTo actionWithDuration:shrinkTime scale:0]];



[circle0 runAction:circle0done];
[circle1 runAction:circle1done];
[circle2 runAction:circle2done];
[circle3 runAction:circle3done];
[circle4 runAction:circle4done];
\$\endgroup\$
  • \$\begingroup\$ You could replace the enumerated variables by an array, e.g. CCSprint **circles. The giant if-cascades can be replaced by a loop from 2 to explosionLength; bound checking happens in the first few lines, so don't repeat it with the >= 7 and >= 8 tests which cannot return true (?). Once you have rewritten the code to use loops, further simplifications will present themselves. \$\endgroup\$ – amon May 17 '13 at 21:32
  • \$\begingroup\$ forloops! So simple! Why didn't I think of that. Will implement, thanks. @amon \$\endgroup\$ – Oscar Apeland May 17 '13 at 21:41
1
\$\begingroup\$

For someone who of some weird reason needs to do the same thing, here's the code I ended up using.

    -(void)explosionFromPoint:(CGPoint)explosionPoint withSprite:(CCSprite*)sprite;
{
    //int 
    explosionLenght += 1;
    if (explosionLenght >= 7) //Just for testing purposes, don't have a way to increase it naturally. 
    {
        explosionLenght = 1;
    }

    BOOL topB = YES;
    BOOL leftB = YES;
    BOOL bottomB = YES;
    BOOL rightB = YES;

    int bombX =    (explosionPoint.x + 1);
    int bombY =    (explosionPoint.y + 1);
    int bombNegX = (explosionPoint.x - 1);
    int bombNegY = (explosionPoint.y - 1);

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

    if (![self isLocationBombable:top])
    {topB = NO;}
    if (![self isLocationBombable:left])
    {leftB = NO;}
    if (![self isLocationBombable:bottom])
    {bottomB = NO;}
    if (![self isLocationBombable:right])
    {rightB = NO;}

    for (int i = 0; i <= explosionLenght; i++) {

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

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

        CCSprite *circleTop    = [[CCSprite alloc]initWithFile:@"Circle.png"];
        CCSprite *circleLeft   = [[CCSprite alloc]initWithFile:@"Circle.png"];
        CCSprite *circleBottom = [[CCSprite alloc]initWithFile:@"Circle.png"];
        CCSprite *circleRight  = [[CCSprite alloc]initWithFile:@"Circle.png"];

        if ([self isLocationBombable:top] && topB == YES)
        {
            circleTop.position = [self positionForTileCoord:top];
            [self addChild:circleTop];
            id fadeTop = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleTop.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circleTop runAction:fadeTop];
        }
        if ([self isLocationBombable:left] && leftB == YES)
        {
            circleLeft.position = [self positionForTileCoord:left];
            [self addChild:circleLeft];
            id fadeLeft = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleLeft.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circleLeft runAction:fadeLeft];
        }
        if ([self isLocationBombable:bottom] && bottomB == YES)
        {
            circleBottom.position = [self positionForTileCoord:bottom];
            [self addChild:circleBottom];
            id fadeBottom = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleBottom.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circleBottom runAction:fadeBottom];
        }
        if ([self isLocationBombable:right] && rightB == YES)
        {
            circleRight.position = [self positionForTileCoord:right];
            [self addChild:circleRight];
            id fadeRight = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleRight.position] two:[CCScaleTo actionWithDuration:1 scale:0]];
            [circleRight runAction:fadeRight];
        }
    }

    [currentBombs addObject:sprite];

    NSLog(@"Explosion done, call checkdamage");

    [self schedule:@selector(checkDamageForBomb)];
    [self performSelector:@selector(removeSprite:) withObject:sprite afterDelay:3];
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Feel free to comment about further improvements to this code. \$\endgroup\$ – Oscar Apeland May 27 '13 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.