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
감사 @all. 나는 아래와 같이 솔루션을 가지고있다 -
//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];
}
. 다른 팁
당신이 꽤 가까이있는 것처럼 보입니다. 터치 엔드에서 선택기를 스케줄링하고 속도를 계산하고 흡동 될 때까지 그 속도에 따라 스프라이트를 움직이고 있으므로 모든 기계 조각이 있습니다.
Iffy가 물리학 인 것처럼 보입니다. 예를 들어, 당신은 전혀 현실적인 것처럼 보이지 않을 공을 느리게하는 속도로 어떤 이상한 것을하고 있습니다.
손가락이 들어 올릴 때 스프라이트의 "Velocity Vector"를 찾으십시오. 이를 위해 순간 속도를 시도 할 수는 있지만 몇 가지 사이클을 샘플링하고 최종 속도를 얻으려면 평균적으로 작동하는 것이 좋습니다.
그런 다음이 벡터를 얻으면 이제와 같이 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은 벡터 대수학을 소개합니다.