A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: AS3 zoom and drop/drag movieClip problem

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    Question AS3 zoom and drop/drag movieClip problem

    I have an aerial photograph which takes up the whole window and is a movieClip called map_mc.

    I have two listeners that mean the user can drag the image around in the window to see more of it. I also have a zoom button which makes the movieClip bigger and therefore allows the user to zoom into a section and drag it around within the window to see different parts much like google maps.

    My problem is the user can drag the movieClip too far exposing the white background. I added a bit of code to stop the user dragging it too far down using 0,0 top left corner but I can do this for the bottom right as the movieClip changes sizes and so isn't a constant.

    Any ideas how I could stop this from happening? my code is below

    Actionscript Code:
    import flash.events.Event;

    zoom20.addEventListener(MouseEvent.CLICK,zoomIn);
    back.addEventListener(MouseEvent.CLICK,backOut);
    map_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    map_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
    stage.addEventListener(Event.ENTER_FRAME,noWhite);


    //var rectangle:Rectangle = new Rectangle(0,0,1920,1080);

    function zoomIn(e:MouseEvent):void
    {

        map_mc.scaleX +=  .2;
        map_mc.scaleY +=  .2;

    }
    function backOut(e:MouseEvent):void
    {

        map_mc.width = 1920;
        map_mc.height = 1275.50;
        map_mc.x = 0;
        map_mc.y = -180.10;

    }

    function drag(e:MouseEvent)
    {

        e.target.startDrag();
    }

    function drop(e:MouseEvent)
    {

        e.target.stopDrag();
    }



    function noWhite(e:Event)
    {

        if (map_mc.y > 0)
        {
            map_mc.y = 0;
        }
       
        if (map_mc.x > 0)
        {
            map_mc.x = 0;
        }
    }

    //()

    I've done a similar thing with a rectangle zone before but that was keeping an object within the zone on stage where as with this I have things in and out of the zone.

  2. #2
    it would be in the no white function, just add the following and see how that works.

    you could also set the limits on the startDrag, I believe it takes a rectangle that would be something like (0,0,-map_mc.width + stage.stageWidth, -map_mc.height + stage.stageHeight)

    function noWhite(e:Event)
    {

    if (map_mc.y > 0)
    {
    map_mc.y = 0;
    }

    if (map_mc.x > 0)
    {
    map_mc.x = 0;
    }

    if (map_mc.y < -map_mc.height + stage.stageHeight)
    {
    map_mc.y = -map_mc.height + stage.stageHeight;
    }

    if (map_mc.x < -map_mc.width + stage.stageWidth)
    {
    map_mc.x = -map_mc.width + stage.stageWidth;
    }

    }

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    sorry that didn't work

    I tried adding those extra if statements to the function but it didn't change anything you can still drag it too far.

    so then I tried the rectangle which I'd tried before but differently and it just means that when you go to try and start the drag it moves the movieclip to 0,0 and then wont move at all.

    sorry thanks for your help!

  4. #4
    the rectangle is the bounding box for the drag, maybe switch the values around..

    new Rectangle(-map_mc.width + stage.stageWidth, -map_mc.height + stage.stageHeight, map_mc.width, map_mc.height)

  5. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    sorry that didn't work either... I think the rectangle variable doesn't work very well with off screen rectangles perhaps...

    I did think there would be a way using variables to make the noWhite function work?

    perhaps storing the values of the map_mc's width and height every time the plus button is clicked and then recalling that later in the if statements? any thoughts?

  6. #6
    does map_mc change ever?

  7. #7
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    yes there is a zoom button which makes map_mc get bigger every time its clicked.

    I've managed to get it to not show the white but it snaps back to 0,0 each time heres my code

    Actionscript Code:
    import flash.events.Event;

    zoom20.addEventListener(MouseEvent.CLICK,zoomIn);
    back.addEventListener(MouseEvent.CLICK,backOut);
    //var rectangle:Rectangle = new Rectangle(-(map_mc.width + stage.stageWidth), -(map_mc.height + stage.stageHeight), map_mc.width, map_mc.height);
    map_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    map_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
    stage.addEventListener(Event.ENTER_FRAME,noWhite);

    var right:uint = stage.stageWidth;
    var bottom:uint = stage.stageHeight;


    function zoomIn(e:MouseEvent):void
    {

        map_mc.scaleX +=  .2;
        map_mc.scaleY +=  .2;

    }
    function backOut(e:MouseEvent):void
    {

        map_mc.width = 1920;
        map_mc.height = 1275.50;
        map_mc.x = 0;
        map_mc.y = -180.10;

    }

    function drag(e:MouseEvent)
    {

        map_mc.startDrag();
        //map_mc.startDrag(true, rectangle);
    }

    function drop(e:MouseEvent)
    {

        map_mc.stopDrag();
    }



    function noWhite(e:Event)
    {

        if (map_mc.y > 0)
        {
            map_mc.y = 0;
        }

        if (map_mc.x > 0)
        {
            map_mc.x = 0;
        }
        if ((map_mc.y + map_mc.height) <  bottom)
        {
            map_mc.y  =   (bottom + map_mc.height);
        }

        if ((map_mc.x + map_mc.width) <    right)
        {
            map_mc.x  =    (right + map_mc.width);
        }
    }

  8. #8
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    in my code window in flash it doesn't highlight "right" in blue? so i'm not sure why this is doing that!

  9. #9
    one thing is that sometimes stage.stageWidth and stage.stageHeight are 0 at the beginning of a movie, i usually limit to flash version 9,0,115 and add the following:

    var right:int;
    var bottom:int;

    addEventListener(Event.ADDED_TO_STAGE, onAdded);

    function onAdded(e:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, onAdded);
    right = stage.stageWidth;
    bottom = stage.stageHeight;

    // call other functions here
    }

  10. #10
    Junior Member
    Join Date
    Sep 2009
    Posts
    22

    Thumbs up

    thanks

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