代码之家  ›  专栏  ›  技术社区  ›  Daniel Bramhall

垂直设置精灵动画

  •  0
  • Daniel Bramhall  · 技术社区  · 11 年前

    我真的很难让一个精灵在屏幕的顶部产生,然后从上到下制作动画。我一直在遵循雷·温德利希斯的教程,创建一个简单的游戏,然而,它会将精灵从右向左移动,我希望它从上到下移动,现在我被卡住了!以下是我的当前代码:

    - (void)addComet:(CCTime)dt
    {
        // Make sprite
        CCSprite *comet = [CCSprite spriteWithImageNamed:@"PlayerSprite.png"];
    
        // Verticle spawn range
        int minY = comet.contentSize.width;
        int maxY = self.contentSize.height - comet.contentSize.height / 2;
        int rangeY = maxY - minY;
        int randomY = (arc4random() % rangeY) + minY;
    
        // Position comets slightly off the screen
        comet.position = CGPointMake(self.contentSize.width + comet.contentSize.width, randomY);
        [self addChild:comet];
    
        // Duration range comets take to fly across screen
        int minDuration = 2.0;
        int maxDuration = 4.0;
        int rangeDuration = maxDuration - minDuration;
        int randomDuration = (arc4random() % rangeDuration) + minDuration;
    
        // Give comet animation
        CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(0, 500)];
        CCAction *actionRemove = [CCActionRemove action];
        [comet runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];
    }
    

    如果有人能给我指出正确的方向,因为我已经在这个问题上坚持了很长时间,只是无法让精灵在屏幕顶部随机生成,然后向下移动到底部。我也看过tweetjump之类的示例代码,但运气不佳。

    1 回复  |  直到 11 年前
        1
  •  0
  •   T. Benjamin Larsen    11 年前

    现在,我的任何项目中都没有Cocos2D,所以不知道是否有错别字,但一般来说,它应该看起来有点像下面的样子。这个想法是所有的彗星都应该从 相同的 Y位置(屏幕外),但具有随机化的水平(x)位置。。。

    - (void)addComet:(CCTime)dt
    {
        // Make sprite
        CCSprite *comet = [CCSprite spriteWithImageNamed:@"PlayerSprite.png"];
    
        NSInteger y = self.contentSize.height; // perhaps + comet.contentSize.height / 2, if the anchorPoint is 0.5, 0.5
    
        // Random horizontal position
        NSInteger maxX = self.contentSize.width;
        NSInteger randomX = (arc4random() % maxX);
    
        // Position comets slightly off the screen
        comet.position = CGPointMake(randomX, y);
        [self addChild:comet];
    
        // Duration range comets take to fly across screen
        NSInteger minDuration = 2.0;
        NSInteger maxDuration = 4.0;
        NSInteger rangeDuration = maxDuration - minDuration;
        NSInteger randomDuration = (arc4random() % rangeDuration) + minDuration;
    
        // Give comet animation
        CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(randomX, 0)]; // Moving it in a straight line vertically
        CCAction *actionRemove = [CCActionRemove action];
        [comet runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];
    }
    
    推荐文章