使用用户触摸更改SpriteNodes坐标

时间:2014-05-10 13:09:08

标签: ios drag-and-drop draggable sprite-kit

我目前正在为SpriteKit平台Xcode编写一个iOS的小游戏。我写了一个代码,在2条不同的“线”上添加了一些SpriteNodes,所以有一半节点在y坐标100处移动而其他节点在y = 200处移动,它们都从左侧移动在无限循环的右边。现在我希望用户可以触摸一个SpriteNode,然后将他的手指移动到另一个SpriteNode,但它必须位于另一条线上,然后移开他的手指和SpriteNode TOUCHBEGAN应该使用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];

}

2 个答案:

答案 0 :(得分:0)

你必须继承SkSpriteNode:

  • 检测TouchEvents,你必须将“userInteractionEnabled”设置为“YES”
  • 对TouchEvents做出反应,实现适合您需要的触摸方法:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

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

答案 1 :(得分:0)

在你的天赋中添加这些功能

- (void)touchesEnded:(NSSet *)触及withEvent:(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);
    }
}