【问题标题】:CCMoveTo: speed of moveCCMoveTo:移动速度
【发布时间】:2011-05-13 06:47:26
【问题描述】:

我对 CCMoveTo 有一些问题:

id actionMove = [CCMoveTo actionWithDuration:3 position:ccp(pointBoard[x][y].x,pointBoard[x][y].y)];

例如,我的精灵从ccp(20,460) 开始移动并移动到ccp(20,0) 没关系。但是当精灵需要移动到ccp(20,200) 时,移动速度会变慢。 我需要以相同的速度移动精灵。我该怎么做?

谢谢。

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案1】:

    您需要计算 [start] 和 [end] 点之间的“距离”,然后您可以计算“持续时间”,以便您的精灵以恒定速度移动。类似的,

    浮动速度 = 1; // 在这里你定义你想要使用的速度。

    CGPoint start = sprite.position; // here you will get the current position of your sprite.
    CGPoint end = ccp(pointBoard[x][y].x,pointBoard[x][y].y);
    
    float distance = ccpDistance(start, end); // now you have the distance
    
    float duration = distance/speed;  // here you find the duration required to cover the distance at constant speed
    

    现在您可以调用 CCMoveTo 函数并提供上述计算的持续时间以使您的精灵以相同的速度移动。

    希望对您有所帮助..!!

    【讨论】:

      【解决方案2】:

      要使您的移动速度在所有距离上保持恒定,请定义移动精灵所需的速度,并使用您小时候在物理课上学过的速度-时间-距离公式从三者中找出未知数。

      float speed = 50.0f;
      id duration = ccpDistance(sprite.position, pointBoard[x][y]) / speed;
      id moveAction = [CCMoveTo actionWithDuration:duration position:pointBoard[x][y]];
      

      【讨论】:

      • 速度应该是一个浮点数,而不是一个 id
      【解决方案3】:

      这里精灵的速度根据距离而变化。如果从 ccp(20,460) 到 ccp(20,0) 的距离与 ccp(20,0) 到 ccp(20,200) 的距离相同。速度保持不变。但是如果距离变化,则速度会相应变化(如果持续时间相同)。

      如果你想要更快的速度,你可以减少时间。

      【讨论】:

      • 我可以在没有持续时间的情况下移动精灵,只需设置速度和位置吗?
      • 如果您不想要持续时间。您可以将持续时间设置为 0。但是您无法使用 MoveTo 控制精灵的速度(您可以通过增加它们的位置来手动移动)。
      【解决方案4】:

      只需使用简单的数学(时间 = 距离/速度)来计算 moveAction 所需的时间。

      float speed = 13.0;
      CGPoint startPoint = ccp(20,300);
      CGPoint endPoint   = ccp(20,100);
      
      float time = ccpDistance(startPoint, endPoint) / speed; 
      id moveAction = [CCMoveTo actionWithDuration:time position:endPoint];
      

      【讨论】:

        【解决方案5】:

        您可以加快速度变量的保持和位置保持位置:CGPointMake 动作。

        漂浮速度 = 3.67;

        CCMoveTo *moveuserleft; CCMoveTo *moveuserleft2;

            moveuserleft = [CCMoveTo actionWithDuration:speed position:CGPointMake(235*scaleX,200*scaleY)];
        
            moveuserleft2 = [CCMoveTo actionWithDuration:speed  position:CGPointMake(360*scaleX,200*scaleY)];
        
            CCSequence *scaleSeqleft = [CCSequence actions:moveuserleft,moveuserleft2, nil];
        
            [user runAction:scaleSeqleft];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-04
          • 1970-01-01
          相关资源
          最近更新 更多