Flipping a sprite

I’m making an enemy npc and when it hits a wall i want it to flip on the x axis and then move in the direction it came from. The code below currently flips the characters animation but i was wondering how i would also flip its collision boxes because i have a hitbox which i want to move to the other side of the character once it has flipped.

sprite.set_hflip("#sprite", self.movement < 0)

below is a screenshot of what the enemy looks like after it has flipped on the x axis, thew circular box is the enemies movement collision group and the rectangular box is the enemies hitbox
image

How about you have two collision objects on either side, and enable/disable them according to the direction?

that’s a great idea!! But first off how would i know what direction the enemy is facing?

Also i’ve got an issue where the enemy hits the right wall it gets knowkced out of its collision box, and when it hits the right wall it gets knowkced out of its collision box. Have you come across this issue before or know how i could go about fixing it?

You already know this based on the code you’ve posted:

sprite.set_hflip("#sprite", self.movement < 0)

This is saying that when self.movement is less than zero, then the sprite should be flipped. If it’s greater than or equal to 0, then it is not flipped. Just adapt this to the new setup!

You should isolate this into a new thread with more relevant information.

Perhaps you can use physics.set_hflip()?

2 Likes

i tried:

	physics.set_hflip("enemy:/enemy#enemy_hitbox", self.movement < 0)

But i got the error message:

Do you know what could be causing this?

The error message is giving you a hint! Looks like enemy.script is not in the same collection as
enemy:/enemy#enemy_hitbox. It has to be for the function to work.

Edit: I see you started a new thread. Bit annoying because it means multiple people answer your question in multiple places.

1 Like