如何使用sprite套件xcode 5的touchAndHold事件

如何使用sprite套件xcode 5的touchAndHold事件,xcode,sprite-kit,Xcode,Sprite Kit,我正在尝试用精灵套件做一个游戏,我需要角色移动。我希望用户按下按钮,使角色在x轴上直线移动。现在你必须多次点击它才能让他移动。我建议在TouchStart事件中设置x方向速度,在touchUp事件中设置速度为0 在节点上触地(左侧或右侧) 检查是否触摸了方向节点 根据触摸的节点设置速度(+值向右移动,-值向左移动) 释放触摸时,将字符速度重置为0 以下是我为您从游戏中删除的一些代码: - (void) touchesBegan:(NSSet *)touches withEvent:(UIEven

我正在尝试用精灵套件做一个游戏,我需要角色移动。我希望用户按下按钮,使角色在x轴上直线移动。现在你必须多次点击它才能让他移动。

我建议在TouchStart事件中设置x方向速度,在touchUp事件中设置速度为0

  • 在节点上触地(左侧或右侧)
  • 检查是否触摸了方向节点
  • 根据触摸的节点设置速度(+值向右移动,-值向左移动)
  • 释放触摸时,将字符速度重置为0
  • 以下是我为您从游戏中删除的一些代码:

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    UITouch * touch = [touches anyObject];
    
    CGPoint location = [touch locationInNode:self];
    
    
    
    if([self.moveRightNode containsPoint: location]) {
         NSInteger xSpeed = yourSpeed;
         character.physicsBody.velocity = CGVectorMake(yourspeed, 0);
    }
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch * touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    character.physicsBody.velocity = CGVectorMake(0, 0);
    
    }


    设置physicsworld时,将gravity设为0或将character.physicsworld.dynamic=NO.

    这只是一个skspritenode,我假设您将其用作按钮(例如moveLeftSpriteNode)。spritekit中没有真正的按钮,因此创建SKSpriteNodes来复制这些按钮效果很好。您所要做的就是检查触摸是否触摸了SKSpriteNode,如果触摸了,则执行一些代码。所有这些都在上面,除了SKSpriteNodes(用于运动)的创建和定位。我还只包括了move right,因为move left除了将xspeed设为负值之外是相同的。我猜您是这么写的,但它给了我一个错误,即“在类型为'MyScene*'的对象上找不到属性'lArrow';您的意思是访问实例变量'lArrow'?”代码:'if([self.lArrow containsPoint:location1]){NSInteger xSpeed=5;charPos=charPos+2;character.position=CGPointMake(charPos,charPos1);character.physicsBody.velocity=CGVectorMake(xSpeed,15);}}'@meisenmanI去掉了注释。但现在我发现它必须是一个特定的点,你触摸的位置。大多数情况下,它不会执行代码。我需要它,这样当你按下按钮时,那家伙就会一直左右移动。当你释放他时,他会停止移动。我猜你给我的密码不起作用。也许我只是做错了什么。我将在下一条评论中发布代码。-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{UITouch*touch=[touch anyObject];location=[touch locationInNode:self];trans.position=location;if([lArrow containsPoint:location1]){NSInteger xSpeed=5;charPos=charPos+2;character.position=CGPointMake(charPos,charPos1);character.physicsBody.velocity=CGVectorMake(xSpeed,15);}