Jawnnypoo / PhysicsLayout

Android layout that simulates physics using JBox2D
Apache License 2.0
772 stars 102 forks source link

Get Body after adding View dynamically #13

Closed OFmarting closed 8 years ago

OFmarting commented 8 years ago

Hi, I'm adding Views to the PhysicsLayout with addView(). I want to create a Joint between multiple dynamically added Views. How can I access the Body Instance right after adding the View so I can create a Joint?

Jawnnypoo commented 8 years ago

Hey there, you should be able to use getPhysics().findBodyById(R.id.yourviewid) on the PhysicsLayout. Have you given this a try?

OFmarting commented 8 years ago

Unfortunately the Body is null when I try to access it after adding the View with addView().

Jawnnypoo commented 8 years ago

Hey there, this has to do with the fact that the body is not actually created until the view measures itself. This is due to the fact that we need to wait for measure before adding the bodies so that we have the proper size to set the bodies to. A OnBodyCreatedListener has been added to the 2.1.0 release so that you can hook into the body creation and do the connections as needed.

OFmarting commented 8 years ago

Thank You!

Jawnnypoo commented 8 years ago

No problem! Let me know if it works out

OFmarting commented 7 years ago

Everything worked out well :+1: