PHYSICS IN TILEMAP

PHYSICS IN TILEMAP
0

my english is not good, im use google translate
i got error when add physics body to sprite in tilemap
///////
my code
//2. menambahkan map tilemap
auto map = TMXTiledMap::create(“gfx/gameplay/level_1.tmx”);
auto layer = map->getLayer(“background”);
addChild(map, 0, 99); // with a tag of ‘99’
//
auto block_body = PhysicsBody::createBox(kucing->getContentSize(),PHYSICSBODY_MATERIAL_DEFAULT);
block_body->setDynamic(false);

//uji coba
auto sprite_bawah = layer->getTileAt(Vec2(49,50));
sprite_bawah->setPhysicsBody(block_body);

////////
my error
Exception thrown: read access violation.
cocos2d::Scene::getPhysicsWorld(…) returned nullptr.

The error says, that when the code tries to access the physics world, it gets a null pointer rather than a pointer to the physics engine to be used in the world.

I had a similar problem.

I solved it by calling “bool Scene::initWithPhysics()” in my scene’s init() before doing any physics stuff.

bool ArtilleryDuelScene::init()
{
	if ( !Scene::init() )	{
		return false;
	}

	initWithPhysics(); //<--CALL BEFORE DOING PHYSICS TO INIT getPhysicsWorld()
	//getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
	getPhysicsWorld()->setGravity(Vec2(0, -98));
	
	...
	
	auto _ball = CreateSprite("red_ball.png");
	_ball->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
	_ball->addComponent(PhysicsBody::createCircle(5.0));
	this->addChild(_ball, 1);
	
	...
}
1 Like