Cocos2dx Box2D-b2body GetUserData始终返回null [英] Cocos2dx Box2D - b2body GetUserData always returns null
本文介绍了Cocos2dx Box2D-b2body GetUserData始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将其引用为链接,但与我相同。没有用。
汽车代码:-
sprintf(temp,CAR_BODY_CAR_PLIST) ;
m_Texture [2] = new TextureObject(temp,spritesheet1,1,true,kTexture2DPixelFormat_Default,2);
//添加购物车//
b2BodyDef bodyDef;
b2FixtureDef FixtureDef;
b2PolygonShape geometricShape;
b2CircleShape circleShape;
bodyDef.position.Set(startPos.x,startPos.y);
bodyDef.type = b2_dynamicBody;
** bodyDef.userData = m_Texture [2]; **
购物车= m_world-> CreateBody(& bodyDef);
夹具DEF。密度= 25.0f;
FixtureDef.friction = 0.3f;
FixtureDef.restitution = 0.1f;
FixtureDef.filter.categoryBits = 0x0008;
fixtureDef.filter.maskBits = 0x0003;
fixtureDef.density = 0.0f;
FixtureDef.isSensor = true;
b2Vec2点[6] =
{
b2Vec2(0.7f,0.0f),
b2Vec2(0.7f,3.25f),
b2Vec2 (-0.7f,3.25f),
b2Vec2(-0.7f,0.0f),
};
多边形Shape.Set(点,4);
m_RiderTest = cart-> CreateFixture(& fixtureDef);
(((GroundTestCallback *)m_GroundRayCastTest)-> addIgnoreFixture(m_RiderTest);
FixtureDef.isSensor = false;
FixtureDef.density = 25.0f;
FixtureDef.filter.categoryBits = 0x0010;
FixtureDef.filter.maskBits = 0x0007;
多边形Shape.SetAsBox(TEST_SCALE * 0.4f,TEST_SCALE * 0.15f,axel1Pos,axel1Angle);
(((TestCallback *)Test)-> addIgnoreFixture(cart-> CreateFixture(& fixtureDef));
polygonShape.SetAsBox(0.4f * axel2Length,TEST_SCALE * 0.15f,axel2Pos,axel2Angle);
(((TestCallback *)Test)-> addIgnoreFixture(cart-> CreateFixture(& fixtureDef));
纹理类别:-
TextureObject :: TextureObject(char * texturename,CCNode * layer,int zOrder,bool isFrameCache,CCTexture2DPixelFormat pixFormat,int tag)
{
CCTexture2D :: setDefaultAlphaPixelFormat(pixFormat);
CCSprite * newSprite = CCSprite :: createWithSpriteFrameName(texturename);
m_Layer =图层;
m_Layer-> addChild(newSprite,zOrder);
newSprite-> setTag(tag);
m_Texture = newSprite;
CCTexture2D :: setDefaultAlphaPixelFormat(kTexture2DPixelFormat_Default);
}
在墙上的课:-(检测Collison检测)
bool wall :: HandleBeginContact(b2Fixture * fixtureA,b2Fixture * fixtureB)
{
b2Fixture * myFixture = m_MainCollision-> GetFixtureList ();
if(myFixture ==夹具A || myFixture ==夹具B)
{
b2Body * bodyA = myFixture-> GetBody();
** CCSprite * sprite =(CCSprite *)bodyA-> GetUserData(); ** // spriteA正在获取空值
CCLog( spritea =%d,sprite-> getTag() );
if(sprite-> getTag()== 2)
{
//骑车离开,它将从起点开始坐姿
}
}
返回false;
}
解决方案
像这样尝试:-
bool wall :: HandleBeginContact(b2Fixture * fixtureA,b2Fixture * fixtureB)
{
b2Body * bodyA = FixtureA-> GetBody();
b2Body * bodyB =夹具B-> GetBody();
if(CartBody == bodyA || CartBody == bodyB)
{
CCLog( am正确碰撞);
}
返回false;
}
am having one car and one bike object. at the time of collison detction. i need to get which(Car or bike) is colliding with wall. for that am using body->getuserdata . but it always returns null. am confuse d that why am getting null. were am wrong?
i refer this link but same as i did. not useful.
Code for car:-
sprintf(temp,CAR_BODY_CAR_PLIST);
m_Texture[2] = new TextureObject(temp,spritesheet1,1,true,kTexture2DPixelFormat_Default,2);
// add cart //
b2BodyDef bodyDef;
b2FixtureDef fixtureDef;
b2PolygonShape polygonShape;
b2CircleShape circleShape;
bodyDef.position.Set(startPos.x,startPos.y);
bodyDef.type = b2_dynamicBody;
**bodyDef.userData = m_Texture[2];**
cart = m_world->CreateBody(&bodyDef);
fixtureDef.density = 25.0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.1f;
fixtureDef.filter.categoryBits = 0x0008;
fixtureDef.filter.maskBits = 0x0003;
fixtureDef.density = 0.0f;
fixtureDef.isSensor = true;
b2Vec2 points[6] =
{
b2Vec2(0.7f,0.0f),
b2Vec2(0.7f,3.25f),
b2Vec2(-0.7f,3.25f),
b2Vec2(-0.7f,0.0f),
};
polygonShape.Set(points,4);
m_RiderTest = cart->CreateFixture(&fixtureDef);
((GroundTestCallback*)m_GroundRayCastTest)->addIgnoreFixture(m_RiderTest);
fixtureDef.isSensor = false;
fixtureDef.density = 25.0f;
fixtureDef.filter.categoryBits = 0x0010;
fixtureDef.filter.maskBits = 0x0007;
polygonShape.SetAsBox(TEST_SCALE*0.4f, TEST_SCALE*0.15f, axel1Pos, axel1Angle);
((TestCallback*)Test)->addIgnoreFixture(cart->CreateFixture(&fixtureDef));
polygonShape.SetAsBox(0.4f*axel2Length, TEST_SCALE*0.15f, axel2Pos, axel2Angle);
((TestCallback*)Test)->addIgnoreFixture(cart->CreateFixture(&fixtureDef));
texture class:-
TextureObject::TextureObject(char *texturename,CCNode *layer, int zOrder, bool isFrameCache,CCTexture2DPixelFormat pixFormat, int tag)
{
CCTexture2D::setDefaultAlphaPixelFormat(pixFormat);
CCSprite *newSprite = CCSprite::createWithSpriteFrameName(texturename);
m_Layer = layer;
m_Layer->addChild(newSprite,zOrder);
newSprite->setTag(tag);
m_Texture = newSprite;
CCTexture2D::setDefaultAlphaPixelFormat(kTexture2DPixelFormat_Default);
}
at wall class:-(Detecting collison detction)
bool wall::HandleBeginContact(b2Fixture *fixtureA,b2Fixture *fixtureB)
{
b2Fixture *myFixture = m_MainCollision->GetFixtureList();
if(myFixture == fixtureA || myFixture == fixtureB )
{
b2Body *bodyA = myFixture->GetBody();
**CCSprite *sprite = (CCSprite *) bodyA->GetUserData();** // spriteA am getting null
CCLog("spritea = %d",sprite->getTag());
if (sprite->getTag() == 2)
{
// rider out and it will satrt from start point
}
}
return false;
}
解决方案
Try Like this:-
bool wall::HandleBeginContact(b2Fixture *fixtureA,b2Fixture *fixtureB)
{
b2Body *bodyA = fixtureA->GetBody();
b2Body *bodyB = fixtureB->GetBody();
if(CartBody == bodyA || CartBody == bodyB )
{
CCLog("am collided correctly");
}
return false;
}
这篇关于Cocos2dx Box2D-b2body GetUserData始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文