【问题标题】:How to move a sprite at a certain angle with a joystick如何用操纵杆将精灵移动到某个角度
【发布时间】:2011-07-15 19:01:16
【问题描述】:

嗨,我终于在 cocos2d 中制作了一个可以工作的操纵杆。我能够将精灵旋转到操纵杆拇指或帽子“指向”的确切角度。但是,我无法将精灵朝同一个方向移动。有没有一种简单的方法可以按照我设置旋转代码的方式移动精灵?如果您的拇指仍然按下但不移动操纵杆,还有没有办法让它保持移动? PS 这段代码都在 TouchesMoved 方法中。聚苯乙烯。 cap 是拇指,pad 是操纵杆背景,Sprite2 是我要移动的精灵。 (95, 95) 是 pad sprite 的中心。

if(capSprite.position.x>=padSprite.position.x){
            id a3 = [CCFlipX actionWithFlipX:NO];
            [sprite2 runAction:a3];
        }
        if(capSprite.position.x<=padSprite.position.x){
            id a4 = [CCFlipX actionWithFlipX:YES];
            [sprite2 runAction:a4];
        }


        CGPoint pos1 = ccp(95, 95);
        CGPoint pos2 = ccp(capSprite.position.x, capSprite.position.y);
        int offX = pos2.x-pos1.x;
        int offY = pos2.y-pos1.y;


        float angleRadians = atanf((float)offY/(float)offX);
        float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
        float theAngle = -1 * angleDegrees;
        sprite2.rotation = theAngle;

【问题讨论】:

    标签: cocos2d-iphone sprite move joystick


    【解决方案1】:

    我不熟悉 cocos2d,但我快速浏览了文档,这个示例可能对您有用:

       if keys[key.UP]:
           self.target.acceleration = (200 * rotation_x, 200 * rotation_y)
    

    我已经写了一个很长的解释来回答你的第二个问题,但我相信这个“​​self.target.acceleration”也可以解决这个问题。你可以在cocos2d API documentation阅读更多内容。

    【讨论】:

      【解决方案2】:

      我通常做的是获取角度,使用 ccpForAngle(float) 将其转换为 CGPoint,然后将 CGPoint 乘以一个值:

      float angle = whatever;
      CGPoint anglePoint = ccpForAngle(angle);
      // You will need to play with the mult value
      angle = ccpMult(angle, 2.5);
      // This also works with box2D or probably Chipmunk.
      sprite.position = angle;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多