Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | sliks |
Hi,
I have a left button and right button for my game on my Android project using Godot 3.0. When the user press 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 || event is InputEventScreenDrag):
if (event.pressed):
emit_signal("right_btn_pressed", event)
SceneTree.set_input_as_handled()
else:
emit_signal("right_btn_released", event)
SceneTree.set_input_as_handled()
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 move around 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 detect the InputEventScreenTouch/fire the event properly. I am not sure if it is because of the overlapping CollisionShape2D, or some other issues. Please help. Any suggestions are welcome. Thank you!