我开始学习游戏开发。作为初学者,我创建了一个演示游戏,在游戏中一个大炮向不同方向的敌人发射子弹。 现在,我遇到了问题:如何使大炮精灵图像根据用户在屏幕上的触摸或敌人的位置旋转。以下是我的初始代码:
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