求助:
我在helloworld里面申明了变量
Sprite* paddle;
b2World* _world;
Texture2D* _spriteTexture; // weak ref
初始化在init里面,
bool SceneGame::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
b2WorldInit();
下面是b2World初始化函数
void SceneGame::b2WorldInit()
{
b2Vec2 gravity; //box2d physics
gravity.Set(0, -100);
_world = new b2World(gravity);
_world->SetAllowSleeping(true);
_world->SetContinuousPhysics(true);
_world->SetAutoClearForces(false);
auto winSize = Director::getInstance()->getWinSize();
//You firstly create a body definition to specify initial properties of the body such as position or velocity.
paddle = Sprite::create("Projectile.png");
paddle->setPosition(Point(winSize / 2));
addChild(paddle);
//创建屏幕边界
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
b2Body* groundBody = _world->CreateBody(&groundBodyDef);
auto groundBox = new b2EdgeShape();
///bottom
groundBox->Set(b2Vec2(0, 0), b2Vec2(winSize.width / PTM_RATIO, 0));
groundBody->CreateFixture(groundBox, 0);
//top
groundBox->Set(b2Vec2(0, winSize.height / PTM_RATIO), b2Vec2(winSize.width / PTM_RATIO, winSize.height / PTM_RATIO));
groundBody->CreateFixture(groundBox, 0);
//left
groundBox->Set(b2Vec2(0, winSize.height / PTM_RATIO), b2Vec2(0, 0));
groundBody->CreateFixture(groundBox, 0);
//right
groundBox->Set(b2Vec2(winSize.width / PTM_RATIO, winSize.height / PTM_RATIO), b2Vec2(winSize.width / PTM_RATIO, 0));
groundBody->CreateFixture(groundBox, 0);
b2Body *_paddleBody;
//Once you set that up, you can use the world object to create a body object by specifying the body definition.
// Create paddle body
b2BodyDef paddleBodyDef;
paddleBodyDef.type = b2_dynamicBody;
paddleBodyDef.position.Set(winSize.width / 2 / PTM_RATIO, 50 / PTM_RATIO);
paddleBodyDef.userData = paddle;
_paddleBody = _world->CreateBody(&paddleBodyDef);
//You then create a shape representing the geometry you wish to simulate.
// Create paddle shape
b2PolygonShape paddleShape;
paddleShape.SetAsBox(paddle->getContentSize().width / PTM_RATIO / 2, paddle->getContentSize().height / PTM_RATIO / 2);
// Create shape definition and add to body
b2FixtureDef paddleShapeDef;
paddleShapeDef.shape = &paddleShape;
paddleShapeDef.density = 10.0f;
paddleShapeDef.friction = 0.4f;
paddleShapeDef.restitution = 0.1f;
//You then create a fixture definition – you set the shape of the fixture definition to be the shape you created, and set other properties such as density or friction.
b2Fixture *_paddleFixture;
//Finally you can use the body object to create a fixture object by specifying the fixture definition.
_paddleFixture = _paddleBody->CreateFixture(&paddleShapeDef);
// Use batch node. Faster
auto parent = SpriteBatchNode::create("Projectile.png", 100);
_spriteTexture = parent->getTexture();
_spriteTexture->retain();
addChild(parent, 0, kTagParentNode);
addNewSpriteAtPosition(_spriteTexture, _world, Point(winSize / 2));
addNewSpriteAtPosition(_spriteTexture, _world, Point(Director::getInstance()->getWinSize() / 2));
}
但是现在我想用这个变量出问题了
我在初始化之后看变量是正常的

但是我按键回调之后出现了这样的问题,因为是单步运行的,应该不存在未初始化的问题
这是按键回调函数和调用变量的函数:
void SceneGame::touchButton(cocos2d::Ref* object, TouchEventType type)
{
//其他选项,后续添加
//测试动画
toAddSprite();
lastActionCompleted = true;
}
void SceneGame::toAddSprite()
{
if (!_world ||!_spriteTexture)
return;
addNewSpriteAtPosition(_spriteTexture, _world, Point(Director::getInstance()->getWinSize() / 2));
}
以下是两个变量,这里看到是未初始化的:

求大神帮忙分析下我是哪里出了问题,这个_world不是node,也不能retain,sprite我retain了还是这个问题,之前遇到个问题也是,设置一个类变量,然后在按键内部修改,update定时器内部这个变量的值还是没有改变