ios - Cocos2d (IOS) - 如何不断获取用户触摸的位置?

标签 ios cocos2d-iphone location touch

如何不断获取用户触摸的位置?我正在开发一款游戏的 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/

相关文章:

android - 收到接近警报 Intent 时如何接收位置信息?

ios - 如何使用其他应用程序从应用程序共享链接

iphone - 我想用另一种颜色交换图像中的特定颜色。有什么可行的办法吗?

iphone - 应该设置球的哪些物理属性?

Javascript:使用 {x} api 获取当前 GPS 位置?

c# - C#中的简单移动

ios - 我怎样才能改变颜色/把东西放在我的 View Controller 的 View 后面(快速)?

ios - 在显示的自定义 View 中调用动画的位置

ios - 使用 URL swift Alamofire 上传视频

objective-c - exportAsynchronouslyWithCompletionHandler : is not working