|
|
|
|
Reply From: |
eons |
Objects can interact in many ways, that depends on the design.
In your case, click and box appears where is clicked, is not magic, you programmed it to do that, if you want to move slowly then you need to program that behaviour too.
get_pos
or get_global_pos
on 2D are used to get position, if you have a reference to a node (you can get it in many ways, depends on what you want to do and your design) just use those methods.
CollisionObjects (like Area) have _input_event
, if they have mouse event is because the mouse is on the area.
There are other ways, you can use Physics2DDirectSpaceState intersect methods and test mouse position to check if there is something in that point.
Once you “picked” a CollisionObject, make it “follow” the mouse pointer.
There are 2 official demos that use areas and pickable bodies that you can look to see how works.
Okay thanks! What about an object entering the area 2D? how do you check for that?
Noob_Maker | 2017-06-15 20:23
Areas can be used for overlap detection against other areas/bodies, so if the node has a child area (or is an area), the faster way is to use the area/body_enter
signals.
An example, the player is a body and spikes an area, spikes can have a signal connected to something (or self), the _enter
signals have a parameter with reference to the body that entered so in this case (after a type or group check) you can call a method on that body to “tell” it what happened.
On demos:
- The princess on the kinematic character demos are areas and use a signal to display a label on body enter.
- Coins on platformer demos are areas that use a flag to accept or ignore the signal and play an animation.
- The ship on the simple shooter demo is an area that detects body enter (for tilemap collision) and area enter (for enemy, I think it should test group instead of method but is just preference).
- Shower of bullets demo use a more advanced way manage a high number of physics elements.
Thanks! I’m trying out the method of having the Area2D check if the mouse enters and send a signal the main Node which I found to be more efficient than having an invisible object constantly follow a mouse but I’ll keep this in mind once I start working with hazardous objects to send the signal to the player that the character is dead.
Noob_Maker | 2017-06-30 00:48