quick-cocos2dx 3.3 中如何侦听碰撞事件

quick-cocos2dx 3.3 中如何侦听碰撞事件,第1张

概述首先按照一般步骤创建物理世界,然后注册碰撞事件侦听器如下: local contactListener = cc.EventListenerPhysicsContact:create() contactListener:registerScriptHandler(function(contact) print("contactBegin", "-----------",

首先按照一般步骤创建物理世界,然后注册碰撞事件侦听器如下:

local contactListener  = cc.EventListenerPhysicsContact:create()    contactListener:registerScriptHandler(function(contact)        print("contactBegin","-----------",type(contact))        return true    end,cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)     contactListener:registerScriptHandler(function(contact,solve)        print("contactPresolve",type(contact),type(solve))        return true    end,cc.Handler.EVENT_PHYSICS_CONTACT_PRESolVE)     contactListener:registerScriptHandler(function(contact,solve)        print("contactpostsolve",type(solve))    end,cc.Handler.EVENT_PHYSICS_CONTACT_postsolVE)     contactListener:registerScriptHandler(function(contact)        print("contactSeperate",type(contact))    end,cc.Handler.EVENT_PHYSICS_CONTACT_SEPERATE)     self:getEventdispatcher():addEventListenerWithFixedPriority(contactListener,1)
随后在设置想要侦听碰撞事件的刚体类型。
 /**     * 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).     */    voID setcategoryBitmask(int bitmask);    /**      * 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 0x00000000 (all bits cleared).     */    voID setContactTestBitmask(int bitmask);    /**     * 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).     */    voID setCollisionBitmask(int bitmask);


categoryBitmask与ContactTestBitmask决定是否发送碰撞事件。

categoryBitmask与CollisionBitmask决定是否发生碰撞。

注意,默认是不发送碰撞事件,且均可发生碰撞的。

总结

以上是内存溢出为你收集整理的quick-cocos2dx 3.3 中如何侦听碰撞事件全部内容,希望文章能够帮你解决quick-cocos2dx 3.3 中如何侦听碰撞事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1038683.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存