4 Replies Latest reply: Oct 6, 2009 8:34 AM by Me2LoveIt2 RSS

    Placing objects with the mouse in Flash CS4

    Me2LoveIt2 Community Member

      Hi all,

      I have played some more with Flash and come upon something that i would know more about.

      I tried to make:

      A scene. when I click the left mouse button on the stage an object(from the librery [graphic]) will be place exactly where the mouse is.

       

      I did not really know how to approach this, but tried anyways.

       

      This is my code:

      stage.addEventListener(MouseEvent.MOUSE_DOWN, addApple);
      
      Function addApple(event:MouseEvent):void{
                //I dont know what to put here to place an object(apple) from the libery
                //onto the stage where the mouse is.
      };
      

      My question is:

       

      what code do I need to complete this code?

       

      And or is there better ways of doing it?

       

      ~ Thanks for your help in advance.

        • 1. Re: Placing objects with the mouse in Flash CS4
          Rob Dillon CommunityMVP

          You need to create a new instance of the Library object and then you need to position the new instance and then you need to add the instance to the display object, in this case the stage. It might go something like this:

           

          function addApple(event:MouseEvent):void{
                    var libObject:ObjectName = new ObjectName();
                    libObject.x = mouseX;
                    libObject.y = mouseY;
                    addChild(libObject);
          };

           

          Function needs to start with a lower case "f". The variable name that you create can be anything. The ObjectName will be the Class name that you gave to your object in the Library.

          • 2. Re: Placing objects with the mouse in Flash CS4
            Me2LoveIt2 Community Member

            Thank you for the reply.

             

            I still do not fully understand though, because I tried your suggestions but get errors.

            I tried this code:

            stage.addEventListener(MouseEvent.MOUSE_DOWN, addApple);
            
            function addApple(event:MouseEvent):void{
                 var libObject:Apple_mc = new Apple_mc(); // <-- This line gives me two errors
                 libObject.x = mouseX;
                 libObject.y = mouseY;
                 addChild(libObject);
            };
            

            This is basically what you send me, but I am getting two errors in line 4:

             

            1046: Type was not found or was not a compile-time constant: Apple_mc

            1180: Call to a possibly undefined method Apple_mc

             

            "Apple_mc" is the drawn picture of an apple inside a movie clip.

             

            What do you thing the problem with this code or setup might me?

             

            ~Thanks for your help and advice~

            • 3. Re: Placing objects with the mouse in Flash CS4
              Rob Dillon CommunityMVP

              The object that you want to duplicate must be a movieClip or Button object that is in the movie's Library and has the "Export for Actionscript" option set.  When you set this option, Flash will create a Class name for this object. This is the name that you need to use in the code.

              var libObject:Apple_mc = new Apple_mc()

              libObject is the name of the variable that will contain the new object instance, you can use any name that you like for this part. Apple_mc should be the name of the Class from the original object that is in the Library.

              • 4. Re: Placing objects with the mouse in Flash CS4
                Me2LoveIt2 Community Member

                Thank you this is what I needed.

                I did not know about the Export for Actionscript option, that was what held me back.

                Thanks it works now!