Ios 触摸在特定移动对象上移动

Ios 触摸在特定移动对象上移动,ios,sprite-kit,touchesmoved,Ios,Sprite Kit,Touchesmoved,我正在使用Spritekit为iOS 7创建游戏。游戏使用此代码在屏幕上移动圆圈 _enemy = [SKSpriteNode spriteNodeWithImageNamed:@"greeny"]; _enemy.position = CGPointMake(CGRectGetMidX(self.frame)+300, CGRectGetMidY(self.frame)); _enemy.size =

我正在使用Spritekit为iOS 7创建游戏。游戏使用此代码在屏幕上移动圆圈

_enemy = [SKSpriteNode spriteNodeWithImageNamed:@"greeny"];
        _enemy.position = CGPointMake(CGRectGetMidX(self.frame)+300,
                                      CGRectGetMidY(self.frame));
_enemy.size = CGSizeMake(70, 70);
_enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:35];
_enemy.physicsBody.mass = 0;

_enemy.physicsBody.categoryBitMask = Collisiongreen;
[_enemies addObject:_enemy];
NSLog(@"Enemies%lu",(unsigned long)_enemies.count);

[self addChild:_enemy];
[_enemy runAction:[SKAction moveByX:-900 y:0 duration:4]];
[self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];
[self runAction:[SKAction sequence:@[
                                  [SKAction waitForDuration:1.4],
                                  [SKAction performSelector:@selector(move)
                                                   onTarget:self],
                                     ]]];
我希望当用户触摸屏幕上移动的某个对象时,他们可以用手指向上或向下移动它(这就是我使用触摸移动的原因) 我现在设置的代码是

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    if(_dead)
        return;
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    if(CGRectContainsPoint(_enemy.frame, location)){

        [_enemy runAction:[SKAction moveTo:[[touches anyObject] locationInNode:self] duration:0.01]];
    }

我的问题是,如果我触摸它,物体会移动几个像素,但会立即停止。我怎样才能让它用我的手指移动,如果我放开它,它会像以前编程的那样继续向左移动?谢谢

这里的诀窍是,在touchesMoved方法中,您希望将敌人的位置设置为您触摸的位置。使用touchesend方法执行一种方法,使敌人继续向你最后一次触摸的方向移动。这是一个粗略的未经测试的示例

您需要存储当前位置与以前位置的差异

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    if(_dead)
        return;
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    if(CGRectContainsPoint(_enemy.frame, location)){

        someBool = False;
        [_enemy setPosition:location];
        changeInX = location.x - lastX;
        changeInY = location.y - lastY;
        lastX = location.x;
        lastY = location.y;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    someBool = True;
}

- (void)update:(NSTimeInterval)currentTime
{
...
    if (someBool) {
        enemy.position = CGPointMake((enemy.position.x + changeInX), (enemy.position.y + changeInY));
    }
...
}

你能解释一下触摸的原因吗。我对这一点还是很陌生这只是暂时的。但在应用changeInX和changeInY时,您需要提醒更新方法。是否需要将changeInX、changeInY、lastX、lastY定义为CGpoints?或者一个整数。。它们是未定义的,它们应该是浮动的。您想将这些变量声明为即时变量。非常感谢。我有一个问题,虽然不是你的代码,而是我的其他代码,我想知道你是否可以看看它--我创建了一个单独的问题