Cocos2d 3.x 代码示例1(个人理解)

都是用我自己懂的话来写,在我成为大神之前,我还是个自私的人。。。因为我根本大方不起来啊。。哈哈


1.加载.json文件

#include "cocos2d.h"

#include "cocostudio/CocoStudio.h"

#include "ui/CocosGUI.h"

USING_NS_CC;

using namespacecocostudio;

using namespaceui;


auto widget =cocostudio::GUIReader::getInstance()->widgetFromJsonFile("Piano.json");

   addChild(widget);


2.触摸事件处理

方式1:

    auto listener =EventListenerTouchOneByOne::create();

    listener->onTouchBegan = [=](Touch *pTouch,Event *pEvent)

    {

       returntrue;

    };

    

    listener->onTouchMoved = [=](Touch *pTouch,Event *pEvent)

    {     

auto newPos = plane->getPosition() + pTouch->getDelta();

    };



方式2:

函数调用:

buttonWidget->addTouchEventListener(this,toucheventselector(HelloWorld::touchButton));

.h声明

void touchButton(cocos2d::Ref* object,cocos2d::ui::TouchEventType type);


3.音效的使用 (buf为音效名称“ .wav”)

#include "SimpleAudioEngine.h"

CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(buf);



4.粒子添加(粒子编辑器 和 系统自带)

ParticleSystemQuad* particle =ParticleSystemQuad::create("slowLight.plist");

addChild(particle);



5.获取可视区域和原点

auto visibleSize =Director::getInstance()->getVisibleSize();

auto origin =Director::getInstance()->getVisibleOrigin();


6.动作的使用

auto heart =Sprite::create("heart.png");

auto move =MoveBy::create(5,Point(visibleSize.width/6,0));

auto rev = move->reverse();//创建相反动作

auto actionSequence =Sequence::create(move, rev,NULL);

heart->runAction(RepeatForever::create(actionSequence));


7.加载动画

    ArmatureDataManager::getInstance()->addArmatureFileInfo("Hero.ExportJson");

   Armature *plane =Armature::create("Hero");

    plane->getAnimation()->play("HeroIdle");

    plane->setPosition(Point(visibleSize.width/2,visibleSize.height/2));

   addChild(plane);

8.schedule的使用

this->schedule(schedule_selector(HelloWorld::HeroFire),HERO_FIRE_DURATION);

voidHelloWorld::HeroFire(float dt);


9.添加物理系统

auto scene = Scene::createWithPhysics();

scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

scene->getPhysicsWorld()->setGravity(Vect(0,0));


10.添加body

    auto body =PhysicsBody::createBox(m_hero->getContentSize());

    body->setTag(ContactTag::HERO);

    body->setContactTestBitmask(0xFFFFFFF);//按位与,碰撞

    m_hero->setPhysicsBody(body);

11.碰撞处理

EventListenerPhysicsContact *hitListener = EventListenerPhysicsContact::create();

collisionCheck(hitListener);   

void HelloWorld::collisionCheck(EventListenerPhysicsContact *hitListener)

{

    hitListener->onContactBegin = [=](constPhysicsContact& contact)

    {

       auto tagA = contact.getShapeA()->getBody()->getTag();

       auto tagB = contact.getShapeB()->getBody()->getTag();

        

       if(tagA != tagB)

        {

           //hero die

           if((tagA == ContactTag::HERO && tagB!=ContactTag::HERO_BULLET) ||

               (tagB ==ContactTag::HERO && tagA!=ContactTag::HERO_BULLET))

            {

                this->unscheduleAllSelectors();

                Director::getInstance()->getEventDispatcher()->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE);

               this->removeChild(contact.getShapeA()->getBody()->getNode());

               this->removeChild(contact.getShapeB()->getBody()->getNode());

            }

            

           //enemy die

           if((tagA == ContactTag::HERO_BULLET && tagB!=ContactTag::HERO) ||

               (tagB ==ContactTag::HERO_BULLET && tagA!=ContactTag::HERO))

            {

               if(tagA == HERO_BULLET)

                {

                   auto enemy = contact.getShapeB()->getBody()->getNode();

                    enemy->unscheduleAllSelectors();

                   this->removeChild(enemy);

                   this->removeChild(contact.getShapeA()->getBody()->getNode());

                }else {

                   auto enemy = contact.getShapeA()->getBody()->getNode();

                    enemy->unscheduleAllSelectors();

                   this->removeChild(enemy);

                   this->removeChild(contact.getShapeB()->getBody()->getNode());

                }

            }

            

        }

        

        return false;

    };

    

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(hitListener,this);

}


12.cocostudio2.x的ui的函数调用,重新游戏和退出游戏(无头脑搞了一个晚上,想通了才发现原来就那么一回事)

void touchButton(Ref *pSender, cocos2d::ui::Widget::TouchEventType type); //.h


auto gameOverDlg = CSLoader::createNode("Node.csb");

gameOverDlg->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));

this->addChild(gameOverDlg,1);                

auto restart = static_cast<ui::Button*>(gameOverDlg->getChildByName("Button_1"));

auto exit = static_cast<ui::Button*>(gameOverDlg->getChildByName("Button_2"));      

restart->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchButton, this));

exit->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchButton, this));


void HelloWorld::touchButton(Ref* pSender, Widget::TouchEventType type)

{

    if (type == Widget::TouchEventType::ENDED) {

        Widget* widget = dynamic_cast<Widget*>(pSender);

        if(widget)

        {

            auto name = widget->getName();

            if(name.compare("Button_2") == 0)

            {

                Director::getInstance()->end();

#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

                    exit(0);

#endif

            } else if (name.compare("Button_1") == 0)

            {

                removeAllChildren();

                Director::getInstance()->getEventDispatcher()->removeAllEventListeners();

                Director::getInstance()->getRunningScene()->getPhysicsWorld()->removeAllBodies();

                startGame();

            }

        }

    }

}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值