Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dx画连接任意两点的绳子【始终连接触摸点与屏幕中心】大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_607_0@最近在搞连线的游戏,之前的博客里也提到了用DrawNode画粗线的方法。(http://blog.csdn.net/no99es/article/details/38823673

@H_607_0@线画出来之后,就想用绳子代替它,由于三角函数忘光了,耽误了时间,今天终于搞完了,记录下来。

@H_607_0@使用的绳子图片:

@H_607_0@

@H_607_0@网上找的,横向的绳子。

@H_607_0@原理:

@H_607_0@用setTextureRect方法,显示指定长度的绳子,然后根据触摸点坐标,进行旋转。

@H_607_0@下面贴出两段关键代码:

@H_607_0@/*
*添加触摸监听
*/

auto myListener = EventListenerTouchOneByOne::create();
//myListener->setSwallowTouches(true);
@H_792_2@myListener->onTouchBegan = CC_CALLBACK_2(LineTest::TouchBegan,this);
@H_792_2@myListener->onTouchMoved = CC_CALLBACK_2(LineTest::TouchMoved,this);
@H_792_2@myListener->onTouchEnded = CC_CALLBACK_2(LineTest::TouchEnded,this);
_eventDispatcher->addEventListenerWithSceneGraphpriority(myListener,this);

@H_607_0@触摸方法里,Began跟Moved代码差不多,大家自由发挥~~

@H_607_0@bool LineTest::TouchBegan(Touch* touch,Event* event)
{
spline->setVisible(true);
//长度,触摸点到屏幕中心的距离
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,texturehight,40));
//旋转
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;
}

@H_607_0@///////////////////获取旋转角度//////////////
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;
}
}
}

@H_607_0@效果:

大佬总结

以上是大佬教程为你收集整理的cocos2dx画连接任意两点的绳子【始终连接触摸点与屏幕中心】全部内容,希望文章能够帮你解决cocos2dx画连接任意两点的绳子【始终连接触摸点与屏幕中心】所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。