Add a second collision shape to the area. One of the parameters you’re passed with the input_event signal is the shape_idx which identifies which shape collected the input.
If I understand the question, you want to process a mouse click event, but only when it’s on the tower icon instead of anywhere inside the collision shape. If that’s the case, you can just include 2 CollisionShape2D nodes (one for the range, and one just covering the tower icon) like this. That could look like this in the scene tree.
Tower (Area2D)
- range (ColliisonShape2D for tower range)
- click (CollisionShape2D for mouse click)
Then, in your input handler, you just need to check the value of the passed shape_idx argument, which indicates which collisionshape was clicked). So, something like:
func _on_Area2D_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.pressed && shape_idx == 1:
print("Tower clicked")
Note, the clicking on the “range” collision shape will set shape_idx to 0 and clicking on the click collision shape will set shape_idx to 1
Got it ! Thank you very much.
Im assuming 0 and 1 are the order of the layers being played out ? Like if I were to add a 3rd collision shape it would be 2 ?
Thank !
quizzcode | 2020-05-15 23:12
Yeah, the indices will be from 0-N (based on the number of collision shapes associated with the area2d) and will be in the order they’re shown in the scene tree (with the upper-most node being 0 and the bottom-most node being “N”).