我试图让 CCScrollLayer 在我的 cocos2d 应用程序中工作,但它不会滚动。
这是我所做的:
使用 cocos2d 模板,创建了一个 iOS cocos2d 项目。添加了 CCScrollLayer 文件,并导入到我的 HelloWorldLayer 类中。添加了一个方法“layerWithLevelName”来创建图层。
在 init 中,我创建了几个层用于测试。下面是我的 HelloWorldLayer 实现的代码。这是非常基本的,因为我只是想让一些图层滚动。
#
// HelloWorldLayer implementation
#import "HelloWorldLayer.h"
#import "CCScrollLayer.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
// return the scene
return scene;
}
-(CCLayer*) layerWithLevelName:(NSString*)name number:(int)number screenSize:(CGSize)screenSize
{
CCLayer *layer = [[[CCLayer alloc] init]autorelease];
int largeFont = [CCDirector sharedDirector].winSize.height / 9;
CCLabelTTF *layerLabel = [CCLabelTTF labelWithString:name fontName:@"Marker Felt" fontSize:largeFont];
layerLabel.position = ccp( screenSize.width / 2 , screenSize.height / 2 + 10 );
layerLabel.rotation = -6.0f;
layerLabel.color = ccc3(95,58,0);
[layer addChild:layerLabel];
return layer;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init]))
{
// ask director the the window size
NSMutableArray* _layers = [[NSMutableArray alloc]init];
CGSize screenSize = [CCDirector sharedDirector].winSize;
for (int i=0; i<3; i++)
{
int number = i;
NSString* name = [[NSString alloc]initWithFormat:@"level%d",i];
CCLayer* layer = [self layerWithLevelName:name number:number screenSize:screenSize];
[_layers addObject:layer];
[name release];
}
// Set up the swipe-able layers
CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:_layers
widthOffset:230];
[self addChild:scroller];
// [scroller selectPage:0];
[scroller release];
[_layers release];
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
@end
如果你运行代码,你会发现你可以看到levels/scroll layer --> 似乎已经正确加载了。但是你不能滚动。我忘了做什么?我究竟做错了什么?
编辑:我正在使用 cocos2d-iphone 1.1beta2