0

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

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

   -(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)];
 }
4

1 回答 1

0

请参考以下示例,

https://github.com/jasarien/JSController

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

于 2013-07-19T09:21:07.683 回答