5

我开始学习游戏开发。作为初学者,我创建了一个演示游戏,其中一门大炮将子弹击中敌人(从不同方向射向大炮)。现在我坚持在用户触摸屏幕或敌人的任何地方进行大炮精灵图像旋转。我是怎么做到的,我的初始代码如下,

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

4

3 回答 3

5

完美答案如下,

float HelloWorld::changingAngleAccordingToCoordinateSystem(CCPoint imageCenter, CCPoint touchLocation, float calculatedAngle){

//Consideration :- all Angles is in Degree 
if((calculatedAngle >= 0 && calculatedAngle <= 90) || (calculatedAngle >= 90 && calculatedAngle <= 180)){
    //Ist quardant
    calculatedAngle = calculatedAngle;
}
else if(calculatedAngle < 0 && calculatedAngle >= -90){
    //IInd quardant
    calculatedAngle = 270 + (90 + calculatedAngle);
} 
else if(calculatedAngle < -90 && calculatedAngle >= -180){
    calculatedAngle = 180 + (180 + calculatedAngle);
}

return calculatedAngle;
}


//Rotate cannon direction toward touch point
float diff_X = touchLocation.x - myImage->getPosition().x;
float diff_Y = touchLocation.y - myImage->getPosition().y;
CCPoint diffPoint = CCPoint(diff_X, diff_Y);
float angleRadians = atan2f(diffPoint.y,diffPoint.x);
angleRadians = CC_RADIANS_TO_DEGREES(angleRadians);
angleRadians = HelloWorld::changingAngleAccordingToCoordinateSystem(myImage->getPosition(), touchLocation, angleRadians);
myImage->setRotation(-angleRadians);
于 2012-07-17T10:52:18.210 回答
3

我用这段代码来旋转我的精灵。我正在根据我的加速度计读数移动精灵。

float angleRadians =-accelX;
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
objGlider->sprite_Glider.rotation = cocosAngle;

核实。慢的原因可能是你可能在代码中使用了 CClog 或 NSLog。

这是完整的代码。

if(!boolPlayerDied)
{
    static float prevX=0, prevY=0;
    #define kFilterFactor 1.0f// don't use filter. the code is here just as an example
    float accelX = (float) acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
    float accelY = (float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
    prevX = accelX;
    prevY = accelY;
    accelX = accelX-0.5;// Angle check fot tgfor the player to play
    float angleRadians =-accelX;
    float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
    if(accelX>0)
    {
        cocosAngle = 1.1 * angleDegrees;
    }
    else
    {
        if(accelX<-0.5)
            accelX=-0.5;
        cocosAngle = 1.1  * angleDegrees;
    }
    objGlider->sprite_Glider.rotation = cocosAngle;
}

objGlider 是创建滑翔机精灵的类的对象,而 sprite_Glider 是滑翔机类中使用的精灵。

你可以使用rotation属性来旋转你的精灵。在 cocos2Dx 中可能是setRotation.

于 2012-07-17T04:45:48.593 回答
1

首先,更换

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;
}

经过

float angleRadians = atan2f(diffPoint.y, diffPoint.x);

那么最好立即设置旋转(无需操作)以处理多次频繁触摸。

_cannonImage->setRotation(angleRadians);

也许您需要像setRotation(-angleRadians)或一样调整旋转setRotation(angleRadians+90)- 这取决于您的坐标系。

于 2012-07-17T06:13:09.997 回答