dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Convert scrolling background from AS2 to AS3

Hybrid View

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    8

    Convert scrolling background from AS2 to AS3

    Just getting back into flash development here and slowly learning AS3.
    I had a great effect on an old website of mine that I used often of a scrolling background that moved with your mouse.
    This is the coding I came up with years ago to get it to work - the background movie was called 'BackGroundBar' -
    Anyway, any idea how I would even remotely start converting this to AS3?

    Any help would be appreciated, thanks.

    this.onMouseMove = function() {
    constrainedMove(BackGroundBar,4,1);
    };
    function constrainedMove(target:MovieClip, speed:Number, dir:Number) {
    var mousePercent:Number = _xmouse/Stage.width;
    var mSpeed:Number;
    if (dir ==0) {
    mSpeed = 1-mousePercent;
    } else {
    mSpeed = mousePercent;
    }
    target.destX = Math.round(-((target._width-Stage.width)*mSpeed));
    target.onEnterFrame = function() {
    if (target._x == target.destX) {
    delete target.onEnterFrame;
    } else {
    target._x += Math.ceil((target.destX-target._x)*(speed/100));
    }
    };
    }

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    Maybe this might give you some help, it's CS5 low as I can go.
    Mess around with it, there is probably a better way too.

    PHP Code:
    import flash.events.Event;

    stage.addEventListener(MouseEvent.MOUSE_MOVE,constrainedMove(BackGroundBar,4,1));

    function 
    constrainedMove(target:MovieClipspeed:Numberdir:Number):Function
    {
        return function(
    e:MouseEvent):void
        
    {
            var 
    mousePercent:Number mouseX stage.stageWidth;
            var 
    mSpeed:Number;
            if (
    dir == 0)
            {
                
    mSpeed mousePercent;
            }
            else
            {
                
    mSpeed mousePercent;
            }
            
    target.Math.round( - ( (target.width stage.stageWidth) * mSpeed) );
            
    target.addEventListener(Event.ENTER_FRAME,doMove);
            function 
    doMove(e:Event):void
            
    {
                if (
    target.== target.x)
                {
                    
    target.removeEventListener(Event.ENTER_FRAME,doMove);
                }
                else
                {
                    
    target.+= Math.ceil( (target.target.x) * (speed 100) );
                }
            }
        };


  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    You could also do it like so without passing the vars direct to the function, however I'm not sure where you a rwe getting the target.destX value from.

    PHP Code:
    import flash.events.Event;
    import flash.display.MovieClip;

    var 
    target:MovieClip BackGroundBar;
    var 
    speed:Number 4;
    var 
    dir:Number 1;

    var 
    mousePercent:Number;
    var 
    mSpeed:Number;

    stage.addEventListener(MouseEvent.MOUSE_MOVE,constrainedMove);

    function 
    constrainedMove(e:MouseEvent):void
    {
        
    mousePercent mouseX stage.stageWidth;

        if (
    dir == 0)
        {
            
    mSpeed mousePercent;
        }
        else
        {
            
    mSpeed mousePercent;
        }
        
    target.Math.round( - ( (target.width stage.stageWidth) * mSpeed ) );
        
        
    target.addEventListener(Event.ENTER_FRAME,doMove);
    }

    function 
    doMove(e:Event):void
    {
        if (
    target.== target.destX)
        {
            
    target.removeEventListener(Event.ENTER_FRAME,doMove);
        }
        else
        {
            
    target.+= Math.ceil( (target.destX target.x) * (speed 100) );
        }


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