2

我已经安装了 cocos2d v2.0,我正在做一些工作,看看 v1 发生了什么变化,我创建了一个简单的 cocos2d + box2d 项目,我运行它并且一切正常,我已经删除了 IntroLayer 和Helloworld 层,我试图显示一个简单的背景层,所以我这样做了:

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface BackgroundLayer : CCLayer

@end

#import "BackgroundLayer.h"

@implementation BackgroundLayer

-(id)init {

self = [super init];
if (self != nil) {

    CCSprite *backgroundImage = [CCSprite spriteWithFile:@"background.png"];
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
    [self addChild:backgroundImage z:0 tag:0];
}

return self;
}

@end

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "BackgroundLayer.h"

@interface GameScene : CCScene

@end

#import "GameScene.h"

@implementation GameScene

-(id)init {

self = [super init];
if (self != nil) {

    BackgroundLayer *backgroundLayer = [BackgroundLayer node];
    [self addChild:backgroundLayer z:0];
}

return self;
}

@end

然后在应用程序委托中我改变了这个:

[director_ pushScene: [IntroLayer scene]];

在这:

[director_ pushScene: [GameScene node]];

但是似乎按比例缩放了 0.5,我为视网膜而不是视网膜创建了高清和非高清图像,我错了什么?这是结果:

http://img706.imageshack.us/img706/4778/schermata072456124alle1.png

编辑: 如果我以这种方式更改 BackgroundLayer.m,一切正常:

@implementation BackgroundLayer

/*-(id)init {

self = [super init];
if (self != nil) {

    CCSprite *backgroundImage = [CCSprite spriteWithFile:@"background.png"];
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
    [self addChild:backgroundImage z:0 tag:0];
}

return self;
}*/

-(void) onEnter
{
[super onEnter];
CCSprite *backgroundImage = [CCSprite spriteWithFile:@"background.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
[self addChild:backgroundImage z:0 tag:0];
}

@end

谁能解释我为什么?我记得在旧版本的 cocos2d 中我可以在 init 方法中添加图层...

4

1 回答 1

1

使用这种风格,也在 cocos2d 2.0 中工作过。

@interface GameScene : CCLayer
{

}
+(CCScene *) scene;
@end

#import "GameScene.h"

@implementation GameScene

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    GameScene *gameScenelayer = [GameScene node];

    // add layer as a child to scene
    [scene addChild: gameScenelayer];

    // return the scene 
    return scene;
}


-(id)init {

    self = [super init];
    if (self != nil) {

        CCSprite *backgroundImage = [CCSprite spriteWithFile:@"background.png"];
        CGSize screenSize = [[CCDirector sharedDirector] winSize];
        backgroundImage.position = ccp(screenSize.width/2, screenSize.height/2);
        [self addChild:backgroundImage z:0 tag:0];
    }

    return self;
}

@end

//调用这个

[director_ pushScene: [GameScene scene]];
于 2012-07-15T17:27:47.950 回答