我真的被困在这上面了。我的应用程序是横向视图,在一个屏幕上我希望我的指令图像可滚动。我已将此图像添加为精灵。首先我试图从其他网站获得滚动效果,但很快我看到滚动正在完成整个屏幕,而不是精灵图像。然后我通过仅在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;
}
答案 0 :(得分:0)
您的方法通常是正确的。
代码格式不正确,您不清楚问题的症状是什么......
但似乎ccTouchesMoved中的数学运算是错误的。锚点不是你关心的,因为它只是图像中位置和旋转锚点发生的比率。像你一样,将它设置为构造函数中有意义的东西,但之后你不需要引用它。
尝试将您的动作添加到精灵:
deltaY = convertedPoint.y - oldPoint.y;
现在您知道手指上下移动了多少像素。
下次重置oldPoint数据:
oldPoint.y = convertedPoint.y;
现在将此delta应用于您的精灵:
instrucitonsImage.position = ccp(instructionsImage.position.y,instructionsImage.position.y + delta);
应该这样做。