Cocos2d 基础知识

本文介绍了Cocos2d的基础知识,包括Director类、Scene、Layer、Sprite的使用,详细讲解了Menu和MenuItem、Label、ProgressTimer、动作(Action)的各类操作,如旋转、移动、缩放、进度条等,并提到了场景跳转的动画效果和音乐播放控制。
摘要由CSDN通过智能技术生成

(1)Director类(导演类):  

(2)Scene类(场景类):

(3)Layer类(图层类):

(4)Sprite类(精灵类):

//创造精灵

auto sp=Sprite::create("circle.png");

  this->setTexture(str);//换图片


1、Menu和MenultemMenu:菜单它是Menultem的容器

Menultem:菜单项

MenultemToggle:可以容纳多个Menultem的子类,点击可以切换下一个显示的按钮。

MenultenImage:Menultem的子类,就是我们平时看到的图片按钮。

MenultemAtlasFont: 我们平时看到的汉字按钮

Menultem的子类有:MenultemLabel、MenultemAtlasFont、MenultemFont、MenultemSprite、Menultenlmage、MenultemToggle

menu->setPosition(Vec2::ZERO);


setCallback  回调函数,用于响应菜单



2、Label(字体):

a)使用系统字体创建auto label=Label::createwithSystemFont中泰教育“fonts/Marker Felt.ttf”30

b)使用BMF字体创建

auto bmfLabel=Label::createwithBMFont(“FontBMFont.fnt”,“植物大战僵尸B2”;

c)使用charmap创建

auto charmapLabel=Label::createwithCharMap(“FontALtas.png”,14,21,48);/ 第一个字符acall

charmapLabel->setPositionVec2240160));

charmapLabel->setString(“123456”);

this->addChildcharmaplLabe1;


3、ProgressTimer 进度条百分比  Percentage

a)圆周形进度条

auto progressCircle=ProgressTimer::create(Sprite::create(“circle.png”));

progressCircle->setPositionVec2240160));

this->addChildprogressCircle;

progressCircle->setPercentage60;//显示百分比

progressCircle->setType(ProgressTimer::Type::RADIAL);//进度条类型

//auto action_1=ProgressFromTo::create(3,0,100);//进度条动作 3秒从0-100

// progressCircle->runAction(action_1);


b)长方形进度条

auto progressLong =ProgressTimer::create(Sprite::create(“Long.png”));

progressLong->setPosition(Vec2(240,160));

progressLong ->setType(ProgressTimer::Type::BAR); //进度条类型

progressLong->setMidpoint(Vec2(0,0.5)); //进度条方向 从0开始

progressLong->setBarChangeRate(Vec2(1,0)); //进度 条 变化率  到(1,0)

progressLong->setPercentage(50);

this->addChild(progressLong);

// auto action_2=ProgressFromTo::create(3,0,100);

// progressLong->runAction(action_2);


LoadingBar进度条       百分比Percent

我们看到游戏的资源加载界面的进度显示就是通过这个控件完成的。

(人物血量)

auto loadingBar =LoadingBar::create("loadingbar.png");

 loadingBar->setPosition(Vec2(240,280));

        this->addChild(loadingBar,10);

        schedule([=](float dt) {

            static int time=1;

            if (time>=100)

            {

                unschedule("loadingbar");

            }

            time+=10;

            loadingBar->setPercent(time);//百分比


        },1,"loadingbar");




Schedule//时间调度器


开启一个时间调度器不断调用updateZT,间隔为0.5s,重复次数为10次,第一次执行会延迟5s 

schedule(CC_CALLBACK_1(Helloworld::updateZT,this),0.5,10,5,updateZT”);

 schedule([=](float dt) {

        static int time=1;

        if (time>=100)

        {

            

            unschedule("loadingbar");

            


           auto scene =My2::createScene();

           Director::getInstance()->pushScene(scene);//入栈

            SimpleAudioEngine::getInstance()->pauseBackgroundMusic();//暂停;

        

        }

        time+=10;

        loadingBar->setPercent(time);

    

    },1,"loadingbar");



Cocos2d-X的动作动作(Action):

    (1)动作是对Node以及其子类的操作实现实体,也就是说Node和其子类都可以“执行动作”。

    (2)动作具有传递性,当一个节点在做动作,那么它的子节点也会相应的做出该动作,这就是传递,更是符合正常思维。

auto sp=Sprite::create("ball1.jpg");

    sp->setPosition(Vec2(160, 240));

    this->addChild(sp,10);

    

    auto sp1=RotateBy::create(0.5, 0, 180);

    

    sp->runAction(sp1);


    1.1持续动作(ActionInterval):需要一定时间去执行的动作基本动作:

(1)MoveTo和MoveBy    //移动位置            移动方向

(2)RotateTo和RotateBy  //旋转角度到       //旋转角度

(3)ScaleTo和ScaleBy    //比例(放大/缩小) //比例(放大/缩小)

(4)JumpTo和JumpBy       跳跃

(5)Blink//Blinkauto      闪烁

(6)reverse   //相反

函数auto _moveBy=MoveBy::create(2,Vec2(100,0));

auto _moveByBack=_moveBy->reverse();

testsp->runAction(_moveByBack);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值