我开始学习游戏开发。作为初学者,我创建了一个演示游戏,其中一个大炮向敌人发射子弹(从不同方向朝向大炮)。 现在,我坚持在任何用户在屏幕或敌人触摸的加农炮精灵图像旋转。我如何做到这一点,我的初始代码如下,根据用户触摸位置旋转精灵图像?
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
将是有益的?顺便说一句,检查ccpAngle和ccpAngleSigned,如果它们在cocos2d-x中可用。 – LearnCocos2D 2012-07-16 11:36:30
@ LearnCocos2D,是的老板......它工作,但它不旋转,我想... – Tirth 2012-07-16 11:47:43
即时通讯也检查改变角度....但它不工作....实际问题是,...图像旋转懒惰和小角度....我想设置加农炮面对用户触摸位置。 – Tirth 2012-07-16 11:49:32