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

发布于 2024-08-11 16:35:47 字数 1510 浏览 5 评论 0原文

我真的很困惑。我的应用程序处于横向视图中,并且在一个屏幕上我希望我的说明图像可以滚动。我已将此图像添加为精灵。首先,我尝试从其他网站获取滚动效果,但很快我发现滚动是针对整个屏幕而不是精灵图像进行的。然后我通过仅在 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;
}

I am really stuck on this. My application is in landscape view and on one screen i wanted my instructions image to be scrollable. I have added this image as a sprite. First i tried to get scroll effect available from other sites, but soon i saw that scrolling was being done for the complete screen and not the sprite image. Then i resorted to implement the scrolling effect by dragging the sprite only in y axis (up and down). Unfortunately i am messing things somewhere, due to which only a portion of the sprite (shown on the screen only with the height 320pixels) is being dragged and the rest of the sprite is not being shown. The code is as follows

in the init layer function i have

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-08-18 16:35:47

您的方法总体上是正确的。

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

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

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

deltaY = conversionPoint.y - oldPoint.y;

现在您知道手指上下移动了多少像素。

重置您的 oldPoint 数据以供下次使用:

oldPoint.y = conversionPoint.y;

现在将此增量应用到您的精灵:

instructionsImage.position = ccp(instructionsImage.position.y,instructionsImage.position.y + delta);

应该做。

Your approach is generally correct.

The code did not format properly, and you were not clear on exactly what the symptom is of the problem...

But it appears as if your math in the ccTouchesMoved is wrong. The anchor point is not what you care about there, as that's just a ratio within the image where the position and rotation anchor occurs. Set that, like you do, to whatever makes sense in the constructor but after that you don't need to reference it.

Try just adding your movement to the sprite:

deltaY = convertedPoint.y - oldPoint.y;

Now you know how many pixels your finger has moved up and down.

Reset your oldPoint data for next time:

oldPoint.y = convertedPoint.y;

Now apply this delta to your sprite:

instrucitonsImage.position = ccp(instructionsImage.position.y,instructionsImage.position.y + delta);

Should do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文