根据onMouseMove cocos2d-x旋转精灵

问题描述 投票:0回答:1

img

auto spr= Sprite::create("spr.png");
spr->setPosition(Vec2(500, 500);
spr->setScale(0.2);
layer->addChild(gun, 1);

我需要做什么,以便我的精灵可以根据鼠标位置旋转我的头部

void HelloWorld::onMouseMove(Event *event)
{   
   EventMouse* e = (EventMouse*)event;
   (................)
}

1

cocos2d-x
1个回答
0
投票

我想这就是你要做的事情:

const float PI = 3.1415;

void HelloWorld::onMouseMove(Event *event)
{    
    float dx = evnt->getCursorX() - spr->getPosition().x;
    float dy = evnt->getCursorY() - spr->getPosition().y;

    float rotation = (atan2(dx, dy)) * 180 / PI;

    spr->setRotation(rotation);
}

我不明白你的意思是“所以我的精灵可以旋转我的脑袋”,但这是旋转精灵的方法,所以将它应用于你需要的任何精灵。

希望能帮助到你! :d

© www.soinside.com 2019 - 2024. All rights reserved.