Я новичок в cocos2d и читаю много уроков. Я хотел бы коснуться экрана, чтобы спрайт следовал за мной.При отпускании касания спрайт останавливается. При касании вдали от спрайта спрайт должен начать двигаться, даже если я непрерывно двигаю пальцем.Как я могу это сделать?
Я пытаюсь по-другому:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
isMoving = YES;
[self schedule:@selector(moveSprite:)];
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
isMoving = NO;
[self unschedule:@selector(moveSprite:)];
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
if (isMoving)
{
touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
}
}
-(void)moveSprite:(ccTime) dt {
CGPoint moveVector = ccpSub(touchLocation, mySprite.position);
float distanceToMove = ccpLength(moveVector);
float velo = 480.0/3.0;
float moveDuration = distanceToMove / velo;
self.moveAction = [CCSequence actions:
[CCMoveTo actionWithDuration:moveDuration position:touchLocation],
nil
];
[mySprite runAction:_moveAction];
}
или еще раз:
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
moveAction = [CCSequence actions:
[CCMoveTo actionWithDuration:2.0f position:touchLocation],
nil
];
[mySprite runAction:moveAction];
}
или просто
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
[_mySprite stopAction:_moveAction];
_moveAction = [CCMoveTo actionWithDuration:1 position:touchLocation];
[_mySprite runAction:_moveAction];
}
Может кто-нибудь помочь?Большое спасибо!