【问题标题】:Move sprite on a background along a grid in sprite kit?沿着精灵套件中的网格在背景上移动精灵?
【发布时间】:2013-11-27 21:54:00
【问题描述】:

如何在精灵工具包中以网格方式拖动和移动精灵?我已经可以在触摸时移动它们,但我需要模拟一个平铺地图......

【问题讨论】:

    标签: scroll grid sprite sprite-kit


    【解决方案1】:

    覆盖 SKScene 中的方法:

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

    那么当你在此收到消息时,只需检查位置:

    UITouch *touch = [touches anyObject];
    if(touch){//
        CGPoint location = [touch locationInNode:self];
    
        .. moving code here
    }
    

    一旦你有了你的位置,只需根据触摸当前所在的网格项移动它:

    //Grid of 20x20 pixels, with entire grid starting at 0,0
    static const CGFloat GridSize = 20;
    
    int gridColumn = (int)(location.x/GridSize); //we can lose the precision here
    int gridRow = (int)(location.y/GridSize); //we can lose the precision here
    
    node.position = CGPointMake(gridColumn*GridSize, gridRow*GridSize);
    

    当然,您可以使用 SKAction 为移动设置动画,但如果您正在跟踪触摸,您可能需要实时进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多