Cocos2dx 3. X to create a Layer

Source: Internet
Author: User


1. Create a class file. Note that the directory is under classes, otherwise the file cannot be found normally.

2. Modify the pre-compilation header. If vs is used, the default value is # pragma once. For compatibility, change to # ifndef | # define | # endif

3. Add the cocos2d. h header file.

4. inherit from cocos2d: layer. The default value is private inheritance. It must be changed to public inheritance.

5. Add Init () and create_func ()

6. Implement Init ()

7. Add createscene (). Optional.

The most basic code:

//GameLayer.h#ifndef _GAMELAYER_H#define _GAMELAYER_H#include "cocos2d.h"class GameLayer : public cocos2d::Layer{public:GameLayer();~GameLayer();public:virtual bool init();CREATE_FUNC(GameLayer);public: static cocos2d::Scene* createScene();};#endif // _GAMELAYER_H
// GameLayer.cpp#include "GameLayer.h"USING_NS_CC;// 构造函数,如有对象成员,需要在此初始化GameLayer::GameLayer(){}// 析构GameLayer::~GameLayer(){}//虚函数,初始化,当执行本类的create()函数时会被自动调用bool GameLayer::init(){bool bl = Layer::init();// 加入内容return bl;}// Scene的入口,通过该函数进入LayerScene* GameLayer::createScene(){auto scene = Scene::create();auto layer = GameLayer::create();scene->addChild(layer);return scene;}
// 直接进入auto layer = GameLayer::createScene();Director::getInstance()->replaceScene(layer);

// 特效进入auto layer = GameLayer::createScene();auto slidein = TransitionSlideInL::create(1.0f, layer);Director::getInstance()->replaceScene(slidein);

// 进入特效完之后才发生//声明虚函数virtual void onEnterTransitionDidFinish();
 Add Timer  
// GameLayer.hprivate:// 声明void gameStep(float dt);
// GamaLayer.cpp// 实现void GameLayer::gameStep(float dt){//bg->move();}void GameLayer::startGame(){// 调用this->schedule(SEL_SCHEDULE(&GameLayer::gameStep), 0.02f);}

Cocos2dx 3. X to create a Layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.