2010-07-13 3 views
0

스프라이트 (볼의 이미지)가 있습니다. 터치하여 움직여서 이동할 수 있습니다. 나는 또한 슬라이딩 기간에 따라 그 스프라이트의 위치 (x 축, y 축)의 변화율을 확인했다.ipad : 슬라이딩하여 스프라이트를 이동하는 방법 및 슬라이딩 속도에 따라 계속 이동하는 방법

이제 속도와 방향에 따라 그 스프라이트를 계속 사용해야합니다. 여기에 현재 내가 기술을 조정 정적 속도를 사용하고, 내 코드 - 비록

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

,하지만 난 (내가 어떤 생각을 주셔서 감사합니다) 당신이 그것을 가지고있는 것처럼

+0

below-으로 내 솔루션을 가지고, 내가 곧 그것을 시도하고 알려줍니다 나를 – Sadat

답변

0

@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]; 

    } 
0

이 보이는 초기 속도에 따라이 동적으로 만들 수 있도록 노력하겠습니다 꽤 가까워. 선택기를 터치 엔드에서 스케쥴링하고, 속도를 계산하고, 스프레이가 축축해질 때까지 그 속도에 따라 이동 시키므로 모든 기계적 조각이 거기에 있습니다.

물리가 보이는지. 예를 들어, 볼을 느리게하는 속도로 어떤 이상한 일을하고있는 것이 현실감이없는 것입니다.

손가락을 들어 올렸을 때 스프라이트에 대한 "속도 벡터"를 찾고 싶습니다. 이것을 위해 순간 속도를 시도 할 수는 있지만 최종 속도를 얻으려면 몇 사이클 뒤로 샘플링하고 평균을 계산하는 것이 더 효과적이라는 것을 발견했습니다.

: 당신이 벡터있어 일단

그런 다음,이 같은 (앞서 안된 의사 코드)를 수행하여 속도를 저해 할 (당신이 지금 가지고있는 것처럼 그것은,는 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 

이것은 공을 놓을 때와 같은 방향으로 계속 움직이며 멈출 때까지 서서히 속도를 줄입니다. 더 많은 정보를 원한다면, 특히 당신이 어떤 바운싱이나 무엇을하고 싶다면, 구글은 벡터 대수학을 소개합니다.

+0

감사 @cc을 알려 주시기 바랍니다. 내가 잘못 아니에요 경우 – Sadat

+0

@cc,이 유사하다 " speedx = speedx * 0.9; 빠른 = * 빠른 0.9 " 은 왜 내가 newSpeed ​​또는 그 계산 사용해야합니까? 다른 설명이 있으면 알려주십시오. – Sadat

+0

속도를 명시 적으로 제로로 줄이기 위해 현재 속도를 알고 자하는 부분을 제외하면 차이가 없습니다. 일반적으로 "0이 아닌 속도"에서 "0 속도"로 전환 할 때 특별한 것을하고 싶습니다. 그래서 항상 사용하는 프레임 워크입니다. 위의 예제에서는 속도를 0으로 설정하고 있지만 선택기의 스케줄을 취소하거나 다른 "at rest"플래그를 설정하려고합니다. 물론, "if speedx