Physics Body spawns at wrong location

Hi,
I have a problem due to my Physics Body spawning at (0, 0) when I create an enemy.

My game is a sidescroller where the enemies walk from right to left and on the left side of the screen I use a raycast to check whether an enemy has reached its destination. So now I have the problem that the enemies reach their destination immediately when they spawn instead of when they reach the left side of the screen by walking there.

My inheritance hierarchy is as follows:
Scene -> AutoScrollerClass -> ScreenToBeScrolled (inherits Node) -> EnemyClass (inherits Node) -> Sprite (which has the PhysicsBody component)

I set the position and scale of the EnemyClass, but when I create the PhysicsBody and add it to the sprite (which is at this point not child of the enemy class yet) it spawns in the bottom left corner of the screen ((0,0) of the Scene I imagine) and only then gets moved to its proper location. How can I avoid this?

PS: I tried setting the position and scale of the sprite instead, but it didn’t help.

can you please post your code.

I’ll split it into multiple answers. Here’s the code for the raycast:

void EnemyTouchdownZone::update(float deltaTime)
{
	physicsWorld = getScene()->getPhysicsWorld();
	physicsWorld->rayCast(CC_CALLBACK_3(EnemyTouchdownZone::touchdown, this), cocos2d::Vec2(touchdownLineX, 0), cocos2d::Vec2(touchdownLineX, _director->getVisibleSize().height), nullptr);

	debug->drawSegment(cocos2d::Vec2(touchdownLineX, 0), cocos2d::Vec2(touchdownLineX, _director->getVisibleSize().height), 1, cocos2d::Color4F::RED);
}

bool EnemyTouchdownZone::touchdown(cocos2d::PhysicsWorld& world, const cocos2d::PhysicsRayCastInfo& info, void* data)
{
	Pirate* pirate = dynamic_cast<Pirate*>(info.shape->getBody()->getNode()->getParent());
	if (pirate == nullptr) return true;

	if(!pirate->hasScored())
		_eventDispatcher->dispatchCustomEvent("touchdown");

	pirate->score();

	return true;
}

And here’s the code for the enemy spawn function:

Pirate* Pirate::createPirate(const Spawn spawn)
{
	Pirate* pirate = Pirate::create();
	pirate->setPosition(spawn.position);
	pirate->setScale(spawn.scale);

	cocos2d::Sprite* sprite = cocos2d::Sprite::create(spawn.spriteFilename);
	sprite->setAnchorPoint(cocos2d::Vec2(0, 0));

	pirate->addChild(sprite);

	// this is very hacky but it works for my small game
	if (spawn.spriteFilename == "sprites/boaty_pirate_small.png")
	{
		pirate->setName("pirate_boat");
		cocos2d::Size pirateSize = cocos2d::Size(sprite->getContentSize().width, sprite->getContentSize().height * 0.5);
		cocos2d::PhysicsBody* pirateBody = cocos2d::PhysicsBody::createBox(pirateSize);
		pirateBody->setGravityEnable(false);
		pirateBody->setCategoryBitmask(0x08);
		pirateBody->setCollisionBitmask(0x00);
		pirateBody->setContactTestBitmask(0x03);
		sprite->addComponent(pirateBody);
	}
	else if (spawn.spriteFilename == "sprites/parrot_frame0.png")
	{
		cocos2d::Size pirateSize = cocos2d::Size(sprite->getContentSize().width * 0.75, sprite->getContentSize().height * 0.5);

		cocos2d::Vector<cocos2d::SpriteFrame*> animationFrames;
		cocos2d::Rect rect = cocos2d::Rect::ZERO;
		rect.size = sprite->getContentSize();

		animationFrames.pushBack(cocos2d::SpriteFrame::create("sprites/parrot_frame0.png", rect));
		animationFrames.pushBack(cocos2d::SpriteFrame::create("sprites/parrot_frame1.png", rect));

		cocos2d::Animation* birdAnimate = cocos2d::Animation::createWithSpriteFrames(animationFrames, 0.25f);
		sprite->runAction(cocos2d::RepeatForever::create(cocos2d::Animate::create(birdAnimate)));

		cocos2d::PhysicsBody* pirateBody = cocos2d::PhysicsBody::createBox(pirateSize);
		pirateBody->setGravityEnable(false);
		pirateBody->setCategoryBitmask(0x08);
		pirateBody->setCollisionBitmask(0x00);
		pirateBody->setContactTestBitmask(0x03);
		sprite->addComponent(pirateBody);
	}

	return pirate;
}

Why did you set the anchor point of the sprite to (0, 0)?

anchor point decides what part of the sprite is used when setPosition() is called.