代码之家  ›  专栏  ›  技术社区  ›  AliBZ

AtlassPriteManager动画

  •  0
  • AliBZ  · 技术社区  · 16 年前

    我正在使用AtlassPriteManager和AltassPrite创建一个文件的fram动画帧。我想写一些东西,首先显示一个简单的图片,没有任何动画,例如当我触摸它,它显示一些动画,并返回到第一个位置和帧。如果没有动画,我就无法使用以下代码显示第一帧:

    Sprite *checker = [Sprite spriteWithFile:@"test.png"];
    float frameWidth = [checker boundingBox].size.width / frameNumber;
    float frameHeight = [checker boundingBox].size.height;
    
    AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:fileName];
    AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(pos.x, pos.y, frameWidth, frameHeight) spriteManager:mgr];
    sprite.position = ccp(pos.x, pos.y);
    [mgr addChild:sprite];
    
    [layer addChild:mgr];
    
    AtlasAnimation* animation = [AtlasAnimation animationWithName:@"Animation" delay:delay];
    assert( animation != nil );
    
    for(int i = 0; i < frameNumber; i++){
        [animation addFrameWithRect:CGRectMake(i * frameWidth, 0, frameWidth, frameHeight)];        
    }
    
    id action = [Animate actionWithAnimation:animation];
    assert( action != nil );
    
    id repeatAction;
    if(repeatNumber == 0){
        repeatAction = [RepeatForever actionWithAction:action];
    }else{
        repeatAction = [Repeat actionWithAction:action times:repeatNumber];
    }
    
    [sprite runAction:repeatAction];
    

    知道怎么做吗? 提前十倍

    2 回复  |  直到 16 年前
        1
  •  0
  •   nash George D Girton    16 年前

    更新的答案

    我想我没有按你的意思理解你的问题。

    正如我现在理解的,你有一个精灵,你有一个动画。但当动画完成后,它不会返回到您希望精灵静止时设置为的帧。

    在我们的代码中,您使用 actionWithAnimation: ,您是否尝试将其设置为 actionWithAnimation:restoreOrigionalFrame: 然后设置 restoreOrigionalFrame 部分是吗?

    这应该渲染动画,但当它停止时,返回到动画之前的帧。

    或者,您可以使操作运行,然后当它停止时,通过调用 setTextureRect: 在地图集上。

    [sprite setTextureRect:CGRectMake( x, y, width, height )];
    

    下面是我的旧答案:


    您现在拥有的代码将立即对其进行动画处理。

    如果您希望动画在触摸时开始,则必须检查触摸。然后添加代码以在接收触摸的方法中启动动画。

    在cocos2d下载中有关于如何使用touch的示例代码。

    基本上:让你的雪碧 TargetedTouchDelegate (或者创建一个新的对象来实现,但这有点麻烦)并实现 -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

        2
  •  0
  •   Aram    16 年前

    我认为你需要结合callfunc动作和sequence动作。

    从您的代码:

    
    }
        .....
        .....
        id action = [Animate actionWithAnimation:animation];
        assert( action != nil );
    
        id repeatAction;
        if(repeatNumber == 0){
                repeatAction = [RepeatForever actionWithAction:action];
        }else{
                repeatAction = [Repeat actionWithAction:action times:repeatNumber];
        }
        id actionCallFunc = [CallFunc actionWithTarget:self selector:@selector(resetSprite)];
        id actionSequence = [Sequence actions: repeatAction, actionCallFunc, nil];
    
        [sprite runAction:repeatAction];
        ....
    }
    
    -(void)resetSprite{
        [sprite runAction:repeatAction];
    }
    
    

    首先将执行您的repeataction,当它结束时,将执行actioncallfunc,调用resetsprite方法,在那里您可以用您的sprite执行您想要的操作。