自定义 CCNode runAction 无法在 Cocos2d-x 中执行

custom CCNode runAction cannot execute in Cocos2d-x?

本文关键字:Cocos2d-x 执行 CCNode runAction 自定义      更新时间:2023-10-16

我有一个继承自CCNode的类。HcharacterDrawnode包含一组StrokeDrawnode,这是另一个自定义CCNode。现在我将 (m_HDrawnode)HcharacterDrawnode 添加到一个层并运行操作。

CCAction* place = CCMoveTo::create(2.0,ccp(0,0));   
m_HDrawnode->runAction(place);

但什么也没发生。我已经检查了一些网页。有人说这可能与m_bRunning有关,但是我找不到设置此变量的地方。

HcharacterDrawnode.h

class HcharacterDrawnode : public CCNode
{
public:
    HcharacterDrawnode();
    ~HcharacterDrawnode();
    CREATE_FUNC(HcharacterDrawnode);
    virtual bool init();
    virtual void onEnter();
    virtual void onExit();
    virtual void draw();
    void addPoint(CCPoint point);
    void addStroke(Stroke s);
    void removeLastStroke();
    CC_SYNTHESIZE_RETAIN(CCArray*,strokeDrawlist,StrokeDrawnodeList);
private:
};

HcharacterDrawnode.cpp

#include "HcharacterDrawnode.h"
HcharacterDrawnode::HcharacterDrawnode():strokeDrawlist(NULL)
{
}
HcharacterDrawnode::~HcharacterDrawnode()
{
    CC_SAFE_RELEASE(strokeDrawlist);
}
void HcharacterDrawnode::onEnter(){
    CCNode::onEnter();
}
void HcharacterDrawnode::onExit(){
    CCNode::onExit();
}
bool HcharacterDrawnode::init(){
    this->setStrokeDrawnodeList(CCArray::create());
    return true;
}
void HcharacterDrawnode::draw(){
    CCObject* ob;
    CCARRAY_FOREACH(strokeDrawlist,ob){
        ((StrokeDrawnode*)(ob))->draw();
    }
}
void HcharacterDrawnode::addPoint(CCPoint point){
    StrokeDrawnode* t = (StrokeDrawnode*)(strokeDrawlist->objectAtIndex(strokeDrawlist->count()-1));
    t->addPoint(point);
}
void HcharacterDrawnode::addStroke(Stroke s){
    strokeDrawlist->addObject(StrokeDrawnode::create(s));
}
void HcharacterDrawnode::removeLastStroke(){
    strokeDrawlist->removeLastObject();
}

StrokeDrawnode.h

class StrokeDrawnode : public CCNode
{
public:
    StrokeDrawnode();
    StrokeDrawnode(Stroke stro);
    ~StrokeDrawnode();
    static StrokeDrawnode* create(Stroke stro);
    Stroke stroke;
    ccColor4B mcolor;
    virtual void onEnter();
    virtual void onExit();
    virtual void draw();
    int visibleIndex;
    void addPoint(CCPoint point);
private:
};

笔画节点.cpp

#include "StrokeDrawnode.h"
StrokeDrawnode::StrokeDrawnode()
{
}
StrokeDrawnode::StrokeDrawnode(Stroke stro){
    this->stroke = stro;
}
void StrokeDrawnode::onEnter(){
    CCNode::onEnter();
}
void StrokeDrawnode::onExit(){
    CCNode::onExit();
}
StrokeDrawnode* StrokeDrawnode::create(Stroke stro){
    StrokeDrawnode* pRet = new StrokeDrawnode(stro);
    if (pRet && pRet->init())
    {
        pRet->autorelease();
        return pRet;
    }else{
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}
StrokeDrawnode::~StrokeDrawnode()
{
}
void StrokeDrawnode::draw(){
    //CCLog("StrokeDrawnode::draw");
    glLineWidth(6.0f);                  
    ccDrawColor4F(0,0,0,1);             
//  glEnable(GL_LINE_SMOOTH);
    CCPoint pre = stroke.pointList[0];
    for (int i = 1; i< stroke.pointCount; i++)
    {
        ccDrawLine(pre,stroke.pointList[i]);
        pre = stroke.pointList[i];
    }
//  glDisable(GL_LINE_SMOOTH);
}
void StrokeDrawnode::addPoint(CCPoint point){
    this->stroke.addPoint(point);
}

你画了你的 StrokeNodes,但你忘了为 HcharacterDrawnode 调用 CCNode::d raw() 函数:

void HcharacterDrawnode::draw(){
    CCNode::draw();
    CCObject* ob;
    CCARRAY_FOREACH(strokeDrawlist,ob){
        ((StrokeDrawnode*)(ob))->draw();
    }
}

此外,如果你在你的类中覆盖 init(),你应该调用你的父级的 init:

bool HcharacterDrawnode::init(){
if(!CCNode::init())
    return false;
this->setStrokeDrawnodeList(CCArray::create());
return true;

}