1

CCSprites を画面に配置する際の基本的な指針を教えてもらえますか?

例:

CCSize s = CCDirector::sharedDirector()->getWinSize();

s、0 から始まる画面の一番下にスプライトを配置したいとします。草のようなものを考えてください。

1024 x 768 で実行している場合、中央は次のとおりです。

setPosition( ccp(s.width/2, s.height/2) );

したがって、左と中央から開始すると、次のようになります。

setPosition( ccp(0, s.height/2) );

では、どうすればもっと下に行けるのでしょうか?

setPosition( 0, s.height) );

これにより、画面の左上から開始し、画面の上部にとどまることができます。

どんな助けでも大歓迎です。

4

2 に答える 2

4

Position is relative to the sprite's parent, as well as its anchorPoint.

anchorPoint generally ranges from 0 to 1 for each coordinate, with a default of 0.5. I say "generally" because it can really be any value, but ranges outside of 0-1 place you outside of the bounds of the sprite.

For example, an anchorPoint of (0,0) makes positions relative to the bottom left. (1,0) is the bottom right, (0,1) is the top left and (1,1) is the top right. (0.5,0.5) is the very center of the sprite, which is the default.

Basically you just multiple the value by the width to get the relative position.

If you want to place a sprite at the very bottom of the screen (the bottom left corner aligned with the bottom left corner of the screen), you can do it multiple ways, based on the anchorPoint alone.

With the default anchorPoint of (0.5,0.5), the position would be (sprite.contentSize.width/2, sprite.contentSize.height/2).

If you set the anchorPoint to (0,0), the same position is obtained by simply (0,0).

If you wanted to move that sprite to the very center of the screen (the center of the sprite right in the middle), with an anchorpoint of (0.5, 0.5), the position would be (s.width/2, s.height/2).

This is all assuming you are adding a sprite to a parent the size of the screen, which is where the 2nd part of positioning comes in.

Position is also relative to the sprite's parent - which could be any other CCNode (CCLayer, another CCSprite, etc).

The way to think of that is not much different than adding a full screen node - except think in terms of the parent's size and position, not the screen.

于 2013-05-24T02:14:40.323 に答える