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

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

概述最近在搞连线的游戏,之前的博客里也提到了用DrawNode画粗线的方法。(http://blog.csdn.net/no99es/article/details/38823673) 线画出来之后,就想用绳子代替它,由于三角函数忘光了,耽误了时间,今天终于搞完了,记录下来。 使用的绳子图片: 网上找的,横向的绳子。 原理: 用setTextureRect方法,显示指定长度的绳子,然后根据触摸点坐标,

最近在搞连线的游戏,之前的博客里也提到了用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;
}
}
}

效果:

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001708.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存