(NO.00004)iOS实现打砖块游戏(十三):伸缩自如,我是如意金箍棒(下)!

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.

如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;)


准备缩短反弹棒素材

和上一篇类似,我们如法炮制一张缩短后反弹棒的素材.

打开SpriteBuilder,新建StickShorter.ccb文件,按下图绘制其sprite帧和物理对象:

(NO.00004)iOS实现打砖块游戏(十三):伸缩自如,我是如意金箍棒(下)!

注意,在Ai中缩小和放大原来反弹棒的图片时,只要沿X轴一个方向放大即可,否则在Y轴方向也会变大,这就不是我们想要的了.

创建缩短道具星

我们用红色的星星表示缩短道具,所以spawStar中是这样写的:

case brkColorRed:
            star = [Star starWithType:starTypeStickShorter];
            break;

在GameScene.m中,在星星与反弹棒碰撞的代码中,加入如下代码:

case starTypeStickShorter:
            @synchronized(self){
                [self scheduleBlock:^(CCTimer *timer){
                    [Star doStickShorterWork:self.stickInGameScene];
                } delay:0];
            }
            break;

好了,最后我们回到Star.m中添加doStickShorterWork方法:

+(void)doStickShorterWork:(Stick *)stick{
    GameScene *gameScene = [GameScene sharedGameScene];
    CCPhysicsNode *physicsWorld = (CCPhysicsNode*)stick.parent;

    @synchronized(gameScene){
        if ([stick.name isEqualToString:@"stickShorter"]) {
            return;
        }

        if ([stick.name isEqualToString:@"stickLonger"]) {
            Stick *stickNormal = [Stick stickNormal];
            stickNormal.position = stick.position;
            [stick removeFromParent];
            //[physicsWorld removeChild:stick cleanup:YES];

            [physicsWorld addChild:stickNormal];
            gameScene.stickInGameScene = stickNormal;
            return;
        }
    }

    CGPoint position = stick.position;

    __block Stick *stickShorter;

    @synchronized(gameScene){
        stickShorter = [Stick stickShorter];
        [stick removeFromParent];
        //[physicsWorld removeChild:stick cleanup:YES];
        stickShorter.position = position;
        [physicsWorld addChild:stickShorter];
        stickShorter.visible = NO;
        gameScene.stickInGameScene = stickShorter;

        CCSprite *stickNode = (CCSprite*)[CCBReader load:@"Elements/StickNode"];
        stickNode.position = stickShorter.position;
        [gameScene addChild:stickNode z:50];

        CCActionScaleTo *shorterAction = [CCActionScaleTo actionWithDuration:0.4f scaleX:0.5f scaleY:1.0f];
        CCActionCallBlock *blk = [CCActionCallBlock actionWithBlock:^{
            [stickNode removeFromParent];
            stickShorter.visible = YES;
        }];
        CCActionSequence *seq = [CCActionSequence actions:shorterAction,blk,nil];
        [stickNode runAction:seq];
    }

    [stickShorter scheduleBlock:^(CCTimer *timer){
        @synchronized(gameScene){
            Stick *stickNormal = [Stick stickNormal];
            stickNormal.position = stickShorter.position;
            [stickShorter removeFromParent];
            [physicsWorld addChild:stickNormal];
            gameScene.stickInGameScene = stickNormal;
        }
    } delay:10];
}

大家可以和变长的对应代码对比下,基本都是一样的.

下面编译运行游戏,效果如下:

(NO.00004)iOS实现打砖块游戏(十三):伸缩自如,我是如意金箍棒(下)!

大家可以在变短和变长中添加更多的特效,脑洞打开吧,童鞋们 ;)

上一篇:异步对象(XMLHttpRequest)的帮助脚本


下一篇:(NO.00004)iOS实现打砖块游戏(十五):导弹发射道具的实现(上)