How to restrict the drag and drop area in a canvas

无人久伴 提交于 2019-12-06 08:34:43

The signature for startDrag() is public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void

The second parameter allows you to pass a Rectangle to act as bounds for your DisplayObject. It won't be dragged outside of this

You should catch MouseDown event on the target controls and then subscribe MouseMove event. In MouseMove handler you should get Canvas and control's rectangles (in the same coordinate space) and use containsRect() method (documentation is here) to determine if control is still within Canvas. If it goes out you shouldn't move it.

And don't remember unsubscribe your MouseMove event on MouseUp!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!