0

Hi. How can I make the joystick which is movable only in four directions? Can anyone give me some suggestions?

Write now I am using this code for joystick. How to make this one to allow only in four directions?

   -(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)];
 }
Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55
Raj
  • 11
  • 5

1 Answers1

0

Refer the following sample,

https://github.com/jasarien/JSController

It consists of four direction button, free drag view and two buttons, it will be helpful for your question.

Venk
  • 5,949
  • 9
  • 41
  • 52
  • Hi Sorry for asking this actually am new to cocos2d .Can i use this one in cocos2d game....? – Raj Jul 19 '13 at 09:50
  • this sample is for an idea only... just convert the method as you need from this sample – Venk Jul 19 '13 at 10:29