欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

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

来源: 开发者 投稿于  被查看 38362 次 评论:153

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


 

准备缩短反弹棒素材

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

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

这里写图片描述vc/y0rK74bHktPMs1eK+zbK7ysfO0sPHz+vSqrXEwcsuPC9wPg0KPGgyIGlkPQ=="创建缩短道具星">创建缩短道具星

我们用红色的星星表示缩短道具,所以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];
}

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

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

这里写图片描述

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

用户评论