Search code examples
actionscript-3flashclickmouseeventmouseclick-event

AS3 Object appears onClick where the mouse clicks on the stage


I want in actionscript to place an object in my library onto the stage where I clicked. Seems easy? Right? TOTALLY BLANKING. any help would be awesome :) my code thus far is:

package code {

import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Main extends MovieClip {

    public var redBox: Box = new Box(mouseX, mouseY);

    public function Main() {
        // constructor code
        stage.addEventListener(MouseEvent.CLICK, mouseClickEvent);
    }
    public function mouseClickEvent(e:MouseEvent):void {
        addChild(redBox);
    }
}

}

that is the main and then the box code is:

package code {

import flash.display.MovieClip;

public class Box extends MovieClip{

    public function Box(myX:Number, myY:Number) {
        // constructor code
        myX = x;
        myY = y;
    }

}

}

Solution

  • Just do this :

    package code {
    
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    public class Main extends MovieClip {
    
        public var redBox: Box = new Box();
    
        public function Main() {
            // constructor code
            stage.addEventListener(MouseEvent.CLICK, mouseClickEvent);
        }
        public function mouseClickEvent(e:MouseEvent):void {
            redBox.x = stage.mouseX;
            redBox.y = stage.mouseY;
            addChild(redBox);
    
        }
    }
    
    }