Cocos2d iphone 在COCOS2D中上下拖动一个特别大的精灵,就像滚动效果一样

Cocos2d iphone 在COCOS2D中上下拖动一个特别大的精灵,就像滚动效果一样,cocos2d-iphone,scroll,touch,sprite,drag,Cocos2d Iphone,Scroll,Touch,Sprite,Drag,我真的被这件事缠住了。我的应用程序在横向视图中,在一个屏幕上,我希望我的指令图像可以滚动。我已将此图像添加为精灵。首先,我试图从其他网站获得滚动效果,但很快我发现滚动是针对整个屏幕而不是精灵图像进行的。然后我通过只在y轴上下拖动精灵来实现滚动效果。不幸的是,我在某个地方搞砸了,因为只有一部分屏幕上显示的高度只有320像素的精灵被拖动,其余的精灵没有显示。代码如下 在init层函数中,我有 //Add the instructions image instructionsImage = [Sp

我真的被这件事缠住了。我的应用程序在横向视图中,在一个屏幕上,我希望我的指令图像可以滚动。我已将此图像添加为精灵。首先,我试图从其他网站获得滚动效果,但很快我发现滚动是针对整个屏幕而不是精灵图像进行的。然后我通过只在y轴上下拖动精灵来实现滚动效果。不幸的是,我在某个地方搞砸了,因为只有一部分屏幕上显示的高度只有320像素的精灵被拖动,其余的精灵没有显示。代码如下

在init层函数中,我有

//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;
}

你的方法大体上是正确的

代码格式不正确,您不清楚问题的具体症状是什么

但是你在CCTouchsMoved中的数学似乎是错误的。锚定点不是您所关心的,因为这只是图像中位置和旋转锚发生的比率。像您所做的那样,将其设置为构造函数中有意义的任何值,但之后您不需要引用它

尝试将您的移动添加到精灵:

deltaY=convertedPoint.y-oldPoint.y

现在你知道你的手指上下移动了多少像素了

为下次重置旧点数据:

oldPoint.y=转换点.y

现在将此增量应用于精灵:


instrucitonsImage.position=ccpinstructionsImage.position.y,instructionsImage.position.y+delta


应该这样做。

你的方法通常是正确的

代码格式不正确,您不清楚问题的具体症状是什么

但是你在CCTouchsMoved中的数学似乎是错误的。锚定点不是您所关心的,因为这只是图像中位置和旋转锚发生的比率。像您所做的那样,将其设置为构造函数中有意义的任何值,但之后您不需要引用它

尝试将您的移动添加到精灵:

deltaY=convertedPoint.y-oldPoint.y

现在你知道你的手指上下移动了多少像素了

为下次重置旧点数据:

oldPoint.y=转换点.y

现在将此增量应用于精灵:


instrucitonsImage.position=ccpinstructionsImage.position.y,instructionsImage.position.y+delta


应该这样做。

CCP说明image.position.y。。。应该是ccpinstructionsImage.position.x,…,但是是的,这种方法是有效的。。。应该是ccpinstructionsImage.position.x,…,但是是的,这种方法是有效的。