Create a 3D model/Add a screen listener, and when you tap the screen, the sprite moves with rotation

Source: Internet
Author: User



3D Model Creation//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 Monitoring
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);

Add screen Listener, when tapping the screen, the sprite with the rotation of the movement
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));//Anti-ATANF

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

S->runaction (Rotateto::create (1,at));
}

Create a 3D model/Add a screen listener, and when you tap the screen, the sprite moves with rotation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.