cocos2d v2扩展我的背景

时间:2012-07-15 15:57:34

标签: iphone ios cocos2d-iphone

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

#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

然后在app委托中我改变了这个:

[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方法中添加图层...

1 个答案:

答案 0 :(得分: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]];