我開始學習遊戲開發。作爲初學者,我創建了一個演示遊戲,其中一個大炮向敵人發射子彈(從不同方向朝向大炮)。 現在,我堅持在任何用戶在屏幕或敵人觸摸的加農炮精靈圖像旋轉。我如何做到這一點,我的初始代碼如下,根據用戶觸摸位置旋轉精靈圖像?
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