스프라이트 (볼의 이미지)가 있습니다. 터치하여 움직여서 이동할 수 있습니다. 나는 또한 슬라이딩 기간에 따라 그 스프라이트의 위치 (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;
}
,하지만 난 (내가 어떤 생각을 주셔서 감사합니다) 당신이 그것을 가지고있는 것처럼
below-으로 내 솔루션을 가지고, 내가 곧 그것을 시도하고 알려줍니다 나를 – Sadat