从平铺地图动画精灵

时间:2011-02-12 18:34:41

标签: cocos2d-iphone

我想在等距瓷砖地图上的特定位置设置精灵动画。我可以在给定图层上设置一个精灵动画,但不能在它是一个来自tilemap的精灵时动画。例如,以下工作正常:

// make a frame cache
CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"spellanim.plist" textureFile:@"spellanim.pvr.ccz"];
// create a sprite
CCSprite *effectSprite = [CCSprite spriteWithSpriteFrameName:@"spell_strength__33.png"];
// set sprite at center of screen
CGSize screenSize = [[CCDirector sharedDirector] winSize];
effectSprite.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);
// create animation using an animation helper (since animationWithName:delay:frames: will be deprecated)
CCAnimation *animation = [CCAnimation animationWithFrame:@"spell_strength__" frameCount:13 delay:0.3f startAt:33];
CCAnimate *animate = [CCAnimate actionWithAnimation:animation];
// run animation on sprite
[effectSprite runAction:animate];
// add sprite as a child of the layer
[self addChild:effectSprite];

现在以下内容不起作用,我假设它与tile贴图的工作原理有关(我在CCSprite setTexture中得到断言失败:):

// add one to x to offset the spell animation from the player
CGPoint tileCoord = CGPointMake(player.entityTileCoordinate.x + 1, player.entityTileCoordinate.y);
// get the effects layer from the tile map
CCTMXTiledMap *tileMap = (CCTMXTiledMap *)[[TileMapLayer sharedTileMapLayer] getChildByTag:TileMapNode];
CCTMXLayer *effectsLayer = [tileMap layerNamed:@"Effects"];
// get a sprite from the effects layer
CCSprite *effectSprite = [effectsLayer tileAt:CGPointMake(0, 0)];
// move the sprite to the desired location (this works just fine)
CGPoint pointPixel = [effectsLayer positionAt:tileCoord];
[effectSprite runAction:[CCMoveTo actionWithDuration:0.0f position:pointPixel]];

// now animate the sprite
CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"spellanim.plist" textureFile:@"spellanim.pvr.ccz"];
CCAnimation *animation = [CCAnimation animationWithFrame:@"spell_strength__" frameCount:13 delay:0.3f startAt:33];
CCAnimate *animate = [CCAnimate actionWithAnimation:animation];
[effectSprite runAction:animate];

我的猜测是因为动画精灵不是图块地图图层的图块集的一部分。有没有办法动态地将这些动画精灵添加到用于在该层上绘制的缓存(基本上在运行时修改磁贴集)?我可以稍后从修改过的瓷砖组中删除这些精灵吗?在运行时修改tileset时是否仍有1024x1024的限制?

在一天结束时,我真的希望能够在瓷砖地图上将一个动画精灵从一个图块移动到另一个图块,但我只是不确定如何以最有效的方式执行此操作。在瓷砖地图上设置效果图层和使用所有拼写动画的图块集(特别是如果你不能在1024x1024中使用它们)似乎非常笨重,因为组装动画会将效果移动到图块GID更新。瓷砖地图。

我知道当图层不是贴图的一部分时,我可以做我想要的 - 我可以使用屏幕坐标动画和移动精灵,但是当我知道的是贴砖坐标时,将它们转换为屏幕坐标(如果到目前为止,瓷砖甚至可以在屏幕上看到)。你如何确定屏幕实际上可以“看到”的图块?那么可见瓷砖屏幕上的像素坐标是什么?

我很欣赏有关如何进行此过程的任何想法。

1 个答案:

答案 0 :(得分:2)

您猜测的是问题的真正含义,使用spritebatchnode创建平铺贴图。 spritebatchnode就像一个具有更高性能的图层,你只能为它添加精灵,但是只有一个限制,spritebatchnode中的所有精灵都必须共享它们的纹理。因此,当您尝试更改图块以显示动画时,您尝试从其他图块(具有默认图块纹理)的不同纹理中绘制一些内容,这会导致崩溃或出现故障。我没有亲自测试,但我认为如果你尝试将所有的框架放在同一个纹理中,你就可以解决其他问题了。

相关问题