Home Database Mysql Tutorial Cocos2d-x学习笔记(3)

Cocos2d-x学习笔记(3)

Jun 07, 2016 pm 03:01 PM
study notes

Cocos2d-x有一个包含所有其他头文件的cocos2d.h,只要在使用时包含这个头文件,就可以使用引擎的全部功能。Cocos2d-x的类都放置于cocos2d的命名空间下,如引擎下的“actions/CCAction.h”中在文件首尾使用NC_CC_BEGIN和NS_CC_END来将所有类型包含在cocos2d命

        Cocos2d-x有一个包含所有其他头文件的cocos2d.h,只要在使用时包含这个头文件,就可以使用引擎的全部功能。Cocos2d-x的类都放置于cocos2d的命名空间下,如引擎下的“actions/CCAction.h”中在文件首尾使用NC_CC_BEGIN和NS_CC_END来将所有类型包含在cocos2d命名空间下。在游戏中使用#define USING_NS_CC using namespace cocos2d定义的宏USING_NS_CC来说明命名空间。

        1、构造函数与初始化

        Cocos2d-x不使用传统的值类型,所有的对象都创建在堆上,然后通过指针引用,创建Cocos2d-x对象主要有两种方法:(1)使用new创造未初始化的对象,然后调用init系列方法来初始化。(2)使用静态的工厂方法直接创建一个对象。

        第一种方法:

       (1)使用new操作符调用构造函数,创建一个没有初始化的空对象

       (2)选择合适的初始化方法,并调用它来初始化对象

        Cocos2d-x的初始化方法都以init()作为前缀,返回一个bool值,代表是否成功。例如:

        CCSprite* sprite1 = new CCSprite();

        sprite1->initWithFile(“HelloWorld.png”);

        CCSprite共提供8个初始化方法。

       第二种方法

       (1)静态工程方法是类提供的静态函数,只要提供必要的参数,就会返回一个完成了初始化的对象,通常init系列的初始化方法都会有对应的工厂方法。例如:

        CCSprite* sprite2 = CCSprite::spriteWithFile(“HelloWorld.png”);//Cocos2d-x 2.0以前版本中的方法

        CCSprite* sprite3 = CCSprite::create(“HelloWorld.png”);///Cocos2d-x 2.0之后版本中的方法

        2、选择器

        选择器是类似于C++中的类函数指针的机制,下面是Cocos2d-x提供的创建选择器语法的宏,用来创建函数指针,这些宏只有一个参数SELECTOR,表示被指向的类方法

        schedule_selector(SELECTOR)

        callfunc_selector(SELECTOR)

        callfuncN_selector(SELECTOR)

        callfuncND_selector(SELECTOR)

        menu_selector(SELECTOR)

        event_selector(SELECTOR)

        compare_selector(SELECTOR)

        3、属性:Cocos2d-x规定了属性访问器的方法名称以get或set为前缀,后接属性名,如CCNode中节点标记属性Tag属性,访问器分别为getTag()和setTag(int aTag)其原理如下:      

int tag;
int getTag() {return tag;}
void setTag() {tag = aTag;}
Copy after login
        Cocos2d-x中与属性相关的宏共有9个,只需要把宏写在类的定义中即可,每个宏有3个参数:(1)varType,属性类型,如果属性类型时对象,需要写成指针的形式。(2)varName,属性的私有字段名称。(3)funName,属性的访问器名称,也就是紧接在get或set后面的部分。如:CC_SYNTHESIZE(int,tag,Tag)

        

Cocos2d-x与属性有关的宏
描述
CC_PROPERTY 定义一个属性及其访问器,没有实现,常用于简单的值类型
CC_PROPERTY_READONLY 定义一个属性,只包含get访问器,没有实现
CC_PROPERTY_PASS_BY_REF 定义一个属性,访问器使用引用类型传递参数,没有实现,通常用于结构体
CC_PROPERTY_READONLY_PASS_BY_REF 定义一个属性,只包含get访问器,且使用引用类型传递参数,没有实现。
CC_SYNTHESIZE 同CC_PROPERTY,实现了访问器方法
CC_SYNTHESIZE_READONLY 同CC_PROPERTY_READONLY,实现了访问器方法
CC_SYNTHESIZE_READONLY_PASS_BY_REF 同CC_PROPERTY_READONLY_PASS_BY_REF,实现了访问器方法
CC_SYNTHESIZE_PASS_BY_REF 同CC_PROPERTY_PASS_BY_REF,实现了访问器方法
CC_SYNTHESIZE_RETAIN 同CC_PROPERTY,实现了访问器方法。用于派生自CCObject的类型,
访问器采用Cocos2d-x的内存管理机制自动维护对象的引用计数。

        4、单例:Cocos2d-x的流程控制器CCDirector是一个独一无二的控制器,用于切换游戏的场景。这种情况下使用单例的技巧。如下代码:

static CCDisplayLinkDirector s_SharedDirector;
CCDirector* CCDirector::sharedDirector(void)
{
    static bool s_bFirstUseDirector = true;
    if(s_bFirstUseDirector)
    {
        s_bFirstUseDirector = false;
        s_bFirstUseDirector.init();
    }
    return &s_SharedDirector;
}
Copy after login
可以放心,CCDirector维护了一个静态的CCDirector实例,在第一次使用前初始化,为了访问CCDirector控制器,使用如下代码:
CCDirector::sharedDirector()->replaceScene(newScene);
Copy after login
获取CCDirector的唯一实例,调用replaceScene切换到新场景。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Learn to completely uninstall pip and use Python more efficiently Learn to completely uninstall pip and use Python more efficiently Jan 16, 2024 am 09:01 AM

No more need for pip? Come and learn how to uninstall pip effectively! Introduction: pip is one of Python's package management tools, which can easily install, upgrade and uninstall Python packages. However, sometimes we may need to uninstall pip, perhaps because we wish to use another package management tool, or because we need to completely clear the Python environment. This article will explain how to uninstall pip efficiently and provide specific code examples. 1. How to uninstall pip The following will introduce two common methods of uninstalling pip.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

A deep dive into matplotlib's colormap A deep dive into matplotlib's colormap Jan 09, 2024 pm 03:51 PM

To learn more about the matplotlib color table, you need specific code examples 1. Introduction matplotlib is a powerful Python drawing library. It provides a rich set of drawing functions and tools that can be used to create various types of charts. The colormap (colormap) is an important concept in matplotlib, which determines the color scheme of the chart. In-depth study of the matplotlib color table will help us better master the drawing functions of matplotlib and make drawings more convenient.

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

See all articles