我正在尝试添加10个级别,每页1个,即10页。我该如何使用此代码执行此操作?现在它只有两页。有人可以帮忙吗?
-(id) init
{
if ((self = [super init]))
{
CGSize s = [[CCDirector sharedDirector] winSize];
self.isTouchEnabled = YES;
isDragging = NO;
lastX = 0.0f;
xVel = 0.0f;
contentWidth = s.width * 10.0;
currentPage = 0;
// main scrolling layer - add as child to this page layer.
scrollLayer = [[[LevelScene alloc] init] autorelease];
scrollLayer.anchorPoint = ccp(0, 1);
scrollLayer.position = ccp(0, 0);
[self addChild:scrollLayer];
[self schedule:@selector(moveTick:) interval:0.02f];
}
return self;
}
- (void) moveTick: (ccTime)dt
{
float friction = 0.99f;
CGSize s = [[CCDirector sharedDirector] winSize];
if (!isDragging)
{
// inertia
xVel *= friction;
CGPoint pos = scrollLayer.position;
pos.x += xVel;
// to stop at bounds
pos.x = MAX(-s.width, pos.x);
pos.x = MIN(0, pos.x);
if (pos.x == -s.width)
{
xVel = 0;
currentPage = 1;
}
if (pos.x == 0)
{
xVel = 0;
currentPage = 0;
}
// snap to page by quickly moving to it: e.g.: xVel = 40
if (fabsf(xVel) < 10)
{
if (pos.x < -s.width/2.0)
{
xVel = -40;
}
else {
xVel = 40;
}
}
scrollLayer.position = pos;
}
else {
xVel = (scrollLayer.position.x - lastX)/2.0;
lastX = scrollLayer.position.x;
}
}
- (void) ccTouchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = YES;
}
- (void) ccTouchesMoved: (NSSet *)touches withEvent: (UIEvent *)event
{
CGSize s = [[CCDirector sharedDirector] winSize];
UITouch *touch = [touches anyObject];
// simple position update
CGPoint a = [[CCDirector sharedDirector] convertToGL:[touch previousLocationInView:touch.view]];
CGPoint b = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
CGPoint nowPosition = scrollLayer.position;
nowPosition.x += (b.x - a.x);
nowPosition.x = MAX(-s.width, nowPosition.x);
nowPosition.x = MIN(0, nowPosition.x);
scrollLayer.position = nowPosition;
}
- (void) ccTouchesEnded: (NSSet *)touches withEvent: (UIEvent *)event
{
isDragging = NO;
}
非常感谢任何帮助!谢谢!
答案 0 :(得分:2)
Jon,好像你正在尝试在cocos2d中重新创建一个UIScrollView。如果是这种情况,我可以建议使用一个名为CCScrollLayer(HERE)的现有开源项目开箱即用它应该做你需要做的一切,并且很容易扩展以更好地满足你的需求。