如何不断获取用户触摸的位置?我正在为游戏制作控制器,而且我必须抬起并单独将手指放在新控件上才能使其正常工作。似乎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";
}
答案 0 :(得分:1)
我看到你有一个触摸开始和触摸过去...但是你错过了touchesMoved。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for(UITouch *touch in touches) {
//do whatever you want
}
}
希望有所帮助,祝你好运!
答案 1 :(得分:0)
要获得触摸,您必须在-(id) init()
方法中启用触控功能。
您可以通过
self.IsTouchEnable = YES
之后,您可以了解以下触控处理方法
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
希望这会对你有所帮助