Note that there are some explanatory texts on larger screens.

plurals
  1. POas3 - addchild with drag and drop
    primarykey
    data
    text
    <p>I am trying to add a child instance of an object to the stage, then allow the user to drag and drop this object (in this case, a movie clip) on the stage. However, I am getting the following error:</p> <blockquote> <p>TypeError: Error #1009: Cannot access a property or method of a null object reference. at working_copy_fla::MainTimeline/dragObject()</p> </blockquote> <p>So, that is my first problem. Then second problem, is I have not found an answer as to how to make a child object (specifically, a movie clip) able to properly be dragged and dropped on the stage.</p> <p>Here is my code:</p> <pre><code>// Allow buttons to bring objects to the stage myButton.addEventListener(MouseEvent.CLICK, addImage); function addImage(event:MouseEvent):void { var myImage:Image_mc = new Image_mc(); stage.addChild(myImage); // Center the object myImage.x = 300; myImage.y = 300; // Allow the object to be drag and dropped myImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); myImage.addEventListener(MouseEvent.MOUSE_UP, stopDragging); } function startDragging(event:MouseEvent):void { event.target.x = event.target.parent.mouseX - event.target.mouseX event.target.y = event.target.parent.mouseY - event.target.mouseY stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject); } function dragObject(event:MouseEvent):void { event.target.x = event.target.parent.mouseX - event.target.mouseX event.target.y = event.target.parent.mouseY - event.target.mouseY } function stopDragging(event:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject); } </code></pre> <p><strong>EDIT</strong></p> <p>I figured it out, and the solution was as simple as looking to the sample code in Adobe Flash (using CS6). Here is my code now:</p> <pre><code>// Allow buttons to bring objects to the stage myButton.addEventListener(MouseEvent.CLICK, addImage); function addImage(event:MouseEvent):void { var myImage:Image_mc = new Image_mc(); stage.addChild(myImage); // Center the object myImage.x = 300; myImage.y = 300; // Allow the object to be dragged myImage.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag); } function clickToDrag(event:MouseEvent):void { event.target.startDrag(); } stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop); function releaseToDrop(event:MouseEvent):void { event.target.stopDrag(); } </code></pre> <p>The key here was that I have created universal functions (clickToDrag and releaseToDrop) that will accept input from any object (so I can re-use these functions with other images that I add to the stage). This code works with multiple children on the stage (all can be drag and dropped at any time).</p> <p>The only problem I am having with it now is that I am getting this error whenever I spawn a child element (by clicking on the <em>myButton</em> button instance):</p> <pre><code>ReferenceError: Error #1069: Property stopDrag not found on flash.display.SimpleButton and there is no default value. at working_copy_fla::MainTimeline/releaseToDrop() </code></pre> <p>This error is not stopping the application from working; everything still runs fine. But I would still like to figure out why this error is occuring. My guess is that whatever is using "stopDrag" (should just be a movie clip) is not capable of that method.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload