iPad:スライドさせてスプライトを移動してスライド速度に従って進み続ける方法
-
14-09-2020 - |
質問
スプライト(ボールの画像)を持っていますスライド期間に応じて、そのスプライトの位置の変化率(x軸、y軸)も同定しました。
今私はその速度と方向に従って行くためにスプライトを続ける必要があります。これが私のコード -
ですTouch Event
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
self.touchStartTime = [event timestamp];
self.touchStartPosition = location;
if (YES == [self isItTouched:self.striker touchedAt:convertedLocation]) {
self.striker.isInTouch = YES;
}
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
self.touchLastMovedTime = [event timestamp];
self.touchMovedPosition = convertedLocation;
if(self.striker.isInTouch == YES){
self.striker.position = self.touchMovedPosition;
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
self.touchEndTime = [event timestamp];
self.touchEndPosition = location;
if( self.striker.isInTouch == YES
&& ( self.touchEndTime - self.touchLastMovedTime ) <= MAX_TOUCH_HOLD_DURATION )
{
float c = sqrt( pow( self.touchStartPosition.x - self.touchEndPosition.x, 2 )
+ pow( self.touchStartPosition.y - self.touchEndPosition.y, 2 ) );
self.striker.speedx = ( c - ( self.touchStartPosition.y - self.touchEndPosition.y ) )
/ ( ( self.touchEndTime - self.touchStartTime ) * 1000 );
self.striker.speedy = ( c - ( self.touchStartPosition.x - self.touchEndPosition.x ) )
/ ( ( self.touchEndTime - self.touchStartTime ) * 1000 );
self.striker.speedx *= 4;
self.striker.speedy *= 4;
self.striker.isInTouch = NO;
[self schedule:@selector( nextFrame ) interval:0.001];
}
}
Scheduled Method to move Sprite
- (void) nextFrame {
[self setPieceNextPosition:self.striker];
[self adjustPieceSpeed:self.striker];
if( abs( self.striker.speedx ) <= 1 && abs( self.striker.speedy ) <= 1 ){
[self unschedule:@selector( nextFrame )];
}
}
SET next Position
- (void) setPieceNextPosition:(Piece *) piece{
CGPoint nextPosition;
float tempMod;
tempMod = ( piece.position.x + piece.speedx ) / SCREEN_WIDTH;
tempMod = (tempMod - (int)tempMod)*SCREEN_WIDTH;
nextPosition.x = tempMod;
tempMod = ( piece.position.y + piece.speedy ) / SCREEN_HEIGHT;
tempMod = (tempMod - (int)tempMod)*SCREEN_HEIGHT;
nextPosition.y = tempMod;
piece.position = nextPosition;
}
Set new Speed
- (void) adjustPieceSpeed:(Piece *) piece{
piece.speedx =(piece.speedx>0)? piece.speedx-0.05:piece.speedx+0.05;
piece.speedy =(piece.speedy>0)? piece.speedy-0.05:piece.speedy+0.05;
}
.
しかし現在は静電和調整手法を使用していますが、初期速度によっては動的にすることを望みます(私はいかいアイデアに感謝します)
解決 2
ありがとう。 私は以下のように私の解決策を持っています -
//in adjustSpeed method
piece.speedx -= piece.speedx * DEACCLERATION_FACTOR;
piece.speedy -= piece.speedy * DEACCLERATION_FACTOR;
//and simply, free the sprite to move from ccTouchMoved method not ccTouchEnd
if(self.striker.isInTouch == YES && distance_passed_from_touch_start>=a_threashold){
//calculate speed and position here as it was in ccTouchEnd method
self.striker.isInTouch = NO;
[self schedule:@selector( nextFrame ) interval:0.001];
}
. 他のヒント
あなたはそれをかなり近いように見えます。タッチエンドでセレクタをスケジュールして、速度を計算し、その速度に従ってスプライトを妨げるまでスプライトを移動させて、すべてのメカニカルピースがあります。
自分が物理学であるのはどのようなものです。たとえば、全く現実的に見えないボールを遅くするスピードでいくつかの奇妙なことをやっています。
指が持ち上げられたときにスプライトの「速度ベクトル」を見つけることです。このために瞬間的な速度をやろうとすることができる間、私はそれが最終的な速度を得るためにいくつかのサイクルを背負って平均するのに適していることを見つけました。
それでは、一度そこに入ったら(あなたが今持っているようにxとyスピードのように見えるでしょう)、あなたはこのようなことをすることによって速度を減衰させたいです(未テストの疑似コード):
float speed = sqrt( speedx * speedx + speedy * speedy );
if (speed == 0) {
// kill motion here, unschedule or do whatever you need to
}
// Dampen the speed.
float newSpeed = speed * .9;
if (newSpeed < SOME_THRESHOLD) newSpeed = 0;
speedx = speedx * newSpeed / speed;
speedy = speedy * newSpeed / speed;
// Move sprite according to the new speed
.
これは、それが解放されたときと同じ方向にボールを維持し、停止するまで徐々に減速します。詳細については、特にバウンスや何かをしたい場合は、Googleがベクトル代数の紹介です。
所属していません StackOverflow