我写了一个bullet类,生成一个带PhysicsBody节点,然后每帧检测这个节点速度是否低于-10,如果低于-10则被释放掉。但是调用self:getPhysicsBody()时发生错误(见下文),然后,我尝试在setPhysicsBody后立即调用self:getPhysicsBody()发现是没有问题。这个是不是cocos2dx本身在lua的问题呢?因为程序有c++版本,我在c++版本中使用相同的方法是没有问题的。
错误log:
stack traceback:
: in function ‘getPhysicsBody’
:31: in function <:27>
(tail call): ?
LUA ERROR: :31: invalid ‘cobj’ in function ‘lua_cocos2dx_Node_getPhysicsBody’
代码:
localBullet=class(“Bullet”,function()
returncc.Node:create()
end)
functionBullet:ctor()
self.m_iHP=1
self.m_spBullet={}
self.m_iBulletType=0
self.m_iColorTag=0
self.m_iRunningTime=0
self.onEnter(self)
end
functionBullet:onEnter()
localscheduler=require"src.framework.scheduler"
scheduler.scheduleUpdateGlobal(handler(self,self.heartBeat))
end
–@function heartBeat
–@param dt number
functionBullet:heartBeat(dt)
ifself:getPhysicsBody():getVelocity().y<-10then
self:removeFromParent()
end
end
–@function reloading
–@param name string
functionBullet:reloading(name)
print(“Bullet:reloading”…name)
localsp=cc.Sprite:createWithSpriteFrameName(name)
self.m_spBullet=sp
self:addChild(sp)
sp:setTag(BULLET_NORMAL_TAG)
localbody=cc.PhysicsBody:createCircle(sp:getContentSize().width/2)
body:setTag(BULLET_NORMAL_TAG)
body:setCategoryBitmask(0x01)
body:setCollisionBitmask(0xFE)
body:setContactTestBitmask(0xFE)
self:setPhysicsBody(body)
self:setTag(BULLET_NORMAL_TAG)
table.insert(g_tbPlayingData.tbBulletsTable,self)
self.m_iBulletType=g_tbPlayingData.iBulletType
end