我开始学习游戏开发。作为初学者,我创建了一个演示游戏,其中一门大炮将子弹击中敌人(从不同方向射向大炮)。现在我坚持在用户触摸屏幕或敌人的任何地方进行大炮精灵图像旋转。我是怎么做到的,我的初始代码如下,
void HelloWorld:: ccTouchesBegan(CCSet *touches, CCEvent * event)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCTouch* touch = (CCTouch*)( touches->anyObject() );
CCPoint location = touch->locationInView(touch->view());
location = CCDirector::sharedDirector()->convertToGL(location);
//Rotate cannon direction toward touch point
CCPoint diffPoint = ccpSub(_cannonImage->getPosition(), location);
float angleRadians = atanf((float)diffPoint.y/(float)diffPoint.x);
float angleOffset = CC_DEGREES_TO_RADIANS(180);
if(diffPoint.x < 0){
angleRadians += angleOffset;
}else{
angleRadians -= angleOffset;
}
CCLog("angle to be rotate = %f", angleRadians);
_cannonImage->runAction(CCRotateBy::actionWithDuration(0.1, angleRadians));
}
代码是用 cocos2d-x 编写的。我也接受用普通 cocos2d 编写的人的回答。
谢谢 iHungry