A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [F8] MC snapping to a x Coord.

  1. #1
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003

    [F8] MC snapping to a x Coord.

    Hi all, I've got a problem with a movie I'm working on.. basically there's a mc that is a rating guide, let's say -10 to +10. I'm trying to get it so that an mc inside the main mc snaps to a _x value, depending on where the user releases the mouse. Here's an example of code that I have.

    this.onPress = function() {
    awrRate.startDrag(true, -143, 0, 143, 0)

    awrRate.onRelease = onReleaseOutside = function() {
    if((awrRate._x - awrRate._width/2) <= -136.1 || (awrRate._x - awrRate._width/2) >= -120.5) {
    setProperty(awrRate, _x, -128.5);


  2. #2
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Leeds, UK

    Your syntax is wrong, and I think you've gone about this the wrong way. Here's the easier way to do it.


    awrRate.onRelease = function() {

    //First of all, we divide by 143 then multiply by 10;
    var x = ( this._x + (this._width/2) ) / 143 * 10;
    //This gives us a number in the range -10 to 10. Now we
    //round to the nearest integer;
    x = Math.round(x);
    //To finish, we reverse what did the first time to get
    //our number back in the range -143 to 143, except that
    //since we have rounded we only get certain values;
    x = x / 10 * 143;

    //We don't use the setProperty function anymore... it's deprecated;
    this._x = x - this._width / 2;

    awrRate.onReleaseOutside = awrRate.onRelease;

    I'm not sure whether you want to add or subtract the width - this will depend on the registration point of your clip (to save yourself hassle, change the reg. point to the center, and then you don't have to mess with the width at all).

    The script will now automatically set your snap points at even intervals between -143 and 143.

    To see how this might work, imagine that the user releases the clip at _x = -127. We then divide by 143 and multiply by 10 to get x = -8.88... . We then round to the nearest number, so now x = 9. We then reverse the operation, so we do x * 143 / 10 to get x = -128.7.

    The problem with syntax is that you forgot to put awrRate.onReleaseOutside. Generally, it's also good practice to only put one '=' sign on any given line, so


    a = b = 1;

    is considered bad practice (in some circles). Also, you should have used the 'this' keyword, rather than the name of the movieclip.

    Hope this helps.
    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

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