cocos3 封装一个ball

第一次写

 

#pragma once
#include "cocos2d.h"

USING_NS_CC;

class Ball:public Sprite
{
public:
    virtual bool init();
    static Ball* create()
    {
        Ball *b=new Ball();
        b->init();
        b->autorelease();
        return b;
    }
};

 

#include "Ball.h"

bool Ball::init()
{
    if ( !Sprite::init() )
    {
        return false;
    }
    initWithFile("ball.png");
    return true;
}

查看create_func的函数定义

#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
    __TYPE__ *pRet = new __TYPE__(); \
    if (pRet && pRet->init()) \
    { \
        pRet->autorelease(); \
        return pRet; \
    } \
    else \
    { \
        delete pRet; \
        pRet = NULL; \
        return NULL; \
    } \
}

 

可以看出来 create_func 其实是一个static create()方法。

 

——TYPE———为什么传递当前类进去也一幕了然。

 

这样我们就拥有了一个create方法,这也是我以前重载的时候不成功的原因。

 

posted @ 2014-12-02 13:21  yufenghou  阅读(157)  评论(0编辑  收藏  举报