[針對初學者]Cocos 瞎搞小技巧1

1 如何實現界面全屏

在AppDelegate.cpp中

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if (!glview) {
        glview = GLViewImpl::createWithRect("TGame", Rect(0, 0, 960, 640));
        director->setOpenGLView(glview);
    }
    // ......
}

這行代碼是用設置窗口大小:

glview = GLViewImpl::createWithRect("TGame", Rect(0, 0, 960, 640));

但是我們需要設置全屏,如何設置呢?將該行代碼改爲如下:

glview = GLViewImpl::createWithFullScreen("TGame");

2 Tiled創建地圖參數設置注意事項

第一次設置地圖總是出問題,經過多次嘗試和百度,發現下面這個參數是OK的。
只用注意: Tile Layer Format
裏面有好幾種,我們選擇XML
其他不用改動,這時候加載就不會出問題了。

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    auto map = CCTMXTiledMap::create("map.tmx");
    map->setAnchorPoint(ccp(0.5f, 0.5f));
    map->setPosition(ccp(winSize.width / 2.0f, winSize.height / 2.0f));
    addChild(map);

其實CCTMXTiledMap這個類名以及告訴我們該如何選擇這個配置了。
另外下面兩行是用來設置地圖居中顯示:
map->setAnchorPoint(ccp(0.5f, 0.5f));
map->setPosition(ccp(winSize.width / 2.0f, winSize.height / 2.0f));

3 獲取窗口大小和原點座標

visibleSize 是窗口尺寸,origin 是窗口原點座標

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

4 獲取屏幕尺寸

    int width = GetSystemMetrics(SM_CXSCREEN);
    int height = GetSystemMetrics(SM_CYSCREEN);

5 場景切換

goScene爲新場景類:

Director::getInstance()->replaceScene(goScene::createScene());

6 新場景初始文件.c和.h

MyScene.h

#ifndef __MyScene_H__
#define __MyScene_H__

#include "cocos2d.h"

class MyScene : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // implement the "static create()" method manually
    CREATE_FUNC(MyScene);
};

#endif // __MyScene_H__

MyScene.c

#include "MyScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* MyScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = MyScene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MyScene::init()
{

    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    auto rootNode = CSLoader::createNode("MyScene.csb");

    addChild(rootNode);

    return true;
}

在使用這段代碼的時候,可以通過替換把MyScene替換成你想要的類名。

7 同級場景切換

MyScene是你接下來想進入同級場景(這個地方不是很肯定,懂的人給指點下):

Director::getInstance()->replaceScene(MyScene::createScene());

8 進入子場景

MyScene是你接下來想進入的子場景

    auto scene = MyScene::createScene();
    // run
    director->runWithScene(scene);

9 設置屏幕分辨率與插入控件尺寸一樣

該操作是用來解決插入的內容不能將屏幕鋪滿的問題。
下面兩行代碼放在新場景init()函數中。

    auto director = Director::getInstance();
    director->getOpenGLView()->setDesignResolutionSize(352, 352, ResolutionPolicy::SHOW_ALL);

下面是設置之後,與沒設置直接的區別
這裏寫圖片描述
這裏寫圖片描述

10 加入Cocos Studio創建的場景

MainScene.csb爲Cocos Studio創建的場景

    auto rootNode = CSLoader::createNode("MainScene.csb");
    addChild(rootNode,0);

11 獲取Cocos Studio創建的場景*.csb中的變量

比如我們在場景中加入了一個Button的變量,下面是獲取控件的辦法,並且設置控件響應事件。

    auto startButton = (ui::Button*)rootNode->getChildByName("Button");
    startButton->addTouchEventListener(CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));

12 CC_CALLBACK_1回調函數

函數參數一定要注意:一定有Ref * pSender

void HelloWorld::menuStartCallback(Ref* pSender)
{
    // To Add Your Code

}

到此還沒有完,你還必須在類裏面添加函數聲明,這個大家都知道,不用廢話。

13 動態添加控件

就拿一個Menu來舉個例子,下面代碼是添加了兩個Menu,分別是closeItem和startItem。

    auto closeItem = MenuItemFont::create("Exit", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    auto startItem = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::menuStartCallback, this));

    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,
        origin.y + closeItem->getContentSize().height / 2));
    startItem->setPosition(Vec2(origin.x + visibleSize.width / 2 - startItem->getContentSize().width / 2,
        origin.y + visibleSize.height/4 + startItem->getContentSize().height / 2));
    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    menu = Menu::create(startItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

至於其他控件如何添加也無非這幾步,(1)創建控件;(2)設置位置;(3)關聯(有的控件不需要關聯);(4)插入

14 靜態添加控件

主要看第11個如何獲取場景中的控件,靜態添加就是在場景中添加完成,並且已經設置完位置,我們只需要讓其與回調函數關聯起來,看12如何用寫回調函數。


接觸Cocos兩三天,瞎學,請大家在下面多多留言提問,指點,互相學習與指導!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章