尝试以恒定的速度平滑地旋转我的精灵。但它会降低速度
(我正在使用cocos2d for iphone)
首先我将向您解释我的代码:(重要部分)
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position)));
plane.dstAngle = dstAngle;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position)));
plane.dstAngle = dstAngle; }
如果我触摸屏幕,我会计算我的平面和触摸位置之间的角度。
现在我设置了飞机的“目标”角度。 我的“飞机”是一个子类。
在我的头文件中,我有这些浮点值:
//this is in my plane subclass
float diffAngle_;
float startAngle_;
float dstAngle_;
float smoothRotationSpeed_;
编辑:在层的初始化中,我将“smoothRotationSpeed”设置为一个值。 “rotationTick”方法已安排。 我有一个刻度方法来旋转飞机(也在飞机子类中)
-(void)rotateTick:(ccTime)dt {
startAngle_ = [self rotation];
if (startAngle_ > 0)
startAngle_ = fmodf(startAngle_, 360.0f);
else
startAngle_ = fmodf(startAngle_, -360.0f);
diffAngle_ =dstAngle_ - startAngle_;
if (diffAngle_ > 180)
diffAngle_ -= 360;
if (diffAngle_ < -180)
diffAngle_ += 360;
[self setRotation: startAngle_ + diffAngle_ * dt]; //smooth rotate
}
-(void)setSmoothRotationSpeed:(float)smoothRotationSpeed {
[self schedule:@selector(rotateTick:)]; //this will be called if the "smoothRotationSpeed" value is changed
}
现在的问题是,如果我触摸一个位置,精灵会正确旋转到它,但一开始它旋转得有点快,然后就会失去速度
。到目的地角度它移动得越慢...
但我希望它以恒定的速度移动。这个“恒定”速度是在“smoothRotationSpeed”值中定义的,但如何在我的代码中实现它?
如果我将间隔设置为“smoothRotationSpeed”值,它就不会平滑。它会滞后。
有人愿意帮助我吗?
(I'm using cocos2d for iphone)
First I'll explain you my code: (the important parts)
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position)));
plane.dstAngle = dstAngle;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position)));
plane.dstAngle = dstAngle; }
If I touch the screen I calculate the angle between my plane and the touch location.
Now I set the "destination" angle of the plane.
My "plane" is a subclass.
In my header file I have these float values:
//this is in my plane subclass
float diffAngle_;
float startAngle_;
float dstAngle_;
float smoothRotationSpeed_;
EDIT: In my init of the layer I set the "smoothRotationSpeed" to a value. The "rotationTick" method IS scheduled.
I have a tick method to rotate the plane (also in the plane subclass)
-(void)rotateTick:(ccTime)dt {
startAngle_ = [self rotation];
if (startAngle_ > 0)
startAngle_ = fmodf(startAngle_, 360.0f);
else
startAngle_ = fmodf(startAngle_, -360.0f);
diffAngle_ =dstAngle_ - startAngle_;
if (diffAngle_ > 180)
diffAngle_ -= 360;
if (diffAngle_ < -180)
diffAngle_ += 360;
[self setRotation: startAngle_ + diffAngle_ * dt]; //smooth rotate
}
-(void)setSmoothRotationSpeed:(float)smoothRotationSpeed {
[self schedule:@selector(rotateTick:)]; //this will be called if the "smoothRotationSpeed" value is changed
}
The problem is now if I touch a location the sprites rotates to it correctly but at first it rotates a little bit fast and than it looses it's speed..
The nearer it is to the destination angle the slower it moves...
But I want it to move at a constant speed. This "constant" speed is defined in the "smoothRotationSpeed" value but how can I implement it in my code?
If I set the interval to the "smoothRotationSpeed" value it wouldn't be smooth. It would lag.
Would anyone like to help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将根据目标角度和当前角度之间的差异来增加精灵的旋转,因此当前角度越接近目标角度,应用到当前旋转的增量就越小。但从你所说的来看,这不是你想要的。所以你可以简单地做这样的事情:
You're incrementing the rotation of your sprite based on the difference between the target and current angles, so the closer the current angle is to the target angle, the smaller is the increment you're applying to the current rotation. But from what you're saying, that's not what you want. So you could simply do something like this: