3.2ajax回调函数数问题,一直没搞懂,求助

Cocos2d-x(19)
CCNotificationCenter.h
/** @brief Adds an observer for the specified target.
@param target The target which wants to observe notification events.
@param selector The callback function which will be invoked when the specified notification event was posted.
@param name The name of this notification.
@param obj The extra parameter which will be passed to the callback function.
void addObserver(CCObject *target,
SEL_CallFuncO selector,
const char *name,
CCObject *obj);
/** @brief Posts one notification event by name.
@param name The name of this notification.
@param object The extra parameter.
void postNotification(const char *name, CCObject *object);
addObserver方法和postNotification方法都能附加一个object对象,当两个都传递object不是同一个对象且都不为空的时候回调方法获得CCObject对象是哪个对象呢?
答案是回调方法不会执行!
CCNotificationCenter.cpp
// observer functions
void CCNotificationCenter::addObserver(CCObject *target,
SEL_CallFuncO selector,
const char *name,
CCObject *obj)
if (this-&observerExisted(target, name))
CCNotificationObserver *observer = new CCNotificationObserver(target, selector, name, obj);
if (!observer)
observer-&autorelease();
m_observers-&addObject(observer);
void CCNotificationCenter::postNotification(const char *name, CCObject *object)
    CCArray* ObserversCopy = CCArray::createWithCapacity(m_observers-&count());
    ObserversCopy-&addObjectsFromArray(m_observers);
    CCObject* obj = NULL;
    CCARRAY_FOREACH(ObserversCopy, obj)
    {
        CCNotificationObserver* observer = (CCNotificationObserver*)
        if (!observer)
           
        
        if (!strcmp(name,observer-&getName()) && (observer-&getObject() == object || observer-&getObject() == NULL || object == NULL))
        {
            if (0 != observer-&getHandler())
            {
                CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()-&getScriptEngine();
                engine-&executeNotificationEvent(this, name);
            }
            else
            {
//如果传递的不是同一个对象且都不为空的时候执行不到这个方法
                observer-&performSelector(object);
            }
        }
    }
CCNotificationObserver.cpp
CCNotificationObserver::CCNotificationObserver(CCObject *target,
                                               SEL_CallFuncO selector,
                                               const char *name,
                                               CCObject *obj)
    m_target =
    m_selector =
    m_object =
    
    m_name = new char[strlen(name)+1];
    memset(m_name,0,strlen(name)+1);
    
    string orig (name);
    orig.copy(m_name,strlen(name),0);
//不注册的时候初始为0
    m_nHandler = 0;
void CCNotificationObserver::performSelector(CCObject *obj)
    if (m_target)
    {
        if (obj) {
            (m_target-&*m_selector)(obj);
        } else {
            (m_target-&*m_selector)(m_object);
        }
    }
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:115053次
积分:2623
积分:2623
排名:第8293名
原创:144篇
转载:21篇
评论:54条
(4)(4)(1)(1)(2)(1)(4)(1)(2)(1)(3)(5)(1)(4)(3)(1)(1)(1)(2)(4)(12)(11)(13)(1)(13)(12)(57)C程序 统计函数f被调用次数 填空 1,2,3,求助,急求_百度知道
C程序 统计函数f被调用次数 填空 1,2,3,求助,急求
counter+=1;stdio.h&i++) sum
f();sum=%d\ }int f(){ (
) int sum=0;n&void main(){ (
f(),sum);; for(i=0; printf(&100#include &lt
提问者采纳
=3. static int而且这个代码还是错的
提问者评价
来自团队:
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Cocos2d-x 3.0中 物理碰撞检测中onContactBegin回调函数不响应问题
好吧,其实这篇也是临时冒出来的,最近朋友要做个物理游戏,以前做物理还是用box2d,呃,确实要花些功夫才能搞懂其中的精髓,但是听讲这套引擎重新封装了一次,要容易很多,所以就简单尝试了一下,感觉确实要简单不少,不过在这其中还是遇到了些问题,首先就来说说onContactBegin这个回调函数响应问题。
先说说情况,简单做了一个打砖块的游戏,前面一切都很顺利,只是做到碰撞检测的时候,发现回调函数弄死都不调用。开始我以为函数写错了,后来查了api,testCpp都没有错,在3.0的api中,没有关于PhysicsBody,PhysicsWorld这些类的说明,所以大家想查移步到3.2的api中吧。
onContactBegin函数的参数就一个,
bool onContactBegin(PhysicsContact& contact),这和3.0beta版本又有些不同,以前是2个,所以在添加事件监听的时候,不要写错,like this,
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
回调函数是在当两个物体有接触的时候就会响应一次,里面的参数从名字中也能看出来,Contact接触,自然会涉及到两个物体。
好了,下面该到重点了,为啥这个onContactBegin函数不响应呢?问题出在三个掩码值上,这里推荐一下看下这篇博文。
或者打开引擎的源代码,可以在CCPhysicsShape这个头文件里看下这段代码,
* A mask that defines which categories this physics body belongs to.
* Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions.
* The default value is 0xFFFFFFFF (all bits set).
inline void setCategoryBitmask(int bitmask) { _categoryBitmask = }
inline int getCategoryBitmask() const { return _categoryB }
* A mask that defines which categories of bodies cause intersection notifications with this physics body.
* When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in.
* The default value is 0x (all bits cleared).
inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = }
inline int getContactTestBitmask() const { return _contactTestB }
  /**
* A mask that defines which categories of physics bodies can collide with this physics body.
* When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a non-zero value, then this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.
* The default value is 0xFFFFFFFF (all bits set).
inline void setCollisionBitmask(int bitmask) { _collisionBitmask = }
inline int getCollisionBitmask() const { return _collisionB }
说白了,两个物体间,能不能碰撞,能不能发送接触事件信息,关键就看这个三个参数值。
总结来说:
一个body的CategoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。
一个body的CategoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,会碰撞,否则不碰撞。
这三个参数都有自己的默认值,采用16位表示,
CategoryBitmask,
默认值为 0xFFFFFFFF
ContactTestBitmask,
默认值为 0x
CollisionBitmask,
默认值为 0xFFFFFFFF
大家可以简单的算一下,如果对这个计算不了解,可以查查看哈,或者掏出你电脑上的计算器也可以哇。
按照前面的总结来说,如果我们创建的body都采用默认值的话,那么
CategoryBitmask & ContactTestBitmask = 0
CategoryBitmask & CollisionBitmask = -1
这样看来,情况就清楚了,如果采用默认的数值,碰撞是可以检测的,但是碰撞事件是不会发出的,so我们的onContactBegin就被屏蔽了,那么当然不会做事情。
所以如果想我们的两个物体即发生碰撞又可以检测到,那么很简单,不让它们&的值不等于0就ok了,所以可以将两个需要碰撞的物体的这个三个掩码值都设置成1,
m_ball->getPhysicsBody()->setCategoryBitmask(0x01);
m_ball->getPhysicsBody()->setContactTestBitmask(0x01);
m_ball->getPhysicsBody()->setCollisionBitmask(0x01);block->getPhysicsBody()->setCategoryBitmask(0x01);
block->getPhysicsBody()->setContactTestBitmask(0x01);
block->getPhysicsBody()->setCollisionBitmask(0x01);
这样它们之间怎么按位与的结果都是1,就可以有相应了。
bool HelloWorld::onContactBegin(PhysicsContact& contact)
auto sp1 = (Sprite*)contact.getShapeA()->getBody()->getNode();
auto sp2 = (Sprite*)contact.getShapeB()->getBody()->getNode();
if (sp1->getTag() == 1)
sp1->removeFromParentAndCleanup(true);
if (sp2->getTag() == 1)
sp2->removeFromParentAndCleanup(true);
前面说了接触是肯定有两个物体的,所以打砖块的逻辑在这里就是判断下,其中一个的tag,如果是我们的砖块,说明碰到了,那么我们移除就ok了。
效果就是这样了。}

我要回帖

更多关于 回调函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信