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



嗨。如何制作只能在四个方向上移动的操纵杆?有人能给我一些建议吗?

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

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

参考以下示例

https://github.com/jasarien/JSController

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

最新更新