2

私は本当にこれにこだわっています。私のアプリケーションは横向きで、1 つの画面で指示画像をスクロール可能にしたいと考えていました。この画像をスプライトとして追加しました。最初に他のサイトからスクロール効果を取得しようとしましたが、すぐにスプライト画像ではなく画面全体に対してスクロールが行われていることがわかりました。次に、スプ​​ライトを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;
}
4

1 に答える 1

0

あなたのアプローチは一般的に正しいです。

コードが適切にフォーマットされておらず、問題の症状が正確に何であるかが明確ではありませんでした...

しかし、ccTouchesMoved の計算が間違っているように見えます。アンカーポイントは、位置と回転のアンカーが発生する画像内の比率にすぎないため、気にする必要はありません。あなたと同じように、コンストラクターで意味のあるものに設定しますが、その後は参照する必要はありません。

スプライトに動きを追加してみてください:

deltaY = convertPoint.y - oldPoint.y;

これで、指が上下に移動したピクセル数がわかります。

次回のために oldPoint データをリセットします。

oldPoint.y = convertPoint.y;

このデルタをスプライトに適用します。

instructionsImage.position = ccp(instructionsImage.position.y,instructionsImage.position.y + デルタ);

やるべきです。

于 2009-12-08T06:58:16.580 に答える