官术网_书友最值得收藏!

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.

  1. In Xcode, select the Classes folder and then go to File | New | File and navigate to iOS | Source select C++ File.
  2. Call it GameSprite and make sure the Also create a header file option is selected.
  3. 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.

主站蜘蛛池模板: 广安市| 丹巴县| 呼玛县| 民乐县| 丹巴县| 定陶县| 孝感市| 三亚市| 深泽县| 富锦市| 靖江市| 普宁市| 泸水县| 双辽市| 福海县| 鹿邑县| 云林县| 宜阳县| 邯郸市| 乐陵市| 肥城市| 莆田市| 桐柏县| 乐清市| 基隆市| 邵阳县| 庆安县| 巫溪县| 乌兰县| 珲春市| 咸丰县| 河西区| 巴里| 枞阳县| 门源| 南通市| 娱乐| 晋中市| 安龙县| 江西省| 湖口县|