动画制作

要使用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文件解析没有完成。。。不然还可以更容易些。。。。





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