Cocos2d平铺地图添加精灵

发布于 2024-10-06 14:17:39 字数 395 浏览 6 评论 0原文

在 cocos2d-iphone 的等距平铺地图 z 排序示例中,他们使用平铺地图中已有的对象作为玩家精灵,该对象被渲染为在树木中移动。

有没有办法使用通过代码创建的随机 CCNode(不在 tmx 文件中,也不是精灵表的一部分),并使其与图块地图的 z 顺序正确? CCTMXLayer 不支持 addChild,并且在使用时会出现错误:'addChild: CCTMXLayer 不支持。而是使用 setTileGID:at:/tileAt:'

必须有一种方法可以使 CCNode(假设是一个简单的 CCSprite)的 z 顺序与图块地图正确匹配,可以使用 cocos2d 的 API,也可以使用某些 z 缓冲区技术。有什么指点吗?

In the isometric tiled map z-ordering example for cocos2d-iphone, they use an object that is already in the tilemap as the player sprite, which is rendered as moving through the trees.

Is there a way to use a random CCNode, created though code (not in the tmx file, and not part of the sprite sheet), and have it z-ordered correctly with the tilemap? addChild is not supported on a CCTMXLayer, and the gives an error when using that reads:'addChild: is not supported on CCTMXLayer. Instead use setTileGID:at:/tileAt:'.

There's got to be a way to have a CCNode (let's say a simple CCSprite) z-order correctly with a tilemap, either using cocos2d's API, or some z-buffer technique. Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-10-13 14:17:40

如果您想添加一个图块来表示图块 (x,y)(图块地图编辑器坐标中的 (x,y)),请使用以下代码 -

myTileMap 是对 CCTMXTiledMap 对象的引用。

CCTMXLayer *layer=[myTileMap layerNamed:@"yourlayer"];
NSAssert(floorLayer !=nil, @"Ground layer not found!");    
CGPoint tileAddPosition = [layer positionAt: CGPointMake(x,y)];

//Create your CCNode or CCSprite or whatever...say object name is **tileToBeAdded**

tileToBeAdded.anchorPoint = CGPointZero;
tileToBeAdded.position = tileAddPosition;
[myTileMap addChild:addedTile z:1];

If you want to add a tile to say tile (x,y) ( (x,y) in Tiled Map editor coordinates ) then use the following code -

myTileMap is the reference to the CCTMXTiledMap object.

CCTMXLayer *layer=[myTileMap layerNamed:@"yourlayer"];
NSAssert(floorLayer !=nil, @"Ground layer not found!");    
CGPoint tileAddPosition = [layer positionAt: CGPointMake(x,y)];

//Create your CCNode or CCSprite or whatever...say object name is **tileToBeAdded**

tileToBeAdded.anchorPoint = CGPointZero;
tileToBeAdded.position = tileAddPosition;
[myTileMap addChild:addedTile z:1];
与之呼应 2024-10-13 14:17:39

CCTMXLayer 不支持在运行时添加图块,因为它是通过单个 spritesheet 上的所有 sprite 来实现的。由于此实现细节,您也无法在单个图块上调用 setTexture。

最简单的解决方案是在开头将要使用的精灵放在精灵表上。如果你不能这样做,因为它是在运行时生成的或其他什么,下一个最好的事情(不接触 cocos2d 代码)将是修改 spritesheet。

将虚拟图块放入用于 TMXLayer 的 spritesheet 中,然后,一旦获得要使用的图像,就使用 CCRenderTexture 或其他工具将其写入 spritesheet,并使用新生成的纹理作为 TMXLayer 的纹理。

您还可以修改 CCTMXLayer 以允许您的功能,但听起来您想避免这种情况。

The CCTMXLayer doesn't support adding tiles at runtime because it's implemented with all sprites on a single spritesheet. Because of this implementation detail you also can't call setTexture on an individual tile.

The easiest solution would be to have the sprite you want to use on the spritesheet at the beginning. If you can't do this because it's generated at runtime or something, the next best thing (without touching cocos2d code) would be to modify the spritesheet.

Put a dummy tile in the spritesheet you're using for the TMXLayer and then, once you have the image you want to use, write it to the spritesheet using CCRenderTexture or something and use the newly generated Texture as your TMXLayer's texture.

You could also modify the CCTMXLayer to allow for your functionality but it sounds like you want to avoid that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文