《Cocos2d-x 3.x游戏开发之旅》读书笔记(1)

来源:互联网 发布:linuxwifi渗透软件 编辑:程序博客网 时间:2024/05/16 12:55

一、场景切换:

1Director::getInstance()->runWithScene(scene);//加载第一个场景

2Director::getInstance()->replaceScene(scene);//切换场景

3Director::getInstance()->replaceScene(TransitionSlideInT::create(3.0f,SecondScene::createScene()));//特效切换场景(还有很多形式可以选择,自行百度或Google

4pushScene()popScene()//pushScene()没有释放场景

 

二、Value的使用(可以用于类型间转换)

1Value val=Valuetype//根据各种类型初始化

2val.asType()//转化为各种类型

 

三、Vector的使用(和C++几乎一样)

          auto label1= Label::create("wugaungyuan1", "Arial", 30);

label1->setPosition(100,100);

 

autolabel2 = Label::create("wugaungyuan2", "Arial", 30);

label2->setPosition(200,200);

   

Vector<Label*>lvec;

lvec.pushBack(label1);

lvec.pushBack(label2);

 

for(auto label : lvec)

{

this->addChild(label,2);

}

 

四、Map的使用

Map<int,Label*> map;

 

for(int i = 0; i < 100; ++i)

{

std::stringname = "wugaungyuan." + Value(i).asString();

Label*label = Label::create(name.c_str(), "Arial", 30);

map.insert(i,label);

}

 

map.at(40)->setPosition(Point(100,100));

this->addChild(map.at(40),2);

 

五、音乐播放

1、背景乐

#include "SimpleAudioEngine.h"//必须包含头文件

 

CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(

"sky_city.mp3", true);//播放音乐“sky_city”(包含在资源文件里),第二个参数表示是否循环播放

 

2、特效声音

#include "SimpleAudioEngine.h"//必须包含头文件

 

CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("error.wav");

 

六、控件

1、九妹(可拉伸控件)

#include"cocos-ext.h"

using namespacecocos2d::extension;

 

Scale9Sprite* nine =Scale9Sprite::create("button.png");

nine->setContentSize(Size(100,200));

nine->setPosition(Point(100,100));

this->addChild(nine,2);

 

(第一次配置还需要的额外工作)




和按钮配合使用:

Scale9Sprite* btn =Scale9Sprite::create("button.png");

Scale9Sprite*btnDown = Scale9Sprite::create("buttonHighlighted.png");

Label*title = Label::create("Touch Me", "Marker Felt", 30);

 

ControlButton* button =ControlButton::create(title, btn);//ControlButton已不再维护

button->setBackgroundSpriteForState(btnDown,Control::State::HIGH_LIGHTED);

 

button->setPosition(Point(100,100));

this->addChild(button,2);

2CocoStudio使用

使用CocoStdio需要的配置





 

头文件内加入:

#include"editor-support/cocostudio/CCSGUIReader.h"

#include"ui/CocosGUI.h"

 

using namespacecocos2d::ui;

using namespacecocostudio;

 

void onClick(Ref*, TouchEventType type);//点击事件的回调函数

 

ImageView* m_xiaoruoImg;//图像设置为全局变量

 

cpp文件内容:

//显示控件

auto UI =cocostudio::GUIReader::getInstance()->

widgetFromJsonFile("NewUi_1.ExportJson");

UI->setPosition(Point(0,0));

this->addChild(UI,2);

//获取控件对象

Button* xiaoruoBtn =(Button*)Helper::seekWidgetByName(UI, "xiaoruoBtn");

m_xiaoruoImg =(ImageView*)Helper::seekWidgetByName(UI, "xiaoruoImg");

//添加按钮点击监听

xiaoruoBtn->addTouchEventListener(this,toucheventselector(HelloWorld::onClick));

 

//onClick函数实现,点击按钮,图像消失或出现

voidHelloWorld::onClick(Ref*, TouchEventType type)

{

switch(type)

{

caseTouchEventType::TOUCH_EVENT_BEGAN:break;

caseTouchEventType::TOUCH_EVENT_MOVED:break;

caseTouchEventType::TOUCH_EVENT_ENDED:

if(m_xiaoruoImg->isVisible())

{

m_xiaoruoImg->setVisible(false);

}

else

{

m_xiaoruoImg->setVisible(true);

}

break;

caseTouchEventType::TOUCH_EVENT_CANCELED:break;//因一些突发事件而中断

}

}

注意:CocoStudio导出的loadingBar不可用。。。

还有其他很多控件可以使用,需要的时候可以在回头看看有没有用得上的

 

七、动作

1MoveTo(移动到目的位置)

auto moveTo =MoveTo::create(0.9f, Point(200, 200));

//精灵执行动作

sprite->runAction(moveTo);

2MoveBy(移动相对现在位置的距离)

automoveTo = MoveBy::create(0.9f,Point(200, 200));

//精灵执行动作

sprite->runAction(moveTo);

3ScaleToScaleBy类似,第一个参数是时间,第二和第三分别是XY拉伸倍数

autoscaleTo =ScaleTo::create(0.9f,2.0f,3.0f);//以原始大小作为参考拉伸

//精灵执行动作

sprite->runAction(moveTo);

autoscaleBy =ScaleBy::create(0.9f,2.0f,3.0f);//以当前大小作为参考进行拉伸

//精灵执行动作

sprite->runAction(moveTo);

4、闪烁Blink

auto blink = Blink::create(2.9f, 3);//第一个参数为时间,第二个参数为闪烁次数

//精灵执行动作

sprite->runAction(blink);

5、贝塞尔曲线运动

ccBezierConfig bezier;//贝塞尔曲线配置

bezier.controlPoint_1 = Point(100, 0);//谷值

bezier.controlPoint_2 = Point(200, 300);//峰值

bezier.endPosition = Point(300, 250);//终点

//BezierTo中的点是实质的

auto bezierTo = BezierTo::create(2.0f, bezier);

sprite->runAction(bezierTo);

//BezierBy中的点是相对原点的偏移量

auto bezierBy =BezierBy::create(2.0f, bezier);

sprite->runAction(bezierBy);

6、重复运动

Repeat::create(动作,重复次数);

RepeatForever::create(动作);

7、同时进行多种动作Spawn

auto moveBy =MoveBy::create(2.0f,Point(50,0));

auto jumpBy =JumpBy::create(1.0f, Point(30, 0), 20, 2);

auto rotateBy =RotateBy::create(2.5f, 200, 5);

Action* action = Spawn::create(moveBy, jumpBy, rotateBy, NULL);//最后一个NULL表示结束

sprite->runAction(action);

8、顺序进行多种动作Sequence

auto moveBy =MoveBy::create(2.0f,Point(50,0));

auto jumpBy =JumpBy::create(1.0f, Point(30, 0), 20, 2);

auto rotateBy =RotateBy::create(2.5f, 200, 5);

Action* action =Sequence::create(moveBy, jumpBy, rotateBy, NULL);

sprite->runAction(action);

9、回调函数

auto func = []()//lambda函数,"[]"内可以写入“=”“&”等

{

log("hello");

};

CallFunc* callFunc = CallFunc::create(func);//回调函数

this->runAction(callFunc);

 

 

八、屏幕触摸事件

1、单点触控

/*创建两个精灵,相互有重叠的部分 */

    Sprite* sp1 =Sprite::create("sprite1.png");

    sp1->setPosition(Point(visibleSize.width* 0.5f, visibleSize.height * 0.5f));

    this->addChild(sp1);

 

    Sprite* sp2 =Sprite::create("sprite2.png");

    sp2->setPosition(Point(visibleSize.width* 0.5f, visibleSize.height * 0.5f));

    this->addChild(sp2);

//事件触控事件监控

   auto listener =EventListenerTouchOneByOne::create();

   listener->setSwallowTouches(true);//事件截断,不能传递到下一个对象

    listener->onTouchBegan = [](Touch*touch, Event* event){

        /*注册监听事件的时候不是绑定了一个Node对象么?在这里就可以取出这个对象 */

        auto target =static_cast<Sprite*>(event->getCurrentTarget());

        Point pos =Director::getInstance()->convertToGL(touch->getLocationInView());

        /*判断点击的坐标是否在精灵的范围内 */

        if(target->getBoundingBox().containsPoint(pos))

        {

            /*设置精灵的透明度为100 */

            target->setOpacity(100);

            return true;

        }

        return false;

    };

    listener->onTouchEnded = [](Touch*touch, Event* event){

        /*恢复精灵的透明度 */

        auto target =static_cast<Sprite*>(event->getCurrentTarget());

        target->setOpacity(255);

    };

    /*注册监听事件,绑定精灵1 */

   _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,sp1);

    /*注册监听事件,绑定精灵2,这里要注意,listener对象拷贝了一个 */

   _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),sp2);

0 0
原创粉丝点击