- Cocos2d-x by Example:Beginner's Guide(Second Edition)
- Roger Engelbert
- 592字
- 2021-07-23 20:00:25
Time for action – creating the interface
The interface, or header file, is just a text file with the .h
extension.
- Create a new text file and save it as
HelloWorld.h
. Then, enter the following lines at the top:#ifndef __HELLOWORLD_H__ #define __HELLOWORLD_H__ #include "cocos2d.h"
- Next, add the namespace declaration:
using namespace cocos2d;
- Then, declare your class name and the name of any inherited classes:
class HelloWorld : public cocos2d::Layer {
- Next, we add the properties and methods:
protected: int _score; public: HelloWorld(); virtual ~HelloWorld(); virtual bool init(); static cocos2d::Scene* scene(); CREATE_FUNC(HelloWorld); void update(float dt); inline int addTwoIntegers (int one, int two) { return one + two; } };
- We finish by closing the
#ifndef
statement:#endif // __HELLOWORLD_H__
What just happened?
You created a header file in C++. Let's go over the important bits of information:
- In C++ you include, you do not import. The
import
statement in Objective-C checks whether something needs to be included;include
does not. But we accomplish the same thing through that clever use of definitions at the top. There are other ways to run the same check (with#pragma once
, for instance) but this one is added to any new C++ files you create in Xcode. - You can make your life easier by declaring the namespaces you'll use in the class. These are similar to packages in some languages. You may have noticed that all the uses of
cocos2d::
in the code are not necessary because of the namespace declaration. But I wanted to show you the bit you can get rid of by adding a namespace declaration. - So next you give your class a name and you may choose to inherit from some other class. In C++ you can have as many super classes as you want. And you must declare whether your super class is public or not.
- You declare your
public
,protected
andprivate
methods and members between the curly braces.HelloWorld
is the constructor and~HelloWorld
is the destructor (it will do whatdealloc
does in Objective-C). - The
virtual
keyword is related to overrides. When you mark a method asvirtual,
you are telling the compiler not to set in stone the owner of the method, but to keep it in memory as execution will reveal the obvious owner. Otherwise, the compiler may erroneously decide that a method belongs to the super and not its inheriting class.Also, it's good practice to make all your destructors
virtual
. You only need use the keyword once in the super class to mark potential overrides, but it is common practice to repeat thevirtual
keyword in all subclasses so developers know which methods are overrides (C++11 adds a tagoverride,
which makes this distinction even clearer, and you will see examples of it in this book's code). In this case,init
comes fromLayer
andHelloWorld
wants to override it.virtual bool init();
- Oh yes, in C++ you must declare overrides in your interfaces. No exceptions!
The inline
method is something new to you, probably. These methods are added to the code by the compiler wherever they are called for. So every time I make a call to addTwoIntegers
, the compiler will replace it with the lines for the method declared in the interface. So the inline
method works just as statements inside a method; they do not require their own bit of memory in the stack. But if you have a two-line inline
method called 50 times in your program, it means that the compiler will add a hundred lines to your code.
- JavaScript 從入門到項目實踐(超值版)
- 工程軟件開發(fā)技術(shù)基礎(chǔ)
- PHP基礎(chǔ)案例教程
- Offer來了:Java面試核心知識點精講(原理篇)
- 云原生Spring實戰(zhàn)
- JavaScript前端開發(fā)與實例教程(微課視頻版)
- 高級C/C++編譯技術(shù)(典藏版)
- Julia Cookbook
- Data Analysis with Stata
- Kotlin從基礎(chǔ)到實戰(zhàn)
- Visual C++開發(fā)入行真功夫
- Procedural Content Generation for C++ Game Development
- Java圖像處理:基于OpenCV與JVM
- 零基礎(chǔ)學(xué)C語言(第4版)
- 青少年學(xué)Python(第2冊)