基于Cocos2d-x3.0集成物理特性的精确点击测试
From: http://www.cocoachina.com/bbs/read.php?tid-209290.html 基于Cocos2d-x3.0集成物理特性的精确点击测试
把我们的精灵添加到层里。
4
5
6
7
8
9
10
11
12
13
14
15
|
// add "2dx.png"
sp_2dx = Sprite::create(
"2dx.png"
);
// position the sprite on the center of the screen
sp_2dx->setPosition(Point(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));
//load
MyBodyParser::getInstance()->parseJsonFile(
"bodies.json"
);
//bind physicsbody to sprite
_body = MyBodyParser::getInstance()->bodyFormJson(sp_2dx,
"2dx"
);
if
(_body !=
nullptr
) {
_body->setDynamic(
false
);
//set it static body.
_body->setCollisionBitmask(0x000000);
//don't collision with anybody.
sp_2dx->setPhysicsBody(_body);
}
// add the sprite as a child to this layer
this
->addChild(sp_2dx,0);
|
MyBodyParser is the helper class to load bodies.json,use rapid json.
MyBodyParser 就是用来读取bodies.json的类,使用快速json。
你能在源码中查询想要的细节,关键函数是MyBodyParser::bodyFormJson。之后添加touchListener,这个过程较为简单并且没有什么值得多费口舌来讲解。
7. 确认是否我们的精灵已经被点击
在函数HelloWorld::nodeUnderTouch中:
;
->convertTouchToNodeSpace(touch);
(
& obj : arr)
;
node;
如果这个函数返回nullptr,这意味着你并没有碰触到精灵物理躯干的内部。
这些就是这篇教程所有的奇妙之处了,确认源代码并阅读更多的信息。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!