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: