Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用用户触摸更改SpriteNodes坐标_Ios_Drag And Drop_Draggable_Sprite Kit - Fatal编程技术网

Ios 使用用户触摸更改SpriteNodes坐标

Ios 使用用户触摸更改SpriteNodes坐标,ios,drag-and-drop,draggable,sprite-kit,Ios,Drag And Drop,Draggable,Sprite Kit,我目前正在用Xcode中的SpriteKit为iOS平台编写一个小游戏。我写了一个代码,在两条不同的“线”上添加了一些SpriteNodes,因此一半的节点以y坐标100移动,另一半以y=200移动,它们都从左侧移动到右侧,这是一个无限循环。现在,我希望用户可以触摸一个SpriteNode,然后将手指移动到另一个SpriteNode,但它必须位于另一行,然后移除手指,使用touchBegind的SpriteNode应更改其与TOUCHEND节点的y坐标。我怎样才能做到这一点 -(void)add

我目前正在用
Xcode
中的
SpriteKit
iOS
平台编写一个小游戏。我写了一个代码,在两条不同的“线”上添加了一些
SpriteNodes
,因此一半的节点以y坐标100移动,另一半以y=200移动,它们都从左侧移动到右侧,这是一个无限循环。现在,我希望用户可以触摸一个
SpriteNode
,然后将手指移动到另一个
SpriteNode
,但它必须位于另一行,然后移除手指,使用
touchBegind
SpriteNode
应更改其与
TOUCHEND
节点的y坐标。我怎样才能做到这一点

-(void)add
{
    SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithImageNamed:@"test1.png"];
    sprite2.position = CGPointMake(-40, self.frame.size.height / 2);
    sprite2.size = CGSizeMake(100, 32);

    SKSpriteNode *sprite3 = [SKSpriteNode spriteNodeWithImageNamed:@"test.png"];
    sprite3.position = CGPointMake(-40, (self.frame.size.height / 2) + 90);
    sprite3.size = CGSizeMake(32, 100);

    [self addChild:sprite1];

    [self addChild:sprite2];

     SKAction *actionMove1 = [SKAction moveTo:CGPointMake(400, (self.frame.size.height / 2) - 90) duration:12];
    SKAction *actionMove2 = [SKAction moveTo:CGPointMake(200, (self.frame.size.height / 2)) duration:12];


    SKAction *actionMoveDone = [SKAction removeFromParent];
    [sprite1 runAction:[SKAction sequence:@[actionMove1, actionMoveDone]]];
    [sprite2 runAction:[SKAction sequence:@[actionMove2, actionMoveDone]]];


}

- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast
{
    self.lastSpawnTime += timeSinceLast;
    if (self.lastSpawnTime > 2)
    {
        self.lastSpawnTime = 0;
        [self add];

    }
}

- (void)update:(CFTimeInterval)currentTime
{
    CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTime;
    self.lastUpdateTime = currentTime;
    if (timeSinceLast > 2)
    {
        timeSinceLast = 1.0 /60.0;
        self.lastUpdateTime = currentTime;
    }

    [self updateWithTimeSinceLastUpdate:timeSinceLast];

}

您必须将SkSpriteNode子类化:

  • 要检测TouchEvents,必须将“userInteractionEnabled”设置为“YES”
  • 要对触摸事件做出反应,请实施适合您需要的触摸方法:

在这种方法中,您可以更改精灵的坐标

将这些函数添加到sksence中

-(void)touchesend:(NSSet*)toucheevent:(UIEvent)event{ /当触摸开始时调用*/

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString:@"plane"])
    {
        NSLog(@"_______touch ended");
        //x and y position of object at Scene
        NSLog(@"%f",node.position.x);
        NSLog(@"%f",node.position.y);
        NSLog(@"%@",node);
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString:@"plane"])
    {
        NSLog(@"_______touch mobing");

    }
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
   SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString:@"plane"])
    {
                NSLog(@"______touch begin");
        //x and y position of object at Scene
        NSLog(@"%f",node.position.x);
        NSLog(@"%f",node.position.y);
        NSLog(@"%@",node);
    }
}

谢谢,但是我不知道在这个方法里面写什么!
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString:@"plane"])
    {
        NSLog(@"_______touch ended");
        //x and y position of object at Scene
        NSLog(@"%f",node.position.x);
        NSLog(@"%f",node.position.y);
        NSLog(@"%@",node);
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString:@"plane"])
    {
        NSLog(@"_______touch mobing");

    }
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
   SKNode *node = [self nodeAtPoint:location];
    if([node.name isEqualToString:@"plane"])
    {
                NSLog(@"______touch begin");
        //x and y position of object at Scene
        NSLog(@"%f",node.position.x);
        NSLog(@"%f",node.position.y);
        NSLog(@"%@",node);
    }
}