我的iOS应用程序中有精灵,水平地在屏幕上移动,从左到右,从右到左。对于大多数这些精灵来说,一条直线或一条明确的弧线是可以接受的.我有一些精灵,然而,我想让移动更随意一点,特别是改变垂直位置的雪碧,因为它移动。
在下面的代码中,"y_variant_“是一个ivar,它指定了我想要应用的差异程度。对于鸟类这样的自然物体,或者像纸飞机这样的人造物体来说,这是一个较小的价值,它们在飞行时只能上下移动一小部分,而对于昆虫和蜜蜂这样的自然物体来说,它的价值要大得多,因为它们的变化要大得多。
这是我目前正在使用的代码(sprite的"update:“方法):
// 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.:如果你觉得自己雄心勃勃,用户的建议之一就是让蜜蜂偶尔向后转一圈,然后继续前进。一旦我解决了当前的“弹跳”问题,我会把它合并起来,所以请记住.
发布于 2014-06-30 21:49:14
你的问题没有简单的答案。但是,如果您使用的是Cocos2d,我将研究使用操作而不是手动定位精灵。因此,重申一下,我建议您研究使用操作。但是,这里有一个简单的例子,如果您坚持手动定位,可能会有所帮助:
#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;
}
}https://stackoverflow.com/questions/24462955
复制相似问题