- Cocos2d-x by Example:Beginner's Guide(Second Edition)
- Roger Engelbert
- 279字
- 2021-07-23 20:00:28
Time for action – adding GameSprite.cpp
From here on, we'll create any new classes inside Xcode, but you could do it just as easily in Eclipse if you remember to update the Make
file. I'll show you how to do that later in this chapter.
- In Xcode, select the
Classes
folder and then go to File | New | File and navigate to iOS | Source select C++ File. - Call it
GameSprite
and make sure the Also create a header file option is selected. - Select the new
GameSprite.h
interface file and replace the code there with this:#ifndef __GAMESPRITE_H__ #define __GAMESPRITE_H__ #include "cocos2d.h" using namespace cocos2d; class GameSprite : public Sprite { public: CC_SYNTHESIZE(Vec2, _nextPosition, NextPosition); CC_SYNTHESIZE(Vec2, _vector, Vector); CC_SYNTHESIZE(Touch*, _touch, Touch); GameSprite(); virtual ~GameSprite(); static GameSprite* gameSpriteWithFile(const char* pszFileName); virtual void setPosition(const Vec2& pos) override; float radius(); }; #endif // __GAMESPRITE_H__
What just happened?
In the interface, we declare the class to be a subclass of the public Sprite
class.
Then we add three synthesized properties. In Cocos2d-x, these are macros to create getters and setters. You declare the type, the protected variable name, and the words that will be appended to the get
and set
methods. So in the first CC_SYNTHESIZE
method, the getNextPosition
and setNextPosition
method will be created to deal with the Point
value inside the _nextPosition
protected variable.
We also add the constructor and destructor for our class, and the ubiquitous static method for instantiation. This receives as a parameter, the image filename used by the sprite. We finish off by overriding setPosition
from Sprite
and adding the declaration for our helper method radius.
The next step then is to implement our new class.
- Cocos2D-X權威指南(第2版)
- 移動UI設計(微課版)
- INSTANT FreeMarker Starter
- Java應用開發與實踐
- Mastering Ubuntu Server
- Python 3破冰人工智能:從入門到實戰
- Python機器學習:手把手教你掌握150個精彩案例(微課視頻版)
- 零基礎學C語言第2版
- Python Essentials
- Python全棧開發:基礎入門
- 信息學奧林匹克競賽初賽精講精練
- Learning Node.js for Mobile Application Development
- Programming MapReduce with Scalding
- TensorFlow+Keras深度學習算法原理與編程實戰
- Fast Data Processing with Spark 2(Third Edition)