reprise - drag element


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private function createTestDragItem() : void
  2. {
  3. m_dragObject = m_rootElement.addComponent('dragElvis');
  4. m_dragObject.addEventListener(MouseEvent.MOUSE_DOWN, dragObject_mouseDown);
  5. m_dragObject.addEventListener(MouseEvent.MOUSE_UP, dragObject_mouseUp);
  6. }
  7.  
  8. private function dragObject_mouseUp(event : MouseEvent) : void
  9. {
  10. UIComponent(event.currentTarget).removeEventListener(Event.ENTER_FRAME, drag_enterFrame);
  11. }
  12.  
  13. private function dragObject_mouseDown(event : MouseEvent) : void
  14. {
  15. m_mousePositionOverCurrentDragObject = new Point(event.localX, event.localY);
  16. UIComponent(event.currentTarget).addEventListener(Event.ENTER_FRAME, drag_enterFrame);
  17. }
  18.  
  19. private function drag_enterFrame(event : Event) : void
  20. {
  21.  
  22. var leftPosition : Number = Math.min(mouseX - m_mousePositionOverCurrentDragObject.x,
  23. 600);
  24. leftPosition = Math.max(leftPosition, 0);
  25. var topPosition : Number = Math.min(mouseY - m_mousePositionOverCurrentDragObject.y,
  26. 300);
  27. topPosition = Math.max(topPosition, 0);
  28.  
  29.  
  30. m_dragObject.setStyle('left', leftPosition + 'px');
  31. m_dragObject.setStyle('top', topPosition + 'px');
  32. }

URL: http://wiki.github.com/tschneidereit/reprise

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.