Cocos2d-x beginner's Guide (6): simulate a touch joystick as a virtual button

Source: Internet
Author: User

There are a lot of such methods. I have found one for you on the internet, but this is not complete yet. How can I determine whether the buttons are left or right. So I added some stupid methods. If you have any good methods, I 'd like to comment out. Thank you.

 

[Cpp]
# Include "cocos2d. h"
Using namespace cocos2d;

Class Joystick:
Public CCLayer
[Cpp]
Public:
Joystick (void );
~ Joystick (void );
Public:
CCPoint centerPoint; // joystick Center
CCPoint currentPoint; // current position of the joystick
Bool active; // whether to activate the joystick
Float radius; // the radius of the joystick.
CCSprite * jsSprite; // joystick instance

//************************************
// Method: Active
// FullName: Joystick: Active
// Access: public
// Returns: void
// Qualifier: Set that the joystick function is available
//************************************
Void Active ();
//************************************
// Method: Inactive
// FullName: Joystick: Inactive
// Access: public
// Returns: void
// Qualifier: the joystick setting function is unavailable.
//************************************
Void Inactive ();
//************************************
// Method: getDirection
// FullName: Joystick: getDirection
// Access: public
// Returns: cocos2d: CCPoint
// Qualifier: gets the joystick direction. The unit vector is returned here.
//************************************
CCPoint getDirection ();
//************************************
// Method: getVelocity
// FullName: Joystick: getVelocity
// Access: public
// Returns: float
// Qualifier: obtains the strength of the joystick.
//************************************
Float getVelocity ();
//************************************
// Method: updatePos
// FullName: Joystick: updatePos
// Access: public
// Returns: void
// Qualifier: refresh the function and submit it to the Calendar Manager.
// Parameter: ccTime dt
//************************************
Void updatePos (ccTime dt );

//************************************
// Method: JoystickWithCenter
// FullName: Joystick: JoystickWithCenter
// Access: public static
// Returns: Joystick *
// Qualifier: Initialize the joystick
// Parameter: CCPoint aPoint joystick Center
// Parameter: float aRadius joystick radius
// Parameter: CCSprite * aJsSprite Joystick Control Point
// Parameter: CCSprite * aJsBg joystick background
//************************************
Static Joystick * JoystickWithCenter (CCPoint aPoint, float aRadius, CCSprite * aJsSprite, CCSprite * aJsBg );
Joystick * initWithCenter (CCPoint aPoint, float aRadius, CCSprite * aJsSprite, CCSprite * aJsBg );

// The following is the rewrite Touch response function.
Virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
Virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent );
Virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent );

LAYER_NODE_FUNC (Joystick );
};
[Cpp]
# Include "myCircle. h"
 
Joystick: Joystick (void)
{
}
 
Joystick ::~ Joystick (void)
{
}
 
Void Joystick: updatePos (ccTime dt)
{
JsSprite-> setPosition (ccpAdd (jsSprite-> getPosition (), ccpMult (ccpSub (currentPoint, jsSprite-> getPosition (), 0.5 )));
}
 
Void Joystick: Active ()
{
If (! Active)
{
Active = true;
Schedule (schedule_selector (Joystick: updatePos); // Add a refresh function
CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this, 0, false); // Add a touch delegate
}
}
 
Void Joystick: Inactive ()
{
If (active)
{
Active = false;
This-> unschedule (schedule_selector (Joystick: updatePos); // Delete the refresh function
CCTouchDispatcher: sharedDispatcher ()-> removeDelegate (this); // delete a delegate
}
}
 
Bool Joystick: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent)
{
If (! Active)
Return false;
 
CCPoint touchPoint = pTouch-> locationInView (pTouch-> view ());
TouchPoint = CCDirector: sharedDirector ()-> convertToGL (touchPoint );
If (ccpDistance (touchPoint, centerPoint)> radius)
Return false;
 
CurrentPoint = touchPoint;
Return true;
}
 
Void Joystick: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent)
{
CCPoint touchPoint = pTouch-> locationInView (pTouch-> view ());
TouchPoint = CCDirector: sharedDirector ()-> convertToGL (touchPoint );
If (ccpDistance (touchPoint, centerPoint)> radius)
{
CurrentPoint = ccpAdd (centerPoint, ccpMult (ccpNormalize (ccpSub (touchPoint, centerPoint), radius ));
}
Else
{
CurrentPoint = touchPoint;
}
}
 
Void Joystick: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent)
{
CurrentPoint = centerPoint;
}
 
CCPoint Joystick: getDirection ()
{
Return ccpNormalize (ccpSub (centerPoint, currentPoint ));
}
 
Float Joystick: getVelocity ()
{
Return ccpDistance (centerPoint, currentPoint );
}
 
Joystick * Joystick: JoystickWithCenter (CCPoint aPoint, float aRadius, CCSprite * aJsSprite, CCSprite * aJsBg)
{
Joystick * jstick = Joystick: node ();
Jstick-> initWithCenter (aPoint, aRadius, aJsSprite, aJsBg );
 
Return jstick;
}
 
Joystick * Joystick: initWithCenter (CCPoint aPoint, float aRadius, CCSprite * aJsSprite, CCSprite * aJsBg)
{
Active = false;
Radius = aRadius;
CenterPoint = aPoint;
CurrentPoint = centerPoint;
JsSprite = aJsSprite;
JsSprite-> setPosition (centerPoint );
AJsBg-> setPosition (centerPoint );
This-> addChild (jsSprite );
This-> addChild (aJsBg );
 
Return this;
}

The following is the result of calling this item.

 


// Determine the direction
CCPoint direct = myCircle-> getDirection ();
CCPoint right = CCPoint (1, 0 );
CCPoint left = CCPoint (-1, 0 );
If (ccpAngle (direct, left) <0.707 & myCircle-> currentPoint. x> myCircle-> centerPoint. x)
{
CCLog ("left ");
}
If (ccpAngle (direct, right) <0.707 & myCircle-> currentPoint. x <myCircle-> centerPoint. x)
{
CCLog ("right ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.