Cocos2dx draw a rope connecting any two points [always connect the touch point to the center of the screen]

Source: Internet
Author: User
Recently, I am engaged in a connected game. I mentioned in my previous blog that I used DrawNode to draw a rough line. (Blog. csdn. netno99esarticledetails38823673) after the wire is drawn, I want to use a rope to replace it. Due to the forgotten trigonometric function, the time was delayed. Today I finally finished it and recorded it. Rope image used: found online,

Recently, I am engaged in a connected game. I mentioned in my previous blog that I used DrawNode to draw a rough line. (Http://blog.csdn.net/no99es/article/details/38823673) line drawn out, I want to replace it with a rope, due to the trigonometric function forget light, delay the time, today finally finished, recorded. Rope image used: found online,

Recently, I am engaged in a connected game. I mentioned in my previous blog that I used DrawNode to draw a rough line. Http://blog.csdn.net/no99es/article/details/38823673)

After the line is drawn, I want to use a rope to replace it. Due to the forgotten trigonometric function, the time was delayed. Today I finally finished it and recorded it.

Rope image used:


Horizontal rope found online.

Principle:

Use the setTextureRect method to display the rope of the specified length, and then rotate it according to the coordinate of the touch point.

The following two key codes are provided:

/*
* Add a touch listener
*/
Auto myListener = EventListenerTouchOneByOne: create ();
// MyListener-> setSwallowTouches (true );
MyListener-> onTouchBegan = CC_CALLBACK_2 (LineTest: TouchBegan, this );
MyListener-> onTouchMoved = CC_CALLBACK_2 (LineTest: TouchMoved, this );
MyListener-> onTouchEnded = CC_CALLBACK_2 (LineTest: TouchEnded, this );
_ EventDispatcher-> addEventListenerWithSceneGraphPriority (myListener, this );

In the touch method, Began is similar to the Moved code, so everyone can play it freely ~~

Bool LineTest: TouchBegan (Touch * touch, Event * event)
{
Spline-> setVisible (true );
// Length, the distance from the touch point to the center of the screen
Int texturehight = sqrt (pow (touch-> getLocation (). x-visibleSize. width/2 + origin. x), 2) + pow (touch-> getLocation (). y-visibleSize. height/2 + origin. y), 2 ));
Spline-> setTextureRect (Rect (0, 0, texturehight, 40 ));
// Rotate
Float x = touch-> getLocation (). x-visibleSize. width/2.0f + origin. x;
Float y = touch-> getLocation (). y-visibleSize. height/2.0f + origin. y;
Float f = GetRotationDegree (x, y );
Spline-> setRotation (f );
Return true;
}

//// // Get the Rotation Angle //////////////
Float LineTest: GetRotationDegree (float x, float y)
{
If (x = 0 & y = 0)
{
Return 0;
}
Float result;
If (x = 0 & y> 0)
{
Return 90;
}
Else if (x = 0 & y <0)
{
Return 180;
}
Else
{
Float temp = y/x;
Result =-atan (temp) * 180.0/M_PI;
If (x> 0)
{
Return result;
}
Else
{
Return result + 180;
}
}
}

Effect:

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.