建立3D模型/添加螢幕監聽,點擊螢幕時,精靈帶有旋轉的移動

來源:互聯網
上載者:User

標籤:



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));
}
 

建立3D模型/添加螢幕監聽,點擊螢幕時,精靈帶有旋轉的移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.