2

CCSprite私はcocos2d フレームワークから拡張されたバックグラウンドを持っています。そして、このスプライトをゲームレイヤーに追加しました。このバックグラウンド クラスでは、次のCCSpritesように他の名前付きスターを追加しようとしています。

//create the stars
stars = [[CCArray alloc] init];
for (int i = 0; i < 10; i++) 
{
    Star* star = [[Star alloc ] initWithFile:@"star-hd.png"];
    CGSize screensize = [[CCDirector sharedDirector] winSize];
    //CCLOG(@"screensize: %f, %f", screensize.width, screensize.height);
    CGPoint newstarlocation;
    newstarlocation.x = CCRANDOM_0_1() * screensize.width;
    newstarlocation.y = CCRANDOM_0_1() * screensize.height;
    star.position = newstarlocation;
    [self addChild:star z:i];
    [stars addObject:star];
}

しかし、星は見えません。私はいくつかのことを試しましたが、背景の代わりにゲームレイヤーに星を追加したときだけうまくいくようです。しかし、それは私が望むものではありません。

cocos2d でスプライトをネストすることは許可されていませんか? 許可されている場合、スプライトをネストするにはどうすればよいですか?

4

3 に答える 3

1

Steffenのポイントをさらに明確にするために(ちなみに、そこに最高のtut本の1つを持っている人)。星はおそらく「star-hd.png」と呼ばれることはありません。むしろ、「star.png」を参照する必要があります。Cocos2dは、画像のサフィックス「-hd」と「-ipad」を自動的に検索して、どの画像をどのデバイスに関連付けるかを確認します。このコードが見つかるAppDelegate.mファイルを確認することで、プログラムが検索するサフィックスを変更できます。

suffixes are going to be used
[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"];      // Default on iPhone RetinaDisplay is "-hd"
[sharedFileUtils setiPadSuffix:@"-hd"];                 // Default on iPad is "ipad"
[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"];    // Default on iPad RetinaDisplay is "-ipadhd"
于 2012-11-09T22:26:13.230 に答える
1

スプライトをネストすることはできませんが、レイヤーをネストすることはできます (すべきです)。

代わりに背景レイヤーを作成し、それに背景スプライトとスター スプライトを追加し、背景レイヤーをゲーム レイヤーの前にシーンに追加します。そうすれば、背景をゲームレイヤーから離し、他のすべての背後に置き、必要な数のスプライトを使用できます。

怠惰な例 (C++):

CCLayer *backgroundLayer = CCLayer::create();
CCSprite *skySprite = CCSprite::createWithSpriteFrameName("sky_sprite.png"); 
CCSprite *starsSprite = CCSprite::createWithSpriteFrameName("stars_sprite.png");

backgroundLayer->addChild(skySprite);
backgroundLayer->addChild(starsSprite);

CCLayer *gameLayer = CCLayer::create();

scene->addChild(backgroundLayer);
scene->addChild(gameLayer);    
于 2012-10-12T14:50:19.220 に答える
-1

I don't see why you would want to nest sprites, and not only that, would it be very efficient. Write a Star class that contains the sprite, and the child sprites.

It is allowing you to do it because cocos2d loves CCNode, almost everything derives from it. That doesn't mean that CCSprite handles drawing their children. Both the CCLayer and CCSprite can have CCNodes added. It's just their handlers are different.

You would also be a bit more efficient in that, because then you could sprite batch, which is a lot more efficient than drawing sprites directly to the game layer.

于 2012-02-13T17:47:27.163 に答える