A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 38

Thread: Select and move multiple movieclips dynamically (AS2, CS4)

  1. #1
    Member
    Join Date
    Sep 2005
    Posts
    64

    Select and move multiple movieclips dynamically (AS2, CS4)

    Hi Flashkit.

    I have a button that uses attachMovie to add a movieclip to the stage. You can add multiple instances of this clip to the stage, which works fine.

    I also have a feature whereby you can select and move these objects around the stage one by one.

    I am trying to add a feature whereby you can select more than one object at a time (indicated with a drop shadow - which I have working) and then move them together. This feature would also allow you to rotate the selected objects by 90º, leaving the unselected objects where they are.

    I'm fairly certain that I'll need an array to hold the selected objects, and then use this info to move/transform only the chosen objects, but I can't figure out how to implement this.

    Any help on this tricky issue greatly appreciated!!!!


  2. #2
    Member
    Join Date
    Sep 2005
    Posts
    64
    No-one had any thoughts on this? Maybe I need to put it more simply.

    I'd like to select multiple movieclips and then move them simultaneously using AS2. The selection process is simple: click the movieclip (to make active), and click again to deactivate it. Using this method, select your chosen shapes on the stage, and move them simultaneously by dragging any one of the chosen shapes. The chosen shapes will stay in formation.

    Any help out there? Pretty please...?

  3. #3
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    I understand what you are requesting, and you are correct, using AS2.0 an array will be needed to store all of the selected MCs. Then you can use a mousemove function to move the object an equal number of pixels based on the initial and current mouse position.

    Since each object will have its own instance name, it isn't as much of a difficult proposal as it may sound....at least at first glance.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  4. #4
    Member
    Join Date
    Sep 2005
    Posts
    64
    Thanks for the reply - I have my mousemove function working when it comes to individual objects, but I'm getting lost on moving more than one. I'm trying to compare the chosen objects _x and _y with that of the selected objects for a simple difference number to move that individual object. Having trouble knowing where to set-up the array...

    Here's some (not working) code:

    Actionscript Code:
    thisShape.onPress = function() {
                selectedShape = this;
                _root.shapeNum++;
                this.swapDepths(_root.shapeNum);
                moveMe = true;
               
                // where was it clicked
                mouse_on_me_x = _xmouse - selectedShape._x;
                mouse_on_me_y = _ymouse - selectedShape._y;
               
                // set up multiple shapes
                for (i = 1000; i < _root.shapeNum; i++) {
                    moveMeToo = _level0.workArea_mc["shape" + i];
                   
                    moveDiff_x = moveMeToo._x - selectedShape._x;
                    moveDiff_y = moveMeToo._y - selectedShape._y;              
                }
        };
        thisShape.onMouseMove = function() {
                if (!moveMe) {
                    return;
                }
                // snap to grid
                selectedShape._x = _xmouse - mouse_on_me_x - ((_xmouse - mouse_on_me_x) % _root.gridSize);
                selectedShape._y = _ymouse - mouse_on_me_y - ((_ymouse - mouse_on_me_y) % _root.gridSize);
               
                // check against all available shapes to see if they move too
                for (i = 1000; i < _root.shapeNum; i++) {
                    if (moveMeToo.activeMe == undefined || false) {
                        moveMeToo.activeMe = false;
                    } else if (moveMeToo.activeMe == true) {
                        // move objects:
                        moveMeToo._x = selectedShape._x + moveDiff_x;
                        moveMeToo._y = selectedShape._y + moveDiff_y;
                    }
                }
                updateAfterEvent();
        };

    Thanks!

  5. #5
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    I'd change your code a little, since you don't have to worry about the multiple selection until you start moving an object. For the press function, it job should be to add or remove itself from a designated array.

    I think I understand the problem. Your code has been built within EACH of the objects placed on the stage, but you need that to relate to all objects on the stage.

    You may want to move your mousemove function to a global level along with the array so it can be modified from the main timeline and not from within each object. It will make your work easier.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  6. #6
    Member
    Join Date
    Sep 2005
    Posts
    64
    I think I understand. How would I implement a global function?

  7. #7
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    Very similar to a local one, except instead of having the individual objects call the local function, you have the stage monitor the movement and apply that movement to all objects within the array (all selected). I'll try to replicate and send it to you....
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  8. #8
    Member
    Join Date
    Sep 2005
    Posts
    64
    Great thanks...!

  9. #9
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    What I'll do it have circles placed on the stage and then you select one, a drop shadow will appear. If you on a selected object, and press and hold and move then all selected objects will move together.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  10. #10
    Member
    Join Date
    Sep 2005
    Posts
    64
    just the ticket...


  11. #11
    Member
    Join Date
    Sep 2005
    Posts
    64
    Looking forward to seeing it...
    (when do you think it possible?)


  12. #12
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813

    Talking Possible Solution

    Sorry it took so long, I had other work to complete, below is the code I used to get your function to work, I've also attached the file.

    Your version was very very close, it was all about the placement of the code, separating global and local functions and how it interacts with all the objects (balls in my case) at one time. I also used a timer to determine if the user is selecting or pressing and holding. Anyway, here is the code which ALL appears on the main timeline, the only thing I have within my object is a stop();

    PHP Code:
    //Randomly place 10 circles on the stage
    var NUMOFBALLS:Number 10;
    var 
    selectArr:Array = new Array();
    var 
    moveObjects:Boolean false;
    var 
    isMoving:Boolean false;
    var 
    intervalID:Number;
    var 
    minTime:Number 0;
    var 
    PAHDelay:Number 100;
    var 
    deselectOnRelease:Boolean true;

    //STAGE EVENT LISTENER
    this.onEnterFrame = function()
    {
        if(
    moveObjects && !isMoving && minTime PAHDelay)
        {
            
    //Move all objects that are in the selectArr
            
    for(k=0selectArr.lengthk++)
            {
                
    //Calculate their differences from the mouse
                
    selectArr[k].distX selectArr[k]._x _root._xmouse;
                
    selectArr[k].distY selectArr[k]._y _root._ymouse;
            }
            
    isMoving true;
        }
    }

    this.onMouseMove = function()
    {
        if(
    moveObjects && isMoving)
        {
            for(
    k=0selectArr.lengthk++)
            {
                
    //Move with the mouse now
                
    selectArr[k]._x _root._xmouse selectArr[k].distX;
                
    selectArr[k]._y _root._ymouse selectArr[k].distY;
            }
        }
        
    updateAfterEvent();
    }

    for(
    i=0NUMOFBALLSi++)
    {
        
    //Create the Ball on the stage
        
    this.attachMovie("Ball""Ball" + (i+1), this.getNextHighestDepth());
        
        
    //Update the local text information
        
    this["Ball" + (i+1)].txtBallNumber.text = (i+1);
        
        
    //Move the ball to a randomly generated position - based on stage dimensions
        
    this["Ball" + (i+1)]._x Math.random()*(Stage.width - (this["Ball" + (i+1)]._width +64));
        
    this["Ball" + (i+1)]._y Math.random()*(Stage.width - (this["Ball" + (i+1)]._height +64));
        
        
    //Add this button's functionality
        //ON PRESS
        
    this["Ball" + (i+1)].onPress = function()
        {
            function 
    callback(){minTime getTimer();}
            
    intervalID setInterval(callbackPAHDelay);
            
    moveObjects true;
        }
        
        
    //ON RELEASE
        
    this["Ball" + (i+1)].onRelease this["Ball" + (i+1)].onReleaseOutside = function()
        {
            
    moveObjects false;
            
    isMoving false;
            if (
    minTime PAHDelay
            {
                if(
    this._currentframe == 1)
                {
                    
    this.gotoAndStop(2);
                    
    updateSelectArr(thistrue);
                }
                else
                {
                    
    this.gotoAndStop(1);
                    
    updateSelectArr(thisfalse);
                }
            }
            else
            {
                if(
    deselectOnRelease)
                {
                    for(
    k=0selectArr.lengthk++)
                    {
                        
    selectArr[k].gotoAndStop(1);
                    }
                    
    selectArr = new Array();
                }
            }
            
            
    //Clear the interval and reset the watcher
            
    clearInterval(intervalID);
            
    minTime 0;
        }
    }

    //GLOBAL FUNCTIONS
    function updateSelectArr(objNameaddTo):Void
    {
        if(
    addTo)
        {
            
    selectArr.push(objName);
        }
        else
        {
            
    //Make sure to remove just this named object
            
    for(j=0;selectArr.lengthj++)
            {
                if(
    selectArr[j] == objName)
                {
                    
    selectArr.splice(j,1);
                }
            }
        }

    I hope this works for you, I created it using CS4 and AS2.0.
    Last edited by samac1068; 08-26-2010 at 03:57 PM.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  13. #13
    Member
    Join Date
    Sep 2005
    Posts
    64
    Having a look now. Thanks so much for your input!


  14. #14
    Member
    Join Date
    Sep 2005
    Posts
    64
    Very clever stuff. Impressive to say the least. I'm now going through it and seeing how I can apply the principles to my flash file. Thanks a million!

    I have another question that is related!

    How would I use another button to rotate these selected objects by 90 degrees, around a dynamic axis - ie the centerpoint of the grouped objects?

    I have managed to rotate the objects using

    _rotation += 90;

    But of course this rotates the objects individually around an axis based on the objects top left coordinates....

    Is this very difficult?

    Thanks again...

  15. #15
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    Wow, that is a good question. You are wanting the system to group selected items together and then using a center registration point, rotate them. Hummmm...that may take a minute of trial and error to figure out.

    Just from the little research I've done, this isn't a small feat. This will take some serious math because you're going to have to fake grouping the objects together, determine the center point and then mathematically move each of the object as a simulated single unit, all the while they are separate.

    You may be able to duplicate the clips and move them into another empty movieclips and then when done the rotation, move this out back on the main stage, of course, you'll need to deal with the different X and Y position from global to local and back again. Sounds trying, but a little fun. If I get an opportunity I will try to work on it, maybe someone on this forum and lend a hand.

    Now thinking about it, if you did that, then you could also do it for the function you are using now, the uniform drag function, rather than what I suggested using. I guess it is 6 in one hand half dozen in the other.
    Last edited by samac1068; 03-22-2010 at 08:06 PM. Reason: after thought
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  16. #16
    Member
    Join Date
    Sep 2005
    Posts
    64
    Yes that's the idea.

    It's had me scratching my head for longer than a minute...


  17. #17
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    I had a couple of after thoughts and added to my previous post.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  18. #18
    Member
    Join Date
    Sep 2005
    Posts
    64
    I too thought that you'd need to create a new movieclip to hold the selected objects, and then work out the centerpoint of that clip. But that scared me, as I can't even work out where to begin with this....

    How do you work out the center of an object even, so that you can rotate around that point?


  19. #19
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    Usually when you create and store an object in the library, you can manipulate the registration point which will alter the center point of rotation. I haven't done that dynamically, and you may not need to if you create a movieclip with nothing in it, but the registration point in the center.

    There is possibly a way to do that completely in actionscript, but I off hand I don't know how...sounds like something I should look up.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  20. #20
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813

    Dynamically Adjust Registration Point

    After research, I found there is a property called _xreg and _yreg. I'm trying to figure out if that is available or needed in Actionscript 3.0, but here is a link that will provide some additional information.

    http://www.darronschall.com/weblog/2...n-with-as2.cfm

    Just a note, according to the text this function should also work in AS3.0.
    Last edited by samac1068; 03-24-2010 at 10:23 AM.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center