我有一个 Sprite (例如纸飞机)。我想让它像下图一样移动。
我可以使用很多 MoveTo
和 RotateBy
Action 来按点定义路径,但这对我来说似乎是个坏主意。如何实现?
最佳答案
我认为发布一个答案可能会很好,该答案显示了如果您对 sprite 有明确的控制,更新将如何工作的基础知识。
我不确定您使用的是 Cocos2d 还是 Cocos2d-X,但该技术适用于任何一种情况。代码是使用 Cocos2d-x 的 C++。
想法是,根据时间,您(手动)更新 Sprite 的位置。 Sprite 在任何时候的位置都由动画开始后的秒数决定。该线名义上遵循从 (x0,y0) 到 (x1,y0) 的直线路径。然后,您可以使用某种三角函数将该线投影到以任意角度绘制的线上。这使得能够沿任何方向具有正弦路径。
基本代码如下(主要工作在UpdateAnimation()中完成):
// This assumes the frame rate is relatively constant
// at 60 fps.
const double SECONDS_PER_TICK = 1.0/60;
const double DURATION = 8.0; // Seconds for total animation.
const double X_START = 100; // Pixels
const double Y_START = 200; // Pixels
const double X_STOP = 800; // Pixels
const double X_SPEED = (X_STOP-X_START)/DURATION;
const double Y_PERIOD = 4.0; // Seconds for y cycle.
const double Y_HEIGHT = 100;
const double LAUNCH_ANGLE = M_PI/4; // Angle for line.
const CCPoint ANCHOR(X_START,Y_START);
CCPoint RotatePointAboutAnchor(const CCPoint& pt,double theta,const CCPoint& anchor)
{
double xPrime = cos(theta) * (pt.x-anchor.x) - sin(theta) * (pt.y-anchor.y) + anchor.x;
double yPrime = sin(theta) * (pt.x-anchor.x) + cos(theta) * (pt.y-anchor.y) + anchor.y;
return CCPoint(xPrime,yPrime);
}
void HelloWorld::InitAnimation()
{
_ticks = 0;
_ticksTotal = DURATION/SECONDS_PER_TICK;
}
void HelloWorld::UpdateAnimation()
{
if(_ticks <= _ticksTotal)
{
double seconds = _ticks*SECONDS_PER_TICK;
double xPos = X_START + seconds*X_SPEED;
double yPos = Y_START + Y_HEIGHT*sin(seconds*2*M_PI/Y_PERIOD);
CCPoint pos = RotatePointAboutAnchor(CCPoint(xPos,yPos), LAUNCH_ANGLE, ANCHOR);
// Set the position of the sprite
_sprite->setPosition(pos);
CCLOG("Tick: %d, Seconds: %5.2f, Position: (%f,%f)",_ticks,seconds,pos.x,pos.y);
if(_ticks%10 == 0)
{ // Add a trail
CCSprite* marker = CCSprite::create("Icon-72.png");
marker->setScale(0.1);
marker->setPosition(_sprite->getPosition());
marker->setZOrder(50);
addChild(marker);
}
// Increment the ticks count for next time.
_ticks++;
}
}
void HelloWorld::draw()
{
CCLayer::draw();
CCPoint start;
CCPoint stop;
start = RotatePointAboutAnchor(CCPoint(X_START,Y_START), LAUNCH_ANGLE, ANCHOR);
stop = RotatePointAboutAnchor(CCPoint(X_STOP,Y_START), LAUNCH_ANGLE, ANCHOR);
ccDrawLine(start,stop);
start = RotatePointAboutAnchor(CCPoint(X_START,Y_START+Y_HEIGHT), LAUNCH_ANGLE, ANCHOR);
stop = RotatePointAboutAnchor(CCPoint(X_STOP,Y_START+Y_HEIGHT), LAUNCH_ANGLE, ANCHOR);
ccDrawLine(start,stop);
start = RotatePointAboutAnchor(CCPoint(X_START,Y_START-Y_HEIGHT), LAUNCH_ANGLE, ANCHOR);
stop = RotatePointAboutAnchor(CCPoint(X_STOP,Y_START-Y_HEIGHT), LAUNCH_ANGLE, ANCHOR);
ccDrawLine(start,stop);
}
void HelloWorld::onEnterTransitionDidFinish()
{
InitAnimation();
scheduleUpdate();
}
void HelloWorld::onExitTransitionDidStart()
{
unscheduleUpdate();
}
void HelloWorld::update(float dt)
{
UpdateAnimation();
}
我画了一些标记来显示路径,并在应该遵循的路径“周围”画了线。这是它的样子:
您可以根据需要更改 LAUNCH_ANGLE 以使其沿不同角度移动。
显然这不是生产代码,但它确实展示了您可以在任何方向上遵循正弦路径的想法。你应该把它封装成更符合你的应用程序的东西。
整个代码库是available on git hub .
还有更多关于此类内容的帖子 in this blog .
关于ios - 如何让 Sprite 在cocos2d中以正弦曲线运动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22893171/