<cocos2d-x for wp7>动画制作
2012-04-13 23:18
363 查看
要使用cocos2d-x在WP7上使用动画,倒废了不少功夫,郁闷是事情就是plist居然没法解析,老是提示我的plist文件Not Support,不晓得是解析的原因还是其他,试了好多次也没法,只有“曲线救国“了。
曲线救国的思路就是:自己生成每一帧添加进去。而CCSpriteFrame的生成只有用CCTexture2D。那么得先生成CCTexture2D.
我使用的是这么一张PNG贴图。
1,添加一个类继承CCScene.
2,声明一个帧的数组来保存帧和一个精灵变量。
3,重载OnEnter事件,
因为我这张图片是白色背景。所以还是把界面背景弄成白色似乎好些。那么添加一个白色的层来做为背景色。
然后定义帧数组并且添加数据。
接着,要往场景中添加一个精灵。精灵由第一帧生成
接着,定义动画。并且定义精灵people的动作。使之永远执行动画。
我研究了很久,发现如果不添加一个CCLabelTTF的话,CCSprite的背景就是黑乎乎的。那么现在权宜之计就添加一个。如果不想显示出来,可以设置visible属性为false
那么,现在就可以显示出来一个人在跑了。基本代码都在OnEnter里面了。那么,需要这个人的方向反过来呢,就设置people.IsFlipX = true;就行了。
其实,还有一种方法,可以自己来模拟一下,也就是用schedule来模拟,每一段时间来替换一下帧。
那么注释掉 people.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithAnimation(animation, false)));
添加这么一句:schedule(Run, 0.2f);//每两秒钟执行一次Run方法
然后再Run方法中替换people的Texture就行了。
先添加一个全局变量int frameIndex = 0;
添加一个方法:
这样,也能显示出来人在跑了。。。。
用了两种方法来制作动画,当然,直接执行动画的还是比较方便的。
可惜的是plist文件解析没有完成。。。不然还可以更容易些。。。。
曲线救国的思路就是:自己生成每一帧添加进去。而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文件解析没有完成。。。不然还可以更容易些。。。。
相关文章推荐
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏:不一样的战斗(回合制战斗)(四)
- <cocos2d-x for wp7>使用cocos2d-x和BOX2D来制作一个BreakOut(打砖块)游戏(一)
- <cocos2d-x for wp7>使用cocos2d-x和BOX2D来制作一个BreakOut(打砖块)游戏(二)
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏:碰撞检测和收集物品(二)
- <cocos2d-x for wp7>使用cocos2d-x制作一个太空射击游戏
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏(一)
- <cocos2d-x for wp7>在cocos2d-x使用spritesheet和用spritesheet创建动画
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏:加入敌人和战斗(三)
- Cocos2d-x 动画(Animate)的制作
- cocos2d-x v3.0beta2使用cocostudio制作的动画
- cocos2d-x中制作动画
- cocos2d-x中制作动画
- cocos2d或者iOS制作视频+虚列动画做游戏时候,视频播放和结束时顿闪问题解决方案
- <cocos2d-x for wp7>在cocos2d-x里面手势识别
- 【笔记】Cocos2d-x高级开发教程:制作自己的<捕鱼达人> 笔记一:序_前言_第一章
- cocos2d-x学习之连帧动画制作
- cocos2d-x3.1.1动画制作编辑核心代码
- 【Cocos2d-x】之使用两张图片制作动画
- 如何在cocos2d里面用TexturePacker制作可移动的炮炮兵动画
- cocos2d-x动画制作(cocos2d-x2.1)