wp7使用Cocos2d-X for XNA制作一个塔防类游戏 (二)在游戏中加入地图和怪物。(下)

上会书刚说到加载地图,还没有说完现在补上。。完整源代码下载

怪物从a点移动到b点他需要经过1234567点,a123467b就是移动的路径。

有了路径下面来看下怪物。我这里有一张石器时代的豆丁图它是4*4 4个方向的图。

在GameScreen的构造函数中添加如下测试代码,只是为了看个效果。

            //318 ,451
            CCTexture2D mode = CCTextureCache.sharedTextureCache().addImage("Images/Sprite/20011");
            List<CCSpriteFrame> frames = new List<CCSpriteFrame>();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    CCSpriteFrame frame = CCSpriteFrame.frameWithTexture(mode, new CCRect(j * 318 / 4, j * 451 / 4, 318 / 4, 451 / 4));
                    frames.Add(frame);
                }
            }

            CCAnimation ani = CCAnimation.animationWithFrames(frames);
            CCSprite enemy = CCSprite.spriteWithSpriteFrame(frames[0]);
            CCAnimate action = CCAnimate.actionWithDuration(.8f, ani, true);
            enemy.runAction(CCRepeatForever.actionWithAction(action));
            enemy.position = new CCPoint(400, 240);
            addChild(enemy);

运行效果图:

怪物有了,地图有了,现在只需要让怪物按照a1234567b的方式执行MoveTo就完成了今天的任务了。

首先把地图显示和怪物显示的两端代码放到相应的两个类AAMap AAEnemy中,让他们都继承CCNode.

我们的地图是由60*60的14*8的小方格组成,的描述路径就只需要把a1234567b这几个点记录起来文件什么都好自己能读取就行了我这里怎么快怎么来了。

通过地图编辑器我们可以很方便的找到点的位置。

我的记录方式代码:

        public List<CCPoint> Path { get; set; }
        public AAMap()
        {
            CCTMXTiledMap tmxmap = CCTMXTiledMap.tiledMapWithTMXFile("Map/Level1");
            addChild(tmxmap);

            Path = new List<CCPoint>();
            Path.Add(new CCPoint(0, 5));
            Path.Add(new CCPoint(1, 5));
            Path.Add(new CCPoint(1, 2));
            Path.Add(new CCPoint(2, 2));
            Path.Add(new CCPoint(2, 3));
            Path.Add(new CCPoint(7, 3));
            Path.Add(new CCPoint(7, 6));
Path.Add(
new CCPoint(11, 6)); Path.Add(new CCPoint(11, 0)); }

敌人动画有四个状态,AAEnemy的四个行为横向纵向移动四个方法在合适的时候调用相应的动画并且一直执行移动。

       public void Run(List<CCPoint> points)
        {
            CCPoint startPoint = points[0];
            sprite.position = ConvertViewPoint(startPoint);

            List<CCFiniteTimeAction> acs = new List<CCFiniteTimeAction>();
            for (int i = 1; i < points.Count; i++)
            {
                float length = CCPointExtension.ccpLength(CCPointExtension.ccpSub(points[i], points[i - 1])) * 60;
                float time = length / 100;
                CCFiniteTimeAction action = CCMoveTo.actionWithDuration(time, ConvertViewPoint(points[i]));
                acs.Add(action);
            }
            CCAction move = CCSequence.actions(acs.ToArray());
            sprite.runAction(move);
        }

        public CCPoint ConvertViewPoint(CCPoint p)
        {
            return CCDirector.sharedDirector().convertToGL(new CCPoint(p.x * 60 + 30, p.y * 60 + 30));
        }

        public void RightMove()
        {
            List<CCSpriteFrame> temp = new List<CCSpriteFrame>();
            for (int i = 8; i < 12; i++)
            {
                temp.Add(frames[i]);
            }
            CCAnimation ani = CCAnimation.animationWithFrames(temp);
            CCAnimate action = CCAnimate.actionWithDuration(.2f, ani, true);
            sprite.runAction(CCRepeatForever.actionWithAction(action));
        }

最终效果路上奔跑的豆丁:

 完整源代码下载

posted @ 2012-04-19 02:23  XNAGAME  阅读(2438)  评论(3编辑  收藏  举报