A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Spinning wheel via mouse drag rotation

  1. #1
    Member
    Join Date
    Oct 2000
    Posts
    79

    Spinning wheel via mouse drag rotation

    I have researched all over and have found some that "kind of" work, but the coding always has issues with CS3 (my version). I am more proficient in AS2, but would be willing to try AS3 if that is the way to go.

    I need a script that will allow the user to use their mouse to drag a wheel that will slow down to a stop. Think "Wheel of Fortune" style. Where it stops will trigger an event.

    Any help will be greatly appreciated...I am pulling my hair out over this one!

  2. #2
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    Hello! Interesting issue...!
    This might come close to what you are looking for (haven't tested it yet):


    var diffY:Number=0;
    var diffX:Number=0;
    var speed:Number=0;
    var desacceleration:Number = 0.95;

    onEnterFrame = function(){
    wheel_mc.onPress = function(){
    var oldX: Number = _xmouse;
    var oldY: Number = _ymouse;
    }
    wheel_mc.onRelease = function(){
    var newX: Number = _xmouse;
    var newY: Number = _ymouse;
    defineMySpin();
    }
    (speed<1? speed=0:speed*=desacceleration);

    wheel_mc._rotation += speed;
    }

    function defineMySpin():Void{
    diffY = newY-oldY;
    diffX = newX-oldX;
    speed = Math.sqrt(diffX*diffX + diffY*diffY);
    }


    Hope it works o
    It might not work if the user starts dragging too far a distance etc...
    Cheers!

  3. #3
    Member
    Join Date
    Oct 2000
    Posts
    79
    Thanks for replying! I just tried your code, but doesn't seem to work for me.

    I did find one that sort of works. The spinning works, but I am trying to figure out how to have the wheel trigger an action depending on where the wheel lands. (a section of the wheel). I threw in a hitTest code, but it isn't working when the wheel STOPS, just constantly during the spinning. Here is the code I'm using:

    Code:
    onClipEvent (load) {
    	wx = getProperty("", _x);
    	wy = getProperty("", _y);
    	Mouse = "up";
    }
    onClipEvent (mouseUp) {
    	Mouse = "up";
    	../rotater:go = "yes";
    	../rotater:w = w;
    }
    onClipEvent (mouseDown) {
    	../rotater:go = no;
    	Mouse = "down";
    	current_rotation = getProperty("", _rotation);
    	if (mouse eq "down") {
    		mx = getProperty("_level0", _xmouse);
    		my = getProperty("_level0", _ymouse);
    		slope = (my-wy)/(mx-wx);
    		theta = Math.atan(slope)*(180/Math.PI);
    		if (mx>wx and my<wy) {
    			ang = current_rotation-theta;
    		} else if (mx<wx and my<wy) {
    			ang = current_rotation-(180-theta)*-1;
    		} else if (mx<wx and my>wy) {
    			ang = current_rotation-180+theta;
    		} else if (mx>wx and my>wy) {
    			ang = current_rotation-(360-theta)*-1;
    		}
    		_level0:theta = theta;
    		_level0:ang = ang;
    	}
    }
    onClipEvent (mouseMove) {
    	if (mouse eq "down") {
    		rot2 = rot1;
    		rot1 = getProperty("", _rotation);
    		w = (rot1-rot2)*5;
    		mx = getProperty("_level0", _xmouse);
    		my = getProperty("_level0", _ymouse);
    		slope = (my-wy)/(mx-wx);
    		theta = Math.atan(slope)*(180/Math.PI);
    		if (mx>wx and my<wy) {
    			setProperty("", _rotation, theta+ang);
    		} else if (mx<wx and my<wy) {
    			setProperty("", _rotation, (180-theta)*-1+ang);
    		} else if (mx<wx and my>wy) {
    			setProperty("", _rotation, 180+theta+ang);
    		} else if (mx>wx and my>wy) {
    			setProperty("", _rotation, (360-theta)*-1+ang);
    		}
    		setProperty("", _xscale, 100);
    		setProperty("", _yscale, 100);
    		_level0:theta = theta;
    		_level0:ang = ang;
    	}
    }
    onClipEvent (mouseMove) {
    	updateAfterEvent();
    	if (rotation<=0) {
    		rotation = 0;
    		_root.clicker.gotoAndStop(1);
    		if (_root.clicker.hitTest(_root.wheel.pies.p1)) {
    			trace("Picked Category 1");
    		} else if (_root.clicker.hitTest(_root.wheel.pies.p2)) {
    			trace("Picked Category 2");
    		} else if (_root.clicker.hitTest(_root.wheel.pies.p3)) {
    			trace("Picked Category 3");
    		}
    	}
    }
    Any more help would be appreciated!

  4. #4
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    you should only do the hittest when the wheel is nog spinning. the variable names are not very clear, but i'm guessing you should put the hittest inside an if(rot2==rot1) or whatever defines if the wheel is spinning.
    good luck on that.

  5. #5
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Just my 2 cents, but some of that (very old) code wont work if you publish AS2 or above player 5 or 6, at least. I suggest you convert all the Flash4 syntax to a more recent/current way of coding. Some expressions are deprecated and will NOT work. For instance:

    ../rotater:w = w; <- Flash4 way of coding variables.
    should be
    _parent.rotator.w = w;

    if (mouse eq "down") <- Flash4 operator
    should be
    if(mouse=="down")

    etc.

    Finally make sure ALL the variables are declared if you publish above player 6.

    gparis

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