首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >雪碧运动过程中垂直位置的变化

雪碧运动过程中垂直位置的变化
EN

Stack Overflow用户
提问于 2014-06-28 02:07:34
回答 1查看 365关注 0票数 1

我的iOS应用程序中有精灵,水平地在屏幕上移动,从左到右,从右到左。对于大多数这些精灵来说,一条直线或一条明确的弧线是可以接受的.我有一些精灵,然而,我想让移动更随意一点,特别是改变垂直位置的雪碧,因为它移动。

在下面的代码中,"y_variant_“是一个ivar,它指定了我想要应用的差异程度。对于鸟类这样的自然物体,或者像纸飞机这样的人造物体来说,这是一个较小的价值,它们在飞行时只能上下移动一小部分,而对于昆虫和蜜蜂这样的自然物体来说,它的价值要大得多,因为它们的变化要大得多。

这是我目前正在使用的代码(sprite的"update:“方法):

代码语言:javascript
复制
//  track/vary the object as it moves along its path...
- ( void ) update: ( ccTime ) delta

{

    //  ask director for the window size...
    CGSize const size = [ [ CCDirector sharedDirector ] winSize ];

    //  get the current position of the projectile...
    CGPoint const ptRaw = [ self position ];

    //  create a variant of the vertical position for "intelligent" items...
    CGPoint const pt = ( y_variant_
                       ? ccp( ptRaw.x
                            , ptRaw.y + y_variant_ * (float)( rand( ) % 25 ) * 0.02f - fmodf( ptRaw.x, y_variant_ )
                            )
                       : ptRaw
                       );
    [ self setPosition: pt ];
        :
        :
        :
        //  if we're supposed to stop the projectile on impact (a "brick wall", etc.)...
        if ( fStopProjectile == ObjectImpactActionStop )

            {

            //  object impact, if this is a "smart" projectile (i.e., something with a brain)...
            if ( y_variant_ )

                {

                //  move the projectile around the object...
                CCAction * const move = [ CCMoveTo actionWithDuration: 0.25f
                                                             position: ccpAdd( [ self position ]
                                                                             , ccp( - CS_IMAGE_OBJECT_W
                                                                                  , CS_IMAGE_OBJECT_H
                                                                                  )
                                                                             )
                                        ];

                [ self runAction: move ];

                }  // end "smart" projectile

            }  //  end projectile impact
        :
        :
        :
    return;

}  //  end update

你会注意到与固体物体碰撞的逻辑:如果一个固体障碍物(墙、建筑物等)遇到时,该对象将向后移动,以避免障碍物。

我遇到的问题是,对于“”这个更大的值来说,观察到的运动是非常的不稳定的,我希望它看起来更流畅.

即使你没有一个解决方案,任何关于什么可以改进或调查的评论将不胜感激。

谢谢你的帮助。

p.s.:如果你觉得自己雄心勃勃,用户的建议之一就是让蜜蜂偶尔向后转一圈,然后继续前进。一旦我解决了当前的“弹跳”问题,我会把它合并起来,所以请记住.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-30 21:49:14

你的问题没有简单的答案。但是,如果您使用的是Cocos2d,我将研究使用操作而不是手动定位精灵。因此,重申一下,我建议您研究使用操作。但是,这里有一个简单的例子,如果您坚持手动定位,可能会有所帮助:

代码语言:javascript
复制
#define kAmountToMovePerFrameInY 10.0 / 60.0

@property float posYCurrentlyMovingTo;
@property SomeSpriteClass *theSpriteMoving;
@property float someXPosition;
@property float nextMoveAmount;


-(void)doStuffDuringUpdate
{


    if (_theSpriteMoving.position.y < labs(_posYCurrentlyMovingTo)) {
        //sprite is still moving to its position

        _theSpriteMoving.position = CGPointMake(_someXPosition, _theSpriteMoving.position.y + _nextMoveAmount);

    }else{
        float variationAmountInY = arc4random_uniform(200) - 100.0;
        _posYCurrentlyMovingTo = variationAmountInY;
        _nextMoveAmount = variationAmountInY * kAmountToMovePerFrameInY;


    }


}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24462955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档