【发布时间】:2011-06-26 04:13:32
【问题描述】:
我想在等距平铺地图上的特定位置为精灵设置动画。我可以为给定图层上的精灵设置动画,但当它是来自瓦片地图的精灵时则不行。例如下面的工作就好了:
// 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];
现在以下不起作用,我认为它与瓦片地图的工作方式有关(我在 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];
我猜这是因为动画精灵不是瓦片地图该层的瓦片集的一部分。有没有办法将这些动画精灵动态添加到用于在该图层上绘制的某个缓存中(基本上在运行时修改图块集)?我可以稍后从修改后的图块集中删除这些精灵吗?在运行时修改瓦片集时是否仍有 1024x1024 的限制?
在一天结束的时候,我真的希望能够让动画精灵在瓦片地图上从一个瓦片移动到另一个瓦片,但我只是不确定如何以最有效的方式做到这一点。在瓦片地图上有一个效果层和一个包含所有拼写动画的瓦片集(特别是如果你不能将它们放在 1024x1024 中)似乎真的很笨重,因为当效果移动时,组装动画会将瓦片 GID 更新链接在一起瓦片地图。
我知道当图层不是瓷砖地图的一部分时我可以做我想做的事 - 我可以使用屏幕坐标为精灵设置动画和移动,但是当我知道瓷砖坐标时,将这些坐标转换为屏幕坐标(如果瓷砖甚至在屏幕上可见)到目前为止已经回避了我的理解。您如何确定屏幕实际上可以“看到”哪些图块?那么可见图块在屏幕上的像素坐标是多少呢?
感谢您对如何进行此过程的任何想法。
【问题讨论】:
标签: cocos2d-iphone