有关quick-cocos2dx中的class方法的笔记

烁灵 posted @ 2014年8月12日 19:35 in Cocos2dx with tags init class quick-cocos2dx ctor 构造方法 , 10605 阅读

因为之前师父告诉咱ctor相当于构造函数,不熟悉lua模拟面向对象细节的咱用的时候感觉怪怪的,所以这里试着总结下,慢慢写。

首先看下源码:(function.lua)

function class(classname, super)
    local superType = type(super)
    local cls

    if superType ~= "function" and superType ~= "table" then
        superType = nil
        super = nil
    end

    if superType == "function" or (super and super.__ctype == 1) then
        -- inherited from native C++ Object
        cls = {}

        if superType == "table" then
            -- copy fields from super
            for k,v in pairs(super) do cls[k] = v end
            cls.__create = super.__create
            cls.super    = super
        else
            cls.__create = super
            cls.ctor = function() end
        end

        cls.__cname = classname
        cls.__ctype = 1

        function cls.new(...)
            local instance = cls.__create(...)
            -- copy fields from class to native object
            for k,v in pairs(cls) do instance[k] = v end
            instance.class = cls
            instance:ctor(...)
            return instance
        end

    else
        -- inherited from Lua Object
        if super then
            cls = {}
            setmetatable(cls, {__index = super})
            cls.super = super
        else
            cls = {ctor = function() end}
        end

        cls.__cname = classname
        cls.__ctype = 2 -- lua
        cls.__index = cls

        function cls.new(...)
            local instance = setmetatable({}, cls)
            instance.class = cls
            instance:ctor(...)
            return instance
        end
    end

    return cls
end

对于使用传入function方式继承cocos2dx类的自定义类如MainScene:(MainScene.lua)

local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

function会成为__create变量的值,在new方法中调用,返回值为display.newScene("MainScene"),即:(display.lua)

function display.newScene(name)
    local scene = CCScene:create()
    scene:setNodeEventEnabled(true)
    scene:setAutoCleanupEnabled()
    scene.name = name or "<unknown-scene>"
    return scene
end

CCScene:create()返回的对象,该对象由cocos2dx的工厂方法create创建,由cocos2dx的内存管理机制管理。

之后调用该对象的ctor方法,在官方demo中MainScene的ctor为自己添加了CCLabelTTF控件,这个在cocos2dx中更像是init方法的行为,而不是构造函数……

比较一下:(CCScene.cpp)

CCScene *CCScene::create()
{
    CCScene *pRet = new CCScene();
    if (pRet && pRet->init())
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        CC_SAFE_DELETE(pRet);
        return NULL;
    }
}

而init方法做了控件等的初始化和附加等操作:(HelloWorld.cpp,注释略)

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
    pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));
    this->addChild(pLabel, 1);
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(pSprite, 0);
    
    return true;
}

c++中的new主要做了:

调用构造函数,调用init方法为scene\layer等添加控件,进入内存管理机制

lua中的此时的new主要做了:

调用原create方法进入内存管理机制,调用ctor方法为scene\layer等添加控件,同时可以用来初始化当前“对象”的“成员变量”

 

结论:

目前自己认为,这种场合ctor可以用来对当前“对象”的“成员变量”进行初始化,这个行为和构造函数相似;同时生成并附加给场景\层所需的控件,这个行为与init方法相似,同时做了两件事。需要自己注意的是ctor执行前,即初始化成员变量前,当前对象已经是(可以当做)由cocos2dx工厂方法create出来的参与了内存管理的对象了。

对于在cocos2dx中通过create来创建并使用的类/子类,在quick中可通过new来创建并使用。

Avatar_small
JK Board 10th Class 说:
2022年8月18日 13:07

JKBOSE 10th Question Paper 2023, Jammu And Kashmir conduct all secondary and higher secondary examinations all over the state. JK Board 10th Class Model Paper 2023 PDF All candidates who belong to Jammu and Kashmir Board can check all Educational activities details like Admission, Examination, New Question Paper, entry card, and result from JKBOSE official website. JKBOSE 10th Question Paper 2023, Jammu And Kashmir conduct all secondary and higher secondary examinations all over the state.

Avatar_small
Emma 说:
2023年1月22日 22:01

The class method in quick-cocos2dx can be a bit confusing to those of us who are not familiar with the details of lua simulation object-oriented programming. Thankfully, our CBD Oil for Emotional distress instructor was kind enough to explain that ctor is equivalent to a constructor. This can be useful to keep in mind when utilizing this function.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter