创建3D模型/添加屏幕监听,点击屏幕时,精灵带有旋转的移动



3D模型创建 //3D models
   
     auto model = Sprite3D::create("Sprite3DTest/boss1.obj");
   
     model->setScale(4);
   
     model->setTexture("Sprite3DTest/boss.png");
   
     model->setPosition3D(Vec3(s.width/2, s.height/2, 0));
   
     addChild(model);

EventListenerTouchOneByOne 监听
 EventListenerTouchOneByOne* _lis = EventListenerTouchOneByOne::create();
 _lis->onTouchBegan = [this](Touch* t,Event* e){
  CCLOG("onTouchBegan");
  return true; 
 };
 _lis->onTouchMoved = [this](Touch* t,Event* e){
  CCLOG("onTouchMoved");
  float dx = t->getDelta().x;
  Vec3 rot = _camControlMode->getRotation3D();
  rot.y += dx;
  _camControlMode->setRotation3D(rot);
  Vec3 woldPos;
  _camNode ->getNodeToWorldTransform().getTranslation(&woldPos);
  Camera::getDefaultCamera()->setPosition3D(woldPos);
  Camera::getDefaultCamera()->lookAt(_camControlMode->getPosition3D());
 };
 Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_lis,this);

添加屏幕监听,点击屏幕时,精灵带有旋转的移动 
HelloWorld::HelloWorld()
{
 auto listener = EventListenerTouchOneByOne::create();
 listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
 listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);

 auto sprite = Sprite::create("Images/grossini.png");
 
 auto layer = LayerColor::create(Color4B(155,55,0,25));
 addChild(layer,-1);

 addChild(sprite,0,kTagSprite);
 sprite->setPosition(20,150);

 sprite->runAction(JumpTo::create(4,Vec2(300,48),100,4));
 layer->runAction(RepeatForever::create(Sequence::create(FadeIn::create(1),FadeOut::create(1),nullptr)));

}

bool HelloWorld::onTouchBegan(Touch* touch,Event* event)
{
 return true;
}

void HelloWorld::onTouchEnded(Touch* touch,Event* event)
{
 auto location = touch->getLocation();
 auto s = getChildByTag(kTagSprite);
 s->stopAllActions();
 s->runAction(MoveTo::create(1,Vec2(location.x,location.y)));
 float o = location.x - s->getPosition().x;
 float a = location.y - s->getPosition().y;

 float at = (float) CC_RADIANS_TO_DEGREES(atanf(o/a));//反atanf

 if (a <0)
 {
  if (o<0)
  {
   at = 180 + fabs(at);
  }else
  {
   at = 180 - fabs(at);
  }
 }

 s ->runAction(RotateTo::create(1,at));
}
 

你可能感兴趣的:(Cocos2d-x,3.4,学习)