【问题标题】:How to get nearest tile from touch with JSTileMap swift如何使用JSTileMap swift从触摸中获取最近的图块
【发布时间】:2015-05-01 07:57:21
【问题描述】:

我刚刚创建了一个在用户触摸位置创建对象的函数,但我希望它根据我的平铺地图自动与平铺的中心对齐。

现在,我只能将对象放置在我的触摸位置,而不是自动将其重定向到最近的图块。

http://puu.sh/ggxOG/2785a2eeca.png(这就是我的瓷砖看起来像 32x32)

JSTileMap 函数

@class JSTileMap;

@interface TMXLayer : SKNode
@property (strong, nonatomic) TMXLayerInfo* layerInfo;
@property (strong, nonatomic) NSMutableSet* tileInfo;  // contains TMXTilesetInfo objects
@property (assign, nonatomic) CGSize mapTileSize;

/** Returns the width of the layer (layerGridSize.width * mapTileSize.width) */
@property (readonly,nonatomic) CGFloat layerWidth;

/** Returns the height of the layer (layerGridSize.height * mapTileSize.height) */
@property (readonly,nonatomic) CGFloat layerHeight;

/** Returns the JSTileMap that contains this layer */
@property (weak, nonatomic) JSTileMap* map;

- (CGPoint)pointForCoord:(CGPoint)coord;
- (CGPoint)coordForPoint:(CGPoint)point;

- (void)removeTileAtCoord:(CGPoint)coord;
- (SKSpriteNode*)tileAt:(CGPoint)point;
- (SKSpriteNode*)tileAtCoord:(CGPoint)coord;
- (int)tileGidAt:(CGPoint)point;
- (id)propertyWithName:(NSString*)name;
- (NSDictionary*)properties;

@end

【问题讨论】:

  • 您能否与我分享您解决此问题的最终代码

标签: ios xcode swift tiled jstilemap


【解决方案1】:

我通过这种方式解决了这个问题以获得最近的瓷砖。

var layer: TMXLayer = map.layerNamed("World1") // retrieve layer from tiled map
if let object = layer.tileAt(location) { // if there is a sprite(tile) at that position then set it as a var
position = object.position // update position var to the position of the sprite
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多