cocos2d-xna学习笔记4

修改游戏程序入口。在AppDelegate里的applicationDidFinishLauching()方法里添加一句:

  pScene.addChild(Tweejump.Classes.MainMenu.node());

public override bool applicationDidFinishLaunching()

        {

            //initialize director

            CCDirector pDirector = CCDirector.sharedDirector();

            pDirector.setOpenGLView();



            //turn on display FPS

            pDirector.DisplayFPS = true;



            pDirector.deviceOrientation = ccDeviceOrientation.CCDeviceOrientationPortrait;



            // set FPS. the default value is 1.0/60 if you don't call this

            pDirector.animationInterval = 1.0 / 60;



            // create a scene. it's an autorelease object

            CCScene pScene = CCScene.node();

            //添加了这句,修改了游戏的入口

            pScene.addChild(Tweejump.Classes.MainMenu.node());

            //run

            pDirector.runWithScene(pScene);



            return true;

        }


然后在mainmenu.cs里又添加了game.cs的入口
 void playCallback(CCObject sender)

        {

            CCScene pScene = CCScene.node();

            pScene.addChild(Tweejump.Classes.Game.node());

            CCDirector.sharedDirector().pushScene(pScene);

        }

当然,在game.cs里还要添加如下代码:

public static new CCLayer node()

        {

            Game ret = new Game();



            if (ret.init())

            {

                return ret;

            }

            return null;

        }


接下来还要重写Init方法

public override bool init();

在init方法里加载了游戏平台(注意,平台是多个且位置不定。见学习笔记2)、小鸟、金币(注意,平台是多个且位置不定。见学习笔记2)等。全部都放在spritesmanager里

在init方法里最重要的方法:this.schedule(step);

schedule,我猜应该是一个实时调用的类,而step里面更新了小鸟是否吃到了金币、小鸟的位移效果等,jump()实现了小鸟踩到平台时的跳跃(y)的变化,

代码:

new void step(float dt)

        {

            //

            base.step(dt);



            if (gameSuspended) return;



            CCSpriteBatchNode spriteManager = (CCSpriteBatchNode)getChildByTag((int)tags.kSpriteManager);

            CCSprite bird = (CCSprite)spriteManager.getChildByTag((int)tags.kBird);

            //小鸟x水平的变化

            bird_pos.x += bird_vel.x * dt;



            #region ----

            //if (bird_vel.x < -30f && birdLookingRight)

            //{

            //    birdLookingRight = false;

            //    bird.scaleX = -sx;

            //}

            //else if (bird_vel.x > 30f && birdLookingRight)

            //{

            //    birdLookingRight = true;

            //    bird.scaleX = sx;

            //} 

            #endregion

            //480是手机宽度,max_x和min_x设置的理由都是基于小鸟图片大小确立的。

            CCSize bird_size = new CCSize(bird.contentSize.width * sx, bird.contentSize.height * sy);

            float max_x = 480 - bird_size.width / 2;

            float min_x = 0 + bird_size.width / 2;

            //当小鸟的x位置大于max_x时,表示已经跨出手机屏幕,故设置最大值为max_x

            //当小鸟的x位置小于min_x时,表示已经跨出手机屏幕,故设置最小值为min_x

            if (bird_pos.x > max_x) bird_pos.x = max_x;

            if (bird_pos.x < min_x) bird_pos.x = min_x;

            //小鸟垂直坐标变化,为什么如此设置?

            bird_vel.y += bird_acc.y * dt;

            bird_pos.y += bird_vel.y * dt;



            CCSprite bonus = (CCSprite)spriteManager.getChildByTag((int)tags.kBonusStartTag + currentBonusType);

            if (bonus.visible)

            {

                //吃到金币

                CCPoint bonus_pos = bonus.position;

                float range = 20f;

                if (bird_pos.x > bonus_pos.x - range &&

                    bird_pos.x < bonus_pos.x + range &&

                    bird_pos.y > bonus_pos.y - range &&

                    bird_pos.y < bonus_pos.y + range)

                {

                    switch (currentBonusType)

                    {

                        case (int)kbonus.kBonus5: score += 5000; break;

                        case (int)kbonus.kBonus10: score += 10000; break;

                        case (int)kbonus.kBonus50: score += 50000; break;

                        case (int)kbonus.kBonus100: score += 100000; break;

                    }

                    //得分的显示实现-1,游戏中没有动作时分数的更新

                    string scoreStr = score.ToString();

                    CCLabelBMFont scoreLabel = (CCLabelBMFont)this.getChildByTag((int)tags.kScoreLabel);

                    scoreLabel.setString(scoreStr);

                    var a1 = CCScaleTo.actionWithDuration(.2f, 1.5f, .8f);

                    var a2 = CCScaleTo.actionWithDuration(.2f, 1.0f, 1f);

                    var a3 = CCSequence.actions(a1, a2, a1, a2, a1, a2);

                    scoreLabel.runAction(a3);

                    this.resetBonus();

                }

            }



            int t;

            //当小鸟的高度小于0时,分两种情况:有平台则跳越,没平台且高度低于自身的一半即游戏结束

            if (bird_vel.y < 0)

            {

                t = (int)tags.kPlatformsStartTag;

                for (int i = t; i < (int)tags.kPlatformsStartTag + kNumPlatforms; i++)

                {

                    CCSprite platform = (CCSprite)spriteManager.getChildByTag(i);



                    CCSize platform_size = new CCSize(platform.contentSize.width * sx, platform.contentSize.height * sy);

                    CCPoint platform_pos = platform.position;

                    //max_x和min_x的差分别是一个平台加上20

                    max_x = platform_pos.x - platform_size.width / 2 - 10;

                    min_x = platform_pos.x + platform_size.width / 2 + 10;

                    float min_y = platform_pos.y + (platform_size.height + bird_size.height) / 2 - kPlatformTopPadding;

                    //platform_pos.y、 min_y、 max_x、 min_x恰好构成了一个平台的范围

                    if (bird_pos.x > max_x &&

                        bird_pos.x < min_x &&

                        bird_pos.y > platform_pos.y &&

                        bird_pos.y < min_y)

                    {

                        this.jump();

                    }

                }

                //判断游戏结束的条件:即当小鸟的Y低于自身图片的一半时生效

                if (bird_pos.y < bird_size.height / 2)

                {

                    this.gameOver();

                }

            }

            else

                //为什么设置400,应该是小鸟跳动的高度

                if (bird_pos.y > 400)

                {

                    float delta = bird_pos.y - 400;

                    bird_pos.y = 400;



                    currentPlatformY -= delta;

                    //更新游戏中云的高度吗?

                    t = (int)tags.kCloudsStartTag;

                    for (int i = t; i < t + kNumClouds; i++)

                    {

                        CCSprite cloud = (CCSprite)spriteManager.getChildByTag(i);

                        Scale(cloud);

                        CCPoint pos = cloud.position;

                        pos.y -= delta * cloud.scaleY * 0.8f;

                        if (pos.y < -cloud.contentSize.height * sy / 2)

                        {

                            currentCloudTag = i;

                            this.resetCloud();

                        }

                        else

                        {

                            cloud.position = pos;

                        }

                    }

                    //更新游戏中平台的高度?

                    t = (int)tags.kPlatformsStartTag;

                    for (int i = t; i < t + kNumPlatforms; i++)

                    {

                        CCSprite platform = (CCSprite)spriteManager.getChildByTag(i);

                        Scale(platform);

                        CCPoint pos = platform.position;

                        pos = new CCPoint(pos.x, pos.y - delta);

                        if (pos.y < -platform.contentSize.height * sy / 2)

                        {

                            currentPlatformTag = i;

                            this.resetPlatform();

                        }

                        else

                        {

                            platform.position = pos;

                        }

                    }



                    if (bonus.visible)

                    {

                        CCPoint pos = bonus.position;

                        pos.y -= delta;

                        if (pos.y < -bonus.contentSize.height * sy / 2)

                        {

                            this.resetBonus();

                        }

                        else

                        {

                            bonus.position = pos;

                        }

                    }

                    //得分的显示实现-2,游戏中有动作时分数的更新

                    score += (int)delta;

                    string scoreStr = score.ToString();



                    CCLabelBMFont scoreLabel = (CCLabelBMFont)this.getChildByTag((int)tags.kScoreLabel);

                    scoreLabel.setString(scoreStr);

                }



            bird.position = bird_pos;

        }
void jump()

        {

            bird_vel.y = 583f + Math.Abs(bird_vel.x);

        }

 

  

 

你可能感兴趣的:(cocos2d-x)