加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx画连接任意两点的绳子【始终连接触摸点与屏幕中心】

发布时间:2020-12-14 19:01:40 所属栏目:百科 来源:网络整理
导读:最近在搞连线的游戏,之前的博客里也提到了用DrawNode画粗线的方法。(http://blog.csdn.net/no99es/article/details/38823673) 线画出来之后,就想用绳子代替它,由于三角函数忘光了,耽误了时间,今天终于搞完了,记录下来。 使用的绳子图片: 网上找的,

最近在搞连线的游戏,之前的博客里也提到了用DrawNode画粗线的方法。(http://blog.csdn.net/no99es/article/details/38823673)

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

使用的绳子图片:


网上找的,横向的绳子。

原理:

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

下面贴出两段关键代码:

/*
*添加触摸监听
*/

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);

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

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;
}

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

效果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读