//创建第一个3D对象
Sprite3D * sp = Sprite3D::create("boss.obj","boss.png");
sp->setScale(17.f);
sp->setPosition(visibleSize/2);
sp->runAction(RepeatForever::create(RotateBy::create(0.1, 10)));
addChild(sp);
//创建第二个对象
Sprite3D * sp_1 = Sprite3D::create("boss.obj","boss.png");
sp_1->setScale(17.f);
sp_1->setPosition(visibleSize/2);
sp_1->setAnchorPoint(Vec2(0.5,0.5));
sp_1->runAction(RepeatForever::create(RotateBy::create(0.1, 10)));
addChild(sp_1);
//创建触摸监听
auto lis = EventListenerTouchOneByOne::create();
lis->setSwallowTouches(true);
//用lambda表达式对回调函数的实现
lis->onTouchBegan=[](Touch * touch,Event * e){
auto target = (Sprite3D*)(e->getCurrentTarget());
Rect rect = target->getBoundingBox();
if (rect.containsPoint(touch->getLocation())) {
CCLOG("sprite3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
target->setOpacity(100);
return true;
}
return false;
};
lis->onTouchMoved=[](Touch * touch,Event * e){
auto target = (Sprite3D*)(e->getCurrentTarget());
target->setPosition(target->getPosition()+touch->getDelta());
};
lis->onTouchEnded=[](Touch * touch,Event * e){
auto target = (Sprite3D*)(e->getCurrentTarget());
target->setOpacity(255);
};
//事件分发器 分别分发给两个不同的对象
_eventDispatcher->addEventListenerWithSceneGraphPriority(lis, sp);
_eventDispatcher->addEventListenerWithSceneGraphPriority(lis->clone(), sp_1);
Sprite3D * sp = Sprite3D::create("boss.obj","boss.png");
sp->setScale(17.f);
sp->setPosition(visibleSize/2);
sp->runAction(RepeatForever::create(RotateBy::create(0.1, 10)));
addChild(sp);
//创建第二个对象
Sprite3D * sp_1 = Sprite3D::create("boss.obj","boss.png");
sp_1->setScale(17.f);
sp_1->setPosition(visibleSize/2);
sp_1->setAnchorPoint(Vec2(0.5,0.5));
sp_1->runAction(RepeatForever::create(RotateBy::create(0.1, 10)));
addChild(sp_1);
//创建触摸监听
auto lis = EventListenerTouchOneByOne::create();
lis->setSwallowTouches(true);
//用lambda表达式对回调函数的实现
lis->onTouchBegan=[](Touch * touch,Event * e){
auto target = (Sprite3D*)(e->getCurrentTarget());
Rect rect = target->getBoundingBox();
if (rect.containsPoint(touch->getLocation())) {
CCLOG("sprite3d began... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
target->setOpacity(100);
return true;
}
return false;
};
lis->onTouchMoved=[](Touch * touch,Event * e){
auto target = (Sprite3D*)(e->getCurrentTarget());
target->setPosition(target->getPosition()+touch->getDelta());
};
lis->onTouchEnded=[](Touch * touch,Event * e){
auto target = (Sprite3D*)(e->getCurrentTarget());
target->setOpacity(255);
};
//事件分发器 分别分发给两个不同的对象
_eventDispatcher->addEventListenerWithSceneGraphPriority(lis, sp);
_eventDispatcher->addEventListenerWithSceneGraphPriority(lis->clone(), sp_1);