首页 > 代码库 > 实现精灵沿着圆形轨迹运动

实现精灵沿着圆形轨迹运动

(原文地址:http://blog.csdn.net/while0/article/details/26008033)


使用样条曲线模拟圆形轨迹,当分割数量足够大时,逐渐逼近圆形。

    Point pos(100, 100); //start point
    m_animSprite->setPosition(pos);

    int count = 8;
    float angle = 360.f/count;
    float radius = 50.f;
    auto array = PointArray::create(count + 1);
    Point pt0(radius, 0.f), pt;
    for (int i = 0; i <= count; i++)
    {
        pt = pt0 + Point(-radius, 0);
        array->addControlPoint(pt);
        pt0 = pt0.rotateByAngle(Point::ZERO, CC_DEGREES_TO_RADIANS(angle));
    }
    auto action = CardinalSplineBy::create(3, array, 0);
    auto seq = RepeatForever::create(action);
    m_animSprite->runAction(seq);