我真的坚持这一点。我的应用程序处于横向视图中,并且在一个屏幕上我希望我的说明图像可以滚动。我已将此图像添加为精灵。首先,我尝试从其他站点获得滚动效果,但很快我发现滚动是针对整个屏幕而不是精灵图像进行的。然后我通过仅在 y 轴(上下)拖动精灵来实现滚动效果。不幸的是,我在某处弄乱了东西,因此只有一部分精灵(仅在屏幕上显示,高度为 320 像素)被拖动,而精灵的其余部分没有显示。代码如下
在我有的初始化层函数中
//Add the instructions image
instructionsImage = [Sprite spriteWithFile:@"bkg_InstructionScroll.png"];
instructionsImage.anchorPoint = CGPointZero;
[self addChild:instructionsImage z:5];
instructionsImage.position = ccp(40,-580);
oldPoint = CGPointMake(0,0);
self.isTouchEnabled = YES;
//The touch functions are as follows
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// Move me!
if(touch && instructionsImage != nil) {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location];
CGPoint newPoint = CGPointMake(40, (instructionsImage.anchorPoint.y+ (convertedPoint.y - oldPoint.y)));
instructionsImage.position = newPoint;
return kEventHandled;
}
return kEventIgnored;
}
//The other function
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// Move me!
if(touch && instructionsImage != nil) {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location];
oldPoint = convertedPoint;
return kEventHandled;
}
return kEventIgnored;
}