Cocos2dx继承CCNode类不绕其中心旋转?

时间:2012-02-22 21:41:59

标签: rotation ccsprite cocos2d-x

我在这里有点不知所措,请原谅我,如果已经有人问我 - 我已经搜索了谷歌的高低,但我找不到任何东西?

我正在尝试旋转一组在类中生成的精灵,然后在主菜单上按下菜单项旋转此对象,但旋转不在精灵的中心?它是一个更大的区域,可能是层大小?

我尝试过将anchooint设置为每种可能的组合?

这是iv得到的

这是gamecharacter.h

    #define COMPUTE_X(x) ((abs(x)) * 16) + (16*2) + (16/2)
    #define COMPUTE_Y(y) (386 - (abs(y) * 16)) + (16/2)
    #define COMPUTE_X_Y(x,y) ccp( COMPUTE_X(x), COMPUTE_Y(y))

    // Game character class
    #include "cocos2d.h"
    using namespace cocos2d;

    //a class to encapsulate playable game character by creating a group of sprites etc..

    #ifndef GAMECHARACTER_H
    #define GAMECHARACTER_H

    class GameCharacter : public CCNode {

    private:

    //some private methods etc....

    public:

    void addSprite(const char* filename);
    void setInitialPosition(CCPoint* position);

    //Various other methods.........

    };

    #endif

    void GameCharacter::addSprite(const char* filename)
    {
    //go get the sprite sheet
    CCTexture2D* gameArtTexture = CCTextureCache::sharedTextureCache()->addPVRImage("SpriteSheet.pvr.ccz");
    CCSpriteBatchNode::batchNodeWithTexture(gameArtTexture);
    CCSprite *tempBlock = CCSprite::spriteWithSpriteFrameName(filename);
    this->addChild((CCSprite*)tempBlock,0);
    }

    void GameCharacter::setInitialPosition(CCPoint* position)
    {
    //loop through the positions and set the character up
    CCArray *children = this->getChildren();
    CCSprite *currentBlock;
    for (int i=0;i<7;i++){
    currentBlock = (CCSprite*) children->objectAtIndex(i);
    //compute x y grid positions (1,1) ---> to real (72,394)
    currentBlock->setPosition(COMPUTE_X_Y(position[i].x,position[i].y));
    }
    }

    This is the gamecharacter.cpp

    void GameScene::AddCharacter(CCPoint* position)
    {
    const char* filename;

    GameCharacter* character = new GameCharacter();

    for (int i = 0; i < 7; i++) {
    filename = helperFunctions::Format("character%d.png",i+1); //character1.png -> character7.png
    character->addSprite(filename);
    }

    character->setInitialPosition(position);
    this->addChild((CCSprite*) character,-1,2);
    _sprite = character;
    }

    //here is the menuitem click handler
    void GameScene::menuRotateRightCallback(CCObject* pSender)
    {
    //rotate the character right
    //really slowly so we can see whats happening
    _sprite->runAction((CCRotateBy::actionWithDuration(2.50,90)));

    }

由于

2 个答案:

答案 0 :(得分:1)

使用

更容易
x = center.x + cos(angle) * radius;
y = center.y + sin(angle) * radius;

答案 1 :(得分:1)

我已经明白了,看着CCNode的文档让我思考。

CCNode默认位置为(0,0),因此轮换使用此作为原点。

将位置设置到我希望角色有一些数学位置的中心来计算偏移量对我有用。