Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 551885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:26:30+00:00 2026-05-13T11:26:30+00:00

Below is sample code from a Module I have in my Flex app. I

  • 0

Below is sample code from a Module I have in my Flex app. I want to dynamically create mx.controls.Image objects and add them to the display. I want the user to be able to move them (this works as below) and be able to change their z-index as well as record new X and Y coords after moving back to the database. How do I determine which object in this.getChildren() is the ‘selected’ item the user is working with? When I add a MOUSE_OVER event, for example, it does not work.

<?xml version="1.0" encoding="utf-8"?>

        import flash.display.DisplayObject;
        import flash.events.MouseEvent;
        import mx.controls.Image;

        public var draggedObject:DisplayObject;
        public var selectedObject:DisplayObject;
        public var offsetX:Number;
        public var offsetY:Number;


        public function Add():void
        {
            var _image:Image = new Image;
            _image.source = "myimage.png";
            _imagem.x = 100;
            _image.y = 100;
            _image.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
            _image.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
            this.addChild(_image);
        }

        // This function is called when the mouse button is pressed.
        public function startDragging(event:MouseEvent):void
        {
            // remember which object is being dragged
            draggedObject = DisplayObject(event.target);
            selectedObject = draggedObject;

            // Record the difference (offset) between where the cursor was when
            // the mouse button was pressed and the x, y coordinate of the
            // dragged object when the mouse button was pressed.
            offsetX = event.stageX - draggedObject.x;
            offsetY = event.stageY - draggedObject.y;

            // move the selected object to the top of the display list
            //stage.addChild(draggedObject);

            // Tell Flash Player to start listening for the mouseMove event.
            stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject);                  
        }

        // This function is called when the mouse button is released.
        public function stopDragging(event:MouseEvent):void
        {
            // Tell Flash Player to stop listening for the mouseMove event.
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject);
        }

        // This function is called every time the mouse moves,
        // as long as the mouse button is pressed down.
        public function dragObject(event:MouseEvent):void
        {
            // Move the dragged object to the location of the cursor, maintaining 
            // the offset between the cursor's location and the location 
            // of the dragged object.
            draggedObject.x = event.stageX - offsetX;
            draggedObject.y = event.stageY - offsetY;

            // Instruct Flash Player to refresh the screen after this event.
            event.updateAfterEvent();
        }
    ]]>
</mx:Script>
<mx:Button x="83" y="93" label="New Image" click="this.Add()"/>
<mx:Label x="83" y="138" id="label1"/>
<mx:Label x="83" y="164" id="label2"/>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:26:30+00:00Added an answer on May 13, 2026 at 11:26 am
    1. Use the startDrag and stopDrag method on an DisplayObject you will avoid to do it yourself
    2. numChildren will give you the number of children in the display list
    3. getChildIndex will give you the index of a object in the display list
    4. setChildIndex / swapChildren can be used to change the “z-index” into the display list

    So mixing this different functions you could end up with:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
    <![CDATA[
        import mx.controls.Image;
    
        private function addImage() : void {
            var img : Image = new Image();
            img.source = imgClass;
            img.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
            canvas.addChild(img);
        }
    
        private function onMouseUp(e : MouseEvent) : void {
            var img : Image = e.currentTarget as Image;
            if (img !== null) {
                stopDrag();
                img.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            }
        }
    
        private function onMouseDown(e : MouseEvent) : void {
            var img : Image = e.currentTarget as Image;
            if (img !== null) {
                var parent : DisplayObjectContainer = img.parent;
    
               // get the index of the image and do what you want with it
                var idxImg : int = parent.getChildIndex(img);
    
               // put the clicked object on top of the list
                parent.setChildIndex(img, parent.numChildren - 1);
    
                img.addEventListener(MouseEvent.MOUSE_UP, onMouseUp, false, 0, true);
                img.startDrag();
            }
        }
    
        [Embed(source="assets/logo.png")]
        private var imgClass : Class;
    ]]>
    </mx:Script>
    <mx:Canvas id="canvas">
        <mx:Button label="add" click="addImage()"></mx:Button>
    </mx:Canvas>
    </mx:Application>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 294k
  • Answers 294k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This should work: $select->joinLeft( 'st_line_item', $this->_db->quoteInto( 'st_line_item.order_id = st_order.id and… May 13, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer According to the Command tutorial on vogella.de some common commands… May 13, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer Well it turns out Microsoft Solver Foundation does not support… May 13, 2026 at 6:40 pm

Related Questions

Is there a simple way to translate an XLS to a CSV formatted file
We have a web service framework we use; which I had no part in
I am writing a custom c# HttpModule that will handle requests from all file
I have some very simple code to generate an assembly and invoke a method
I am creating Linq expression trees from F# that operates on a custom datatype

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.