简体   繁体   English

SpriteKit和JSTileMap

[英]SpriteKit and JSTileMap

I am having difficulties to understand how JSTileMap works with SpriteKit and Tiled. 我很难理解JSTileMap如何与SpriteKit和Tiled一起使用。

The problem is related to pixels and points, and also to loading apropriate tileset using JSTileMap based on screen scale factor. 问题与像素和点有关,也与基于屏幕比例因子的JSTileMap加载适当的图块有关。

File structure in project 项目中的文件结构

In my project there are files which have following structure and this represents my low-res map (1024x768px with 32x32px tiles): 在我的项目中,文件具有以下结构,这表示我的低分辨率贴图(1024x768px和32x32px切片):

  • folder called SD 名为SD的文件夹
    • level-1.tmx 级别1.tmx
    • folder tmx-levels-sd.atlasc 文件夹tmx-levels-sd.atlasc
      • tmx-levels-sd.1.png tmx-levels-sd.1.png
      • tmx-levels-sd.plist tmx-levels-sd.plist

In this case when I load level-1.tmx on non-retina simulator everything is positioned properly. 在这种情况下,当我在非视网膜模拟器上加载level-1.tmx时 ,所有位置均正确放置 When I run the same configuration on retina simulator, again, everything is fine but tiles are scaled. 当我在视网膜模拟器上运行相同的配置时,一切都很好,但是切片已缩放。 Because of that quality suffers. 因此,质量受损。 So, how to load high-res tileset when needed? 那么,如何在需要时加载高分辨率图块集?

The other solution 另一种解决方案

I also tried to solve this by using two versions of the map (low-res and high-res) for each level. 我还尝试通过为每个级别使用两个版本的地图(低分辨率和高分辨率)来解决此问题。 That is actually what I was trying to avoid but still is acceptable: 那实际上是我想要避免的事情,但仍然可以接受:

For this situation I have files structured like this (map is 2048x1536 with 64x64px tiles): 在这种情况下,我的文件结构如下(映射为2048x1536和64x64px磁贴):

  • folder called SD 名为SD的文件夹
    • level-1.tmx 级别1.tmx
    • folder tmx-levels-sd.atlasc 文件夹tmx-levels-sd.atlasc
      • tmx-levels-sd.1.png tmx-levels-sd.1.png
      • tmx-levels-sd.plist tmx-levels-sd.plist
  • folder called HD 名为HD的文件夹
    • level-1-hd.tmx 级别1-hd.tmx
    • folder tmx-levels-hd.atlasc 文件夹tmx-levels-hd.atlasc
      • tmx-levels-hd.1.png tmx-levels-hd.1.png
      • tmx-levels-hd.plist tmx-levels-hd.plist

I guess that map with size 2048x1536 and doubled tilesize is appropriate for high-res version of my map? 我猜该地图的大小为2048x1536并把tilesize加倍是适合我的地图的高分辨率版本吗? Also as you can see, there is no @2x tilesets at all in my project(I guess that we can't use @2x suffix with Tiled editor and JSTileMap?). 您还可以看到,我的项目中根本没有@ 2x瓦片集(我想我们不能在Tiled编辑器和JSTileMap中使用@ 2x后缀吗?)。

Problem with this configuration is that everything is altered(positions and tilesizes are doubled). 这种配置的问题是,一切都被更改了(位置和tileize都加倍了)。 JSTileMap probably works with points, Tiled with pixels, and above all that there is a known iPad simulator bug related to texture atlases which makes me difficult to understand what causing the problems with high-res map. JSTileMap可能与点一起使用,并与像素一起平铺,最重要的是,还有一个已知的iPad模拟器错误与纹理地图集有关,这使我很难理解导致高分辨率地图问题的原因。

Can somebody clarify these things about using JSTileMap,SpriteKit and Tiled altogether ? 有人可以澄清一下有关一起使用JSTileMap,SpriteKit和Tiled的事情吗? Thanks! 谢谢!

I got your msg. 我知道了

Firstly you need both the @1x and @2x versions of the tiles (sprites). 首先,您需要图块(精灵)的@ 1x和@ 2x版本。 So if you have an atlas named myTiles you will need: myTiles.png & myTiles@2x.png. 因此,如果您有一个名为myTiles的地图集,则需要:myTiles.png和myTiles@2x.png。

I created my sprite sheet using texture packer. 我使用纹理打包器创建了精灵表。 I added the @2x images and then on export I had it create a @1x version. 我添加了@ 2x图像,然后在导出时创建了@ 1x版本。

In Tiled only use the @1x version so you would use myTiles.png 在Tiled中仅使用@ 1x版本,因此您将使用myTiles.png

If your map is 32x32 your @1x version will be 32x32 and your @2x version will be 64x64 如果您的地图是32x32,则@ 1x版本将是32x32,而@ 2x版本将是64x64

Forget the SD & HD folders and just use 1 folder called say Levels 忘记SD和HD文件夹,只使用1个名为“说水平”的文件夹

Inside here you need: 在这里,您需要:

level-1.tmx, tmx-levels-sd.1.png, tmx-levels-sd.1@2x.png level-1.tmx,tmx-levels-sd.1.png,tmx-levels-sd.1 @ 2x.png

You can have sub folders under the main levels folder. 您可以在主级别文件夹下拥有子文件夹。 Up to you, but you must have both the @1x version & @2x version of the png's and you can't just duplicate the @1x or @2x versions. 由您决定,但是您必须同时拥有png的@ 1x版本和@ 2x版本,并且不能仅复制@ 1x或@ 2x版本。 The @1x version must be 32x32 based tiles and the @2x version must be double that (64x64). @ 1x版本必须是基于32x32的切片,而@ 2x版本必须是其两倍(64x64)。 In the tmx file itself you don't load the @2x version as this will mess up your level. 在tmx文件本身中,您不会加载@ 2x版本,因为这会弄乱您的级别。

** So your game will be on a 32x32 tileset. **因此,您的游戏将在32x32的图块集上。 What sprite kit will do is double the pixels on retina displays so for this you need to have the @2x version available otherwise it will upscale your @1x version and the tiles will look big and pixelated. Sprite Kit要做的是使视网膜显示器上的像素翻倍,因此,您需要使用@ 2x版本,否则它将升级@ 1x版本,并且图块看起来很大且像素化。 So try not to get confused with the whole pixels to points thing. 因此,请不要将整个像素指向点弄糊涂。

Code Examples: 代码示例:

GameViewController (Load scene): GameViewController(加载场景):

- (void)viewDidLoad
{
[super viewDidLoad];

self.currentLevel = 1;

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;

// Create and configure the scene.
LevelScene *scene = [[LevelScene alloc]initWithSize:skView.bounds.size level:self.currentLevel];
scene.scaleMode = SKSceneScaleModeAspectFill;

// Present the scene.
[skView presentScene:scene];
}

LevelScene (Loads JSTileMap): LevelScene(加载JSTileMap):

#import "LevelScene.h"
#import "SKTAudio.h"
#import "JSTileMap"
#import "Player.h"

@interface LevelScene()

@property (nonatomic, assign) NSUInteger currentLevel;
@property (nonatomic, strong) SKNode *gameNode;
@property (nonatomic, strong) JSTileMap *map;
@property (nonatomic, strong) Player *player;
@property (nonatomic, assign) NSTimeInterval previousUpdateTime;

@implementation LevelScene

-(id)initWithSize:(CGSize)size level:(NSUInteger)currentLevel {

if ((self = [super initWithSize:size])) {
    self.currentLevel = currentLevel;
self.gameNode = [SKNode node];
    [self addChild:self.gameNode];
NSString *levelName = [levelDict objectForKey:@"level"];
    self.map = [JSTileMap mapNamed:levelName];

    [self.gameNode addChild:self.map];
return self;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM