2011-02-12 86 views
1

我想在等軸測地圖上的特定位置爲精靈創建動畫。我可以在一個給定的圖層上對一個精靈進行動畫處理,但是當它從一個地圖上生成一個精靈時則不會。例如,下面的作品就好了:從瓦片貼圖動畫雪碧

// 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]; 

我的猜測是因爲動畫精靈不是瓷磚地圖的圖層集的一部分。有沒有辦法將這些動畫精靈動態添加到用於在該圖層上繪製的某個緩存(基本上是在運行時修改拼貼集)?我以後可以從修改後的瓷磚組中刪除這些精靈嗎?在運行時修改tileset時是否仍然存在1024x1024限制?

在一天結束的時候,我真的希望能夠在瓷磚地圖上從一個瓷磚移動到另一個瓷磚,但我不知道如何以最有效的方式做到這一點。在瓦片地圖上有一個效果圖層,並且使用所有咒語動畫(尤其是如果你不能將它們裝入到1024x1024)時,看起來真的很笨重,因爲組裝動畫時會將瓦片GID更新鏈接在一起,瓷磚地圖。

我知道如果圖層不是地圖的一部分,我可以做我想要的東西 - 我可以使用屏幕座標動畫和移動精靈,但是當我知道的是平鋪座標時,將它們轉換爲屏幕座標(如果這塊瓷磚在屏幕上甚至是可見的)迄今爲止已經迴避了我的理解。你如何確定屏幕實際上可以「看到」的圖塊?什麼是可見瓦片屏幕上的像素座標?

我很欣賞關於如何去做這個過程的任何想法。

回答

2

你猜是什麼問題的真正cuase,瓷磚地圖是使用spritebatchnode創建的。 spritebatchnode就像一個更高性能的圖層,你只能添加精靈,但是有一個限制,一個spritebatchnode中的所有精靈必須共享它們的紋理。所以當你試圖改變一個圖塊來顯示一個動畫時,你試圖從其他圖塊(具有默認圖塊紋理)的不同紋理中繪製一些東西,並導致崩潰或出現故障。我沒有自己測試,但我認爲如果你嘗試把你所有的幀放在一個相同的紋理中,你就放置其他的瓷磚,問題就會解決。