gpt4 book ai didi

iphone - cocos2d v2 缩放我的背景

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:12 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com