iphone - cocos2d v2 缩放我的背景

标签 iphone ios cocos2d-iphone

我已经安装了 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

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

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

最佳答案

使用这种风格,也适用于 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]];

关于iphone - cocos2d v2 缩放我的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11493365/

相关文章:

ios - 一个低框架的cocos2d程序

ios - CCJumpTo 和 CCJumpBy 高度参数?

ios - 是否可以在同一个 iOS 应用程序中同时支持 Facebook 登录和 Google Plus 登录?

iphone - iOS 6 - Facebook 'Like' 按钮

iphone - 将图像设置为按钮

ios - 如何在 objective-c 中访问子类中父类 Collection View 的不可见单元格

ios - 在 iOS 上绘制扭曲的文本

ios - 空 UIStackView 导致高度/位置不明确

objective-c - 没有信息的崩溃

ios - 无法使用 appium 1.6.0 在 IOS 10 真实设备中启动 ipa 文件