動畫製作

要使用cocos2d-x在WP7上使用動畫,倒廢了不少功夫,鬱悶是事情就是plist居然沒法解析,老是提示我的plist文件Not Support,不曉得是解析的原因還是其他,試了好多次也沒法,只有“曲線救國“了。

曲線救國的思路就是:自己生成每一幀添加進去。而CCSpriteFrame的生成只有用CCTexture2D。那麼得先生成CCTexture2D.

我使用的是這麼一張PNG貼圖。


1,添加一個類繼承CCScene.

2,聲明一個幀的數組來保存幀和一個精靈變量。

	CCSprite people;
        List<CCSpriteFrame> animFrames;

3,重載OnEnter事件,

因爲我這張圖片是白色背景。所以還是把界面背景弄成白色似乎好些。那麼添加一個白色的層來做爲背景色。

            CCLayerColor layerColor = CCLayerColor.layerWithColor(new ccColor4B(255, 255, 255, 255));
            addChild(layerColor);
然後定義幀數組並且添加數據。

animFrames = new List<CCSpriteFrame>(10);
            CCTexture2D peopleTexture = CCTextureCache.sharedTextureCache().addImage("resources/People");
            CCSpriteFrame frame0 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(0, 0, 96, 96));
            CCSpriteFrame frame1 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(96, 0, 96, 96));
            CCSpriteFrame frame2 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(192, 0, 96, 96));
            CCSpriteFrame frame3 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(288, 0, 96, 96));
            CCSpriteFrame frame4 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(384, 0, 96, 96));
            CCSpriteFrame frame5 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(480, 0, 96, 96));
            CCSpriteFrame frame6 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(576, 0, 96, 96));
            CCSpriteFrame frame7 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(672, 0, 96, 96));
            CCSpriteFrame frame8 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(768, 0, 96, 96));
            CCSpriteFrame frame9 = CCSpriteFrame.frameWithTexture(peopleTexture, new CCRect(864, 0, 96, 96));
            animFrames.Add(frame0);
            animFrames.Add(frame1);
            animFrames.Add(frame2);
            animFrames.Add(frame3);
            animFrames.Add(frame4);
            animFrames.Add(frame5);
            animFrames.Add(frame6);
            animFrames.Add(frame7);
            animFrames.Add(frame8);
            animFrames.Add(frame9);
接着,要往場景中添加一個精靈。精靈由第一幀生成

            people = CCSprite.spriteWithSpriteFrame(frame0);
	    CCSize winSize = CCDirector.sharedDirector().getWinSize();
            people.position = new CCPoint(winSize.width / 2, winSize.height / 2);
            addChild(people);




接着,定義動畫。並且定義精靈people的動作。使之永遠執行動畫。

            CCAnimation animation = CCAnimation.animationWithFrames(animFrames, 0.2f);//第二個參數是幀延遲時間。
            people.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithAnimation(animation, false)));//第二個參數false指動畫結束不返回第一幀
這樣,運行。在這裏,理論上應該看到一個人在跑。但是,我這顯示的黑乎乎的第一個矩形在中央。如果你的機子上已經顯示人在跑了。就不用下面這一步了。

我研究了很久,發現如果不添加一個CCLabelTTF的話,CCSprite的背景就是黑乎乎的。那麼現在權宜之計就添加一個。如果不想顯示出來,可以設置visible屬性爲false


            CCLabelTTF title = CCLabelTTF.labelWithString("Simple Game", "Arial", 24);
            title.position = new CCPoint(winSize.width / 2, winSize.height - 50);
            addChild(title);
那麼,現在就可以顯示出來一個人在跑了。基本代碼都在OnEnter裏面了。那麼,需要這個人的方向反過來呢,就設置people.IsFlipX = true;就行了。



其實,還有一種方法,可以自己來模擬一下,也就是用schedule來模擬,每一段時間來替換一下幀。

那麼註釋掉 people.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithAnimation(animation, false)));

添加這麼一句:schedule(Run, 0.2f);//每兩秒鐘執行一次Run方法

然後再Run方法中替換people的Texture就行了。

先添加一個全局變量int frameIndex = 0;

添加一個方法:

        public void Run(float ft)
        {
            people.DisplayFrame = animFrames[frameIndex % 10];
            frameIndex++;
        }

這樣,也能顯示出來人在跑了。。。。

用了兩種方法來製作動畫,當然,直接執行動畫的還是比較方便的。

可惜的是plist文件解析沒有完成。。。不然還可以更容易些。。。。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章