【问题标题】:Move a whole animation sprite sheet移动整个动画精灵表
【发布时间】:2014-01-03 14:06:37
【问题描述】:

我有一些精灵表,我必须永远制作动画,我想将它添加为CCLayer 到我的场景。 稍后,我必须在屏幕上移动整个动画精灵。 因此,例如,我有一些狗走路的动画,来自精灵表,这个动画永远在运行。比我希望能够在制作动画时在屏幕上移动这条狗。

最好的方法是什么? (或正确的方式)

这就是我动画帧的方式:

    CCSprite *boom;
    boom = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%@_00000.png",file]];
    boom.position=touch;
    [self addChild:boom];


    NSMutableArray *animFrames = [NSMutableArray array];
    for(int i = 0; i < 5; i++)

    {

        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:
                                                                                               @"%@_0000%i.png",file,i]];
        [animFrames addObject:frame];
    }

    CCAnimation* boxAnimation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.075f];
    CCAnimate * boxAction = [CCAnimate actionWithAnimation:boxAnimation];
    CCAction *call=[CCCallBlock actionWithBlock:^{[self removeFromParentAndCleanup:YES];}];
    CCAction * sequence=[CCSequence actions:boxAction,[CCHide action],call,nil];
    [boom runAction:sequence];

    return self;

你会如何移动这整个东西?

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案1】:

    有几种方法可以做到这一点。如果您不专注于碰撞检测,那么一种方法是:

    CGPoint egressPosition = ccp(0,0); // figure this out in your app
    float moveDuration = 1.5f ;        // whatever time you compute for desired speed and distance 
    id move = [CCMoveTo actionWithDuration:moveDuration position:egressPosition];
    id spawn = [CCSpawn actions:sequence,move,nil];
    [boom runAction:spawn];
    

    否则,按原样使用您的代码

    [self schedule:@selector(moveBox:)];  // optional, you could do this in update method
    [boom runAction:sequence];
    
    
    -(void) moveBoom:(CCTime) dt {
        CGPoint newPosition;
        delta = ccp(dt*speedX,dt*speedY);       // crude , just to get the idea
        newPosition = ccpAdd(boom.position,delta);
    
        // here you can figure out collisions at newPosition before the collision
        // and do whatever seems appropriate
    
        boom.position = newPosition;
    
    }
    

    【讨论】:

    • 还有一件事,如果我有很多包含精灵的图层,并且我需要进行一些碰撞检测,那么从所有图层收集所有精灵并检查它们之间的碰撞的正确方法是什么?
    • @Curnelious 您是在寻找精灵的边界框或图像本身(例如狗的身体)之间的碰撞吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2014-11-09
    • 2017-06-21
    • 2019-03-10
    • 1970-01-01
    • 2011-12-01
    相关资源
    最近更新 更多