您当前的位置: 首页 > 

Chipmunk僵尸物理对象的出现和解决(五)

发布时间:2015-11-09 13:27:34 ,浏览量:0

,因为将反弹棒变长和缩短的代码是类似的所以我们只看一个即可,就让我们详细看看缩短的方法吧.

+(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; [physicsWorld removeChild:stick cleanup:YES]; [physicsWorld addChild:stickNormal]; gameScene.stickInGameScene = stickNormal; return; }
    }

    CGPoint position = stick.position; __block Stick *stickShorter; @synchronized(gameScene){
        stickShorter = [Stick stickShorter]; [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]; stickShorter = nil; [physicsWorld addChild:stickNormal]; gameScene.stickInGameScene = stickNormal; }
    } delay:10]; }

代码比较长,我们依次看一下.

首先取得gameScene和物理世界对象的实例.

如果当前stick已经是短棒了,不能再变短了,所以直接退出方法 如果当前stick是长棒,则删除stick,新建一个正常长度的stick.相当于长棒碰到变短五角星时恢复至正常尺寸,然后退出方法.

否则当前应该是正常尺寸的stick需要将其变短,具体做法为:

1.首先在原来stick位置新建一个短的stick,但将其设置为不可见,因为还要有一个变短的动画效果; 2.建立一个不带物理对象的stick节点,并执行变短的动画; 3.在动画结束时将自身删除同时将短stick设为可见.注意动画时间需要适中,既不能太长也不能过短.这个将在iOS游戏开发系列博文中详述. 4.给短的stick添加一个延时10秒调用的block,在10秒后将其删除,在原位置新建一个正常尺寸的stick.相当于10秒后恢复stick正常的尺寸.

以上就是相关的功能代码了.

编译运行App.在游戏运行时偶尔会在屏幕中产生”僵尸棒”,前面说了在物理世界中找不到该对象,更无从删除.

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    108697博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0456s