gpt4 book ai didi

ios - 在移动过程中改变 Sprite 的垂直位置

转载 作者:行者123 更新时间:2023-11-29 12:42:41 25 4
gpt4 key购买 nike

我的 iOS 应用程序中有 Sprite 在屏幕上水平移动,从左到右和从右到左。对于大多数这些 Sprite ,直线或明确定义的弧线是可以接受的。然而,我有一些 sprite,我希望它们移动得更随机一些,特别是在移动时改变 sprite 的垂直位置。

在下面的代码中,“y_variant_”是一个 ivar,它指定了我想要应用的方差程度。对于像鸟这样的自然物体或像纸飞机这样的人造物体,它的值较小,它们在飞行时应该只上下移动少量,但对于像昆虫和蜜蜂这样的自然物体,它的值更大,它们具有更大的方差.

这是我目前正在使用的代码( Sprite 的“更新:”方法):

//  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

您会注意到存在涉及与固体物体碰撞的逻辑:如果遇到固体障碍物(墙壁、建筑物等),该物体会后退并垂直移动以避开障碍物。

我遇到的问题是,对于较大的“y_variant_”值,观察到的运动非常不稳定,我希望它看起来更平滑....

即使您没有解决方案,如果您对可以改进或调查的内容提出任何意见,我们将不胜感激。

感谢您的帮助。

p.s.:如果您有雄心壮志,用户的建议之一是让蜜蜂非常偶尔向后转一圈,然后继续它的路径。一旦我解决了当前的“弹跳”问题,我就会合并它,所以请记住这一点....

最佳答案

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

#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;


}


}

关于ios - 在移动过程中改变 Sprite 的垂直位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24462955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com