I have a "virtual" left button and right button for my game on my Android project using Godot 3.0. When the user presses/touches the left or right button, it will control the main character to move left or right. Here is the script for the right button.
func _on_RightButton_input_event(viewport, event, shape_idx):
if (event is InputEventScreenTouch):
if (event.pressed):
emit_signal("right_btn_pressed", event)
else:
emit_signal("right_btn_released", event)
And this is the node structure of the right button:
RightButton (Area2D)
+CollisionShape2D (a circle)
+Sprite (a right arrow image)
I also have some enemy spawning randomly on the scene. They always move to the left and here is the script for the enemies
extends Node2D
enum MOVEMENT_TYPES{normal}
var movementType = MOVEMENT_TYPES.normal
export var motion = Vector2()
const UP = Vector2(0, -1)
var dyingBounce = true
var counter = 1
var enemyDie = false
func _physics_process(delta):
if(enemyDie):
if dyingBounce == true:
motion.y = -100
dyingBounce = false
motion.y +=10
if(movementType == MOVEMENT_TYPES.normal):
motion = $Body.move_and_slide(motion, UP)
else:
var xf = Transform2D()
xf[2]= motion * (delta * counter)
counter = counter - 2
$Body.transform = xf
func hit_by_main_character():
print("hit")
$Body.get_node("CollisionShape2D").disabled = true
enemyDie = true
pass # replace with function body
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
And this is the node for the enemy:
Enemy
+ Body
+ VisibilityNotifier2D
+ ColorRect (just a rectangle as a place holder)
+ CollisionShape2D (also a rectangle)
Most of the time, the button works. However, when the enemy moves behind the right button, the right button will not emit the input_event signal. (I am connecting the input_event signal with _on_RightButton_input_event.) I am not sure if it is because of the overlapping CollisionShape2D, or some other issues. Please help. Any suggestions are welcome. Thank you!
Take me two whole days to figure it out. I am not sure why but the following config fixes the issues.
In the colorRect under Enemy, there is a "Mouse" section. Originally it was set to "stop". Change it to "Ignore" fixes the issue.