cocos2dx
记录cocos2dx的学习历程和遇到的坑。 版本4.0
坐标系问题
- 对于精灵,坐标系从左下开始向右上增长,x轴从左向右。例如 sprite->setPosition(Vec2(0, 0)); 设置精灵为左下角。
- 对于鼠标,坐标系从左上方开始向右下增长,x轴从左向右。例如 Point pt = e->getLocation(); pt={0,0}代表鼠标位于左上角。
鼠标控制事件
对于鼠标事件,cocos官方文档中对于4.0版本中还未更新。我们可以同过如下方式实现鼠标对于精灵的控制。
static bool mouse_down = false;
auto myMouseListener = EventListenerMouse::create();
myMouseListener->onMouseDown = [=](Event* event)
{
EventMouse* e = (EventMouse*)event;
Point pt = e->getLocation();
pt.y = visibleSize.height - pt.y;
Rect rect = charac->getBoundingBox();
//log("point x:%f,y%f", pt.x, pt.y);
if (rect.containsPoint(pt))
{
// log("rect x:%f,y%f", rect.origin.x, rect.origin.y);
mouse_down = true;
charac->setPosition(pt);
}
};
myMouseListener->onMouseUp = [=](Event* event)
{
mouse_down = false;
};
//鼠标移动
myMouseListener->onMouseMove = [=](Event* event)
{
EventMouse* e = (EventMouse*)event;
Point pt = e->getLocation();
pt.y = visibleSize.height - pt.y;
if (mouse_down )
{
charac->setPosition( pt);
}
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(myMouseListener, this);
setUserObject
在使用setUserObject传值时,对应的参数要new,否则会出现莫名其妙的bug。
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment
GitalkValine