2

I'm new to AS3 and have been working on an XML driven navigation system written in AS3.

At present, I've imported the contents of an XML file and plotted it inside a containing MovieClip created at root level dynamically on the stage. This MovieClip is called 'container'.

What I want to accomplish is a smooth, accelerating / decelerating effect which animates the container movieclip along the X axis depending on where the mouse cursor is in relation to the middle of the stage.

My code can be found here: http://pastie.org/521432

Line 87 onwards is the code I'm using right now to make the movieclip scroll left & right.

What I have does work but is clunky but does work - I just want it to be a little more polished and have drawn a blank with Google. Because I want the MovieClip to continue to scroll at the current relative speed even when the mouse stops moving, I used an instance of the Timer class.

Can anyone suggest improvements? Thanks in advance.

BeesonBison
  • 1,053
  • 1
  • 17
  • 27

1 Answers1

4

You should separate out you calculations and your drawing methods. So have it do all the calculations in an onMouseMove handler, but actually draw the changes in an onEnterFrame handler.

Also I think your algorithm could be much simpler and nobody would notice. I made a quick example of how you might do it. paste this code into an AS3 file called Main.as and make it the document class of a new FLA.

package 
{
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {
        private const boxCount:int = 10;
        private const boxWidth:int = 45;
        private const boxMargin:int = 5;
        private const startPoint:int = 150;
        private const boxesWidth:int = boxCount * (boxWidth + boxMargin);
        private const endPoint:int = boxesWidth + startPoint;
        private const zeroPoint:int = boxesWidth / 2 + startPoint;

        private var container:MovieClip;
        private var targetX:Number;
        private var speed:Number = 0;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            container = new MovieClip();
            addChild(container);
            container.x = 150;
            container.y = 300;
            for (var i:int = 0; i < boxCount; i++) 
            {
                container.graphics.beginFill(Math.random() * 0xFFFFFF);
                container.graphics.drawRect(i*(boxWidth+boxMargin), 0, boxWidth, boxWidth);
            }

            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        }

        private function mouseMoveHandler(e:MouseEvent):void 
        {
            var distanceFromCenter:int = stage.mouseX - zeroPoint;
            speed = distanceFromCenter * -0.01; // Bring number into a good range, and invert it.
        }

        private function enterFrameHandler(e:Event):void 
        {
            container.x += speed;
        }
    }
}
Adam Harte
  • 10,369
  • 7
  • 52
  • 85
  • Thanks very much for this code. It works really well! I've not used the DocumentClass setting before - I love the way you can separate out the ActionScript from the FLA. I'm tweaking the code at the moment - the only issue I'm experiencing is with the zeroPoint and distanceFromCenter calculations... it seems that scrolling left works well, but if I add more boxes and try to scroll right, the speed does not increase? – BeesonBison Jul 10 '09 at 08:46
  • Actually ignore the above, I've fixed it - I changed the declaration of 'const zeroPoint' (line 16) to: private const zeroPoint:int = stage.stageWidth / 2 + startPoint – BeesonBison Jul 10 '09 at 09:23
  • How could this code be modified so that it performs like a carousel - automatically drawing the first box after it has scrolled up to the last box? Could this also be made to automatically scroll when the mouse isn't inside the stage? – BeesonBison Jan 10 '11 at 15:49
  • I've managed to do this! Probably not the best way I know, but please check out my code: http://pastie.org/579689 – BeesonBison Jan 10 '11 at 15:49