2

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

我正在尝试旋转在一个类中生成的一组精灵,然后在主游戏场景中单击菜单项时旋转该对象,但旋转不在精灵的中心?它是一个更大的区域,可能是层大小?

我尝试将锚点设置为每个可能的组合?

这是iv得到的

这是游戏角色.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)));

    }

谢谢

4

2 回答 2

1

我想通了,查看 CCNode 的文档让我想到了。

CCNode 默认位置为 (0,0),因此旋转以此为原点。

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

于 2012-02-23T22:48:36.143 回答
1

容易做到,使用

x = center.x + cos(angle) * radius;
y = center.y + sin(angle) * radius;
于 2012-02-23T16:55:00.060 回答