需要制作四个方向的操纵杆

时间:2013-07-19 07:21:30

标签: iphone ios cocos2d-iphone joystick

您好。如何制作只能在四个方向上移动的操纵杆?谁能给我一些建议?

现在写我正在使用此代码进行操纵杆。如何让这个只允许四个方向?

   -(void) trackVelocity:(CGPoint) nodeTouchPoint {
    CGPoint ankPt = self.anchorPointInPoints;

// Get the touch point relative to the joystick home (anchor point)
    CGPoint relPoint = ccpSub(nodeTouchPoint, ankPt);

// Determine the raw unconstrained velocity vector
    CGPoint rawVelocity = CGPointMake(relPoint.x / travelLimit.x,relPoint.y / travelLimit.y);

// If necessary, normalize the velocity vector relative to the travel limits
    CGFloat rawVelLen = ccpLength(rawVelocity);
    velocity = (rawVelLen <= 1.0) ? rawVelocity : ccpMult(rawVelocity, 1.0f/rawVelLen);

// Calculate the vector in angular coordinates
// ccpToAngle returns counterclockwise positive relative to X-axis.
// We want clockwise positive relative to the Y-axis.
    CGFloat angle = 90.0- CC_RADIANS_TO_DEGREES(ccpToAngle(velocity));
    if(angle > 180.0) {
    angle -= 360.0;
    }
//  angularVelocity.radius = ccpLength(velocity);
//  angularVelocity.heading = angle;

   // Update the thumb's position, clamping it within the contentSize of the Joystick
    [thumbNode setPosition: ccpAdd(ccpCompMult(velocity, travelLimit), ankPt)];
 }

1 个答案:

答案 0 :(得分:0)

参考以下示例

https://github.com/jasarien/JSController

它由四个方向按钮,自由拖动视图和两个按钮组成,对您的问题很有帮助。