How to continuously run my pendulum equation in cocos2d (Objective-C)?

*爱你&永不变心* 提交于 2019-12-12 04:36:52

问题


I got a working equation for the rotation angle of my pendulum as seen in the code below:

const float gravity = 9.8;

float L = 0.01;

double CurrentTime = CACurrentMediaTime();

double angle = 120 * sin(sqrt((gravity/L)*CurrentTime));

But what I can't seam to figure out is how to make the sprite rotate continuously so that it acts just like a pendulum. I only make it move to the current value of the angle at the moment I click run, then it stopes when it reaches that position. Here is what I tried to do but only gave the result just explained:

[_claw runAction:[CCActionRepeatForever actionWithAction:[CCActionRotateTo actionWithDuration:3.0 angle:angle]]];

So how do I make it rotate to the angle value continuously, so that it swings back and forth? Any help is highly appreciated :D


回答1:


Try this;

id rotation = [CCActionRotateTo actionWithDuration:3.0 angle:angle];

[_claw runAction:[CCActionRepeatForever actionWithAction:[CCSequence actions:rotation, [rotation reverse], nil]]];

If reverse method not found

id rotation = [CCActionRotateTo actionWithDuration:3.0 angle:angle];
id reverseRotation = [CCActionRotateTo actionWithDuration:3.0 angle:_claw.rotation];
[_claw runAction:[CCActionRepeatForever actionWithAction:[CCSequence actions:rotation, reverseRotation , nil]]];


来源:https://stackoverflow.com/questions/26688817/how-to-continuously-run-my-pendulum-equation-in-cocos2d-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!