Godot Engine

Godot Engine

Raptec May 3, 2021 @ 8:49pm
How flip enemy sprites?
I have an enemy with already an Area2d as sensor, so he chase me when i touch the area, the problem reside when i pass behing him and don´t flip his sprites, already try many things but nothing close, here is what i do until the moment:
(by the way, "Side" is a new area2d i make to the enemy with this sole intention of flip him, i put that behind him, so in theory if i get behind him, he should flip)

1. func _on_turnback_area_entered(area):
if scale.x = scale.y * -1

works only one time, the enemy in fact "flip", but when i touch again the "turnback" it don´t react anymore to it, and don´t flip again

2. func _on_turnback_area_entered(area):
$Sprite.flip_h = false
Do nothing, and give me this error:
E 0:00:13.689 emit_signal: Error calling method from signal 'area_entered': 'KinematicBody2D(KB2D Rata.gd)::_on_turnback_area_entered': Method not found..
<Fuente C++> core/object.cpp:1257 @ emit_signal()

3. func _on_turnback_area_entered(area):
$Sprite.flip_h = true

(Kick me out of the game)
E 0:00:10.836 get_node: (Node not found: "Sprite" (relative to "/root/Arena de pruebas/Enemigo Rata").)
<Error de C++>Condition "!node" is true. Returned: __null
<Fuente C++> scene/main/node.cpp:1371 @ get_node()
<Stack Trace> KB2D Rata.gd:42 @ _on_turnback_area_entered()

I also try with $Sprite.set_flip_h, $AnimationPlayer.set_flip_h, $AnimationPlayer.flip_h
with similar results

The enemy is facing to the left and the player to the right as start, so I thought in something like: if the coordinate X of the player is bigger than the enemy (x>), then enemy should flip, and when the coordinate of X the player is less than the enemy (x<) then enemy should flip back , but i don´t know how write that, already read the official documentation but get lost in the terminology, in the seach bar there is not a word "coor" or "coordinate" or something like that
< >
Showing 1-5 of 5 comments
AAA May 4, 2021 @ 10:58pm 
1. Why you depend scale x on scale y?
Let's assume this your sprite size is (a, b) and you flip it with your method, it will be (-b, b) and when you flip it again it will still be (-b, b) :rbrb3:
You should do it like this
scale.x = -scale.x # or scale.x = scale.x * -1 # or scale.x *= -1

2. your signal connection seem broken, try reconnect it.

3. $Sprite is same as get_node("Sprite"), it is search for direct child node with name "Sprite" of this node. I don't know how you store your Area2d node and Sprite node, you might want try to access parent first
get_parent().get_child("Sprite") # or $"../Sprite"
(in this case I assume you store like this
- Enemy (Node2d)
-- Sprite
-- Area2d
--- CollisionShape2d
)

4. global_position return position of Node2d


Note; I am not insult you or something, but I think you should learn more about basic Godot, basic programming (non Godot) and basic game dev. Prefer you to learn from videos not text for now and do along the videos. Good luck :rbrbu3:
Last edited by AAA; May 4, 2021 @ 11:00pm
Raptec May 4, 2021 @ 11:18pm 
Nothing of that help me, but is appreciated
Ellye May 14, 2021 @ 8:11am 
Originally posted by Raptec:
Nothing of that help me, but is appreciated
His first point is the exact answer for your issue.

You're doing:

scale.x = -scale.y

So your width becomes your negative height.

Assuming default values, you went from (1.0, 1.0, 1.0) to (-1.0, 1.0, 1.0)... so the first time, it would seem like it worked.

But then when you run that code again and nothing happens. Because scale.y was never changed, so the negative of it is still -1.0, so scale.x will still become -1.0, not 1.0 like you'd want to.

You just need to do:

scale.x = -scale.x
Last edited by Ellye; May 14, 2021 @ 8:16am
Raptec May 14, 2021 @ 9:51am 
Thanks!
AAA May 14, 2021 @ 9:55am 
And my 2 and 3 is resolve your 2 and 3 off-topic issues
Can't connect signal because method is now not found and can't find node in question.

About 4, since you said thing about no existence of coor or coordinate, so I point out proper name of that method.
< >
Showing 1-5 of 5 comments
Per page: 1530 50

Date Posted: May 3, 2021 @ 8:49pm
Posts: 5