以下是CCNode::init()和CCNode::onEnter()之间的区别的主题。然而,我遵循了他们给出的建议。
void MyLayer::onEnter() {
CCLayer::onEnter();
//your code goes here
}我收到了Assertion failed!错误!
MyLayer代码:
class MyLayer : public CCLayerColor我应该在我的CCLayerColor::onEnter()代码中添加MyLayer::onEnter()吗?CCLayer::init()和CCLayer::onEnter()的区别是什么?我应该将哪一部分代码放入init()中,哪些部分应该放入onEnter()
发布于 2013-12-25 13:08:47
Cocos2d-x有它的内存分配模型作为一个两步过程,就像目标-c。每个对象都分配了内存(通常使用"create“方法),然后初始化其状态(通常使用名为”init“的方法)。因此,在create/init方法中,您分配内存并执行运行它所需的任何对象初始化。
当对象开始放到显示器上,或者当它被添加到另一个容器时,它的"onEnter“方法就会被调用。当框架本身显示一个CCScene/CCLayer (其中任何一个可能包含您的CCNode派生对象)时,就会调用它。
内存分配和对象创建至少有两种模式,我倾向于遵循这样的模式:类包含静态工厂方法和私有构造函数,因此必须通过工厂创建对象,而不能自己创建对象。
例如,我目前正在处理这个“按钮”类:
class ActionButton;
class ActionButtonTarget
{
public:
virtual void ActionButtonActivated(ActionButton* button) = 0;
};
class ActionButton : public CCNode, public CCTargetedTouchDelegate
{
private:
ActionButton();
CCNode* _node; // Weak Reference
CCRect _testRect;
ActionButtonTarget* _target;
bool init(ActionButtonTarget* target, CCNode* node, CCRect rect);
bool IsTouchInside(CCTouch* touch);
void NotifyTarget();
protected:
virtual CCAction* CreateAction();
public:
virtual ~ActionButton();
// The class registers/unregisters on entry
// or exit of the layer. This
virtual void onEnterTransitionDidFinish();
virtual void onExitTransitionDidStart();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
static ActionButton* create(ActionButtonTarget* target, CCNode* node, CCRect rect);
};请注意,“创建”方法是创建它的唯一方法。在这种情况下,它为按钮的参数取参数。其他CCNode派生对象(例如CCScene)通常不会。
内部:
ActionButton::ActionButton()
{
}
ActionButton::~ActionButton()
{
}
// The class registers/unregisters on entry
// or exit of the layer. This
void ActionButton::onEnterTransitionDidFinish()
{
CCNode::onEnterTransitionDidFinish();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
void ActionButton::onExitTransitionDidStart()
{
CCNode::onExitTransitionDidStart();
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
bool ActionButton::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if(IsTouchInside(pTouch))
{
return true;
}
return false;
}
void ActionButton::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
// Nothing to do here.
}
void ActionButton::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
NotifyTarget();
}
bool ActionButton::IsTouchInside(CCTouch* touch)
{
CCPoint point = touch->getLocationInView();
point = CCDirector::sharedDirector()->convertToGL(point);
return _testRect.containsPoint(point);
}
void ActionButton::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
_target->ActionButtonActivated(this);
}
ActionButton* ActionButton::create(ActionButtonTarget* target, CCNode* node, CCRect rect)
{
ActionButton *pRet = new ActionButton();
if (pRet && pRet->init(target,node,rect))
{
pRet->autorelease();
return pRet;
}
else
{
CC_SAFE_DELETE(pRet);
return NULL;
}
}
CCAction* ActionButton::CreateAction()
{
return NULL;
}
void ActionButton::NotifyTarget()
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(Constants::SOUND_EFFECT_BUTTON_CLICK());
_target->ActionButtonActivated(this);
}
bool ActionButton::init(ActionButtonTarget* target, CCNode* node, CCRect rect)
{
assert(target != NULL);
assert(node != NULL);
assert(dynamic_cast<ActionButtonTarget*>(target) != NULL);
_target = target;
_node = node;
_testRect = rect;
addChild(_node);
return true;
}注意,OnEnterXXX和onExitXXX方法调用父类方法。--您必须这样做--,否则它不会像预期的那样工作。
这个类是一个按钮,它将在节点上运行一个操作(可能会使其增长/收缩,以表示它已被按下)。它需要用户的触摸。我不能在它进入现场之前将它添加到触摸管理器中。因此,我使用onEnterTransitionDidFinish方法添加它,也使用onExitTransitionDidStart删除它,这样它就不会在场景被移除后继续接收触摸。我不知道容器是否会被销毁,所以当按钮退出显示时,我必须移除它。
这对我们有帮助吗?
发布于 2013-12-25 11:12:09
第一部份
查看基类的OnEnter方法(CCLayer),如果发生了一些严重的事情,那么您应该调用它。
IInd部件
就像这个话题..。在初始化过程中,OnEnter在"viewDidAppear“和init时被调用。
假设您通过以下方式创建了层
MyLayer *newLayer=MyLayer::create(); //...init is called before OnEnter..然后你把它添加到某个场景中
X->addChild(newLayer); // ...*Now onEnter Method is called*X必须添加到某个场景或家长中,等等。
IIIrd部件
在某些情况下,您只需创建存储在数组中的多个对象,而不添加它们。当对象进入/添加到屏幕时,您正在等待添加them.This的正确时间,您希望它执行特定的任务。
例如。在一个游戏中,你必须攻击某人,并且有30 troops.All,部队是在装载场景中创建的,但他们都没有增加层。当战斗开始时,按用户部署的部队出现在screen.Troop上,在enter上有一个任务要去目标建筑,该方法可以通过onEnter调用。
发布于 2013-12-26 06:42:28
如果您想在屏幕上显示某件事情时,就调用onEnter()。即使在屏幕上没有层,init()方法也将被调用。就像这样:
void FirstScene::methodInit()
{
customlayer = new CustomLayer();
customlayer -> init();
customlayer-> retain();
// At this point,customlayer (which is a CustomLayer object that is a subclass of CCLayer)
// is still not on the screen, hence, CustomLayer::onEnter() is still not called
}
void FirstScene::methodEnter
{
this -> addChild( customLayer, customIndex, customTag );
customLayer -> release();
// At this point, CustomLayer::onEnter() is called, because customLayer is being rendered
}https://stackoverflow.com/questions/20769747
复制相似问题