A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: move movieClip within a range

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Posts
    996

    move movieClip within a range

    I have 2 lines that are representing a range that the clip can move within a boundary if you like. My lines are named "left" and "right". What I'm trying to do is move the movieclip based on the mouseX but the mouseX should only move it within the range or boundaries.

    Here is what I have so far.

    Code:
    var cp : Number = left.x + (right.x - left.x) / 2;
    addEventListener(Event.ENTER_FRAME, onLoop);
    var sx : Number = sq.x;
    
    function onLoop( evt : Event ) : void
    {
    	var dx : Number = cp - mouseX;
    	var range = right.x - left.x ;
    	sq.x = dx
    	//trace("dx = "+(dx ));
    }
    I have tried a few different ideas inside the above code but its driving me nuts when it seems like something very easy. The MovieClip should move based on the mouseX but should only move within the left and right clips as boundaries.
    Last edited by ericflash; 05-18-2009 at 07:21 PM.

  2. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    just put there what you said in words. assuming your clip is originally in range, you could wrote it like this:
    Code:
    var dx:Number = cp - mouseX;
    // the mouseX should only move it within the range
    if ((dx > left.x) && (dx < right.x)) {
        // move the movieclip based on the mouseX 
        sq.x = dx;
    }
    who is this? a word of friendly advice: FFS stop using AS2

  3. #3
    Senior Member
    Join Date
    Apr 2001
    Posts
    996
    Thanks realMakc for the reply. I posted this on another forum also and got a great reply. Here is the reply that worked for me in-case anyone else needs this.

    convert the mouse position to a percentage

    flip percentage

    multiply percentage by the distance between both lines (considering registration point)


    Code:
    //left and right lines, remember these must be relative to the registration point of the box
    var lft:Number = 50;
    var rght:Number = 550;
    
    //this assumes the stage is aligned "TL"
    var per:Number = stage.mouseX / stage.stageWidth;
    per = 1 - per;//flip it because you're flipping the direction 0.5 will still yield 0.5
    
    var ix:Number = lft + (rght - lft) * per;
    ix = Math.min( rght, Math.max( lft, ix ) );
    
    myBox.x = ix;
    Thanks again for the help.

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