【问题标题】:Change SpriteNodes coordinates with User touch通过用户触摸更改 SpriteNodes 坐标
【发布时间】:2014-05-10 13:09:07
【问题描述】:

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

}

【问题讨论】:

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


    【解决方案1】:

    你必须继承 SkSpriteNode:

    • 要检测 TouchEvent,您必须将“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{}
    

    在这个方法中你可以改变你的精灵的坐标

    【讨论】:

    • 谢谢,但我不知道在这个方法里面写什么!
    【解决方案2】:

    在你的sksence中添加这些功能

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

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多