如何不断获取用户触摸的位置?我正在开发一款游戏的 Controller ,就目前情况而言,我必须抬起手指并单独将手指放在新 Controller 上才能使其工作。似乎 touchLocation 变量仅在每次点击时更新,我希望它不断更新,这样即使我在周围滑动手指,它也会知道位置并相应地更新它。这是相关代码。谢谢你!
-(void)update:(CCTime)delta{
if([playerDirection isEqual:@"left"]){
_player.position = ccp(_player.position.x - 2, _player.position.y);
}
if([playerDirection isEqual:@"right"]){
_player.position = ccp(_player.position.x + 2, _player.position.y);
}
if([direction isEqual:@"up"]){
_enemy.position = ccp(_enemy.position.x, _enemy.position.y + 2);
}
if([direction isEqual:@"down"]){
_enemy.position = ccp(_enemy.position.x, _enemy.position.y - 2);
}
if(_enemy.position.y >= self.contentSize.height){
direction = @"down";
}
if(_enemy.position.y <= 2){
direction = @"up";
}
if(isTouching){
_player.position = ccp(_player.position.x + 2, _player.position.y);
}
}
// -----------------------------------------------------------------------
#pragma mark - Touch Handler
// -----------------------------------------------------------------------
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
if(touchLocation.x > 400){
playerDirection = @"right";
}
if(touchLocation.x < 200){
playerDirection = @"left";
}
CGPoint touchLoc = [touch locationInNode:self];
//isTouching = true;
// Log touch location
CCLOG(@"Move sprite to @ %@",NSStringFromCGPoint(touchLoc));
// Move our sprite to touch location
//CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc];
//[_sprite runAction:actionMove];
}
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
playerDirection = @"none";
}
最佳答案
我看到您有一个 touchesBegan 和 touchesEnded...但是您缺少 touchesMoved。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for(UITouch *touch in touches) {
//do whatever you want
}
}
希望对您有所帮助,祝您好运!
关于ios - Cocos2d (IOS) - 如何不断获取用户触摸的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24372545/