A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: How to make spinning object and easing stop.

Threaded View

  1. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    This doesn't do everything you want, but might help you move in the right direction?
    (i reorganized your code example a bit, then added some things which are explained in the comments)

    Actionscript Code:
    stop();

    import flash.events.MouseEvent;

    star.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick);
    star.addEventListener(MouseEvent.MOUSE_OVER,  onHover);
    stage.addEventListener(MouseEvent.CLICK, clickStage);
    stage.addEventListener(Event.ENTER_FRAME, updateMouse);

    var rotationSpeed:Number = 1; //I set this to zero so it starts with no effect
    var spinDirection:Number = 1; //this is only ever set to "+1" or "-1", and is multiplied against rotationSpeed to determine if we're rotating clockwise or counterclockwise

    function mouseClick (evt:MouseEvent):void{ // So your star MC has frames? that you want to play?  (if not, then i'd delete this function altogether)
    evt.target.play();  //added "evt.currentTarget" to try and give Flash some idea of what should be playing.
    }

    function onHover(evt:MouseEvent):void{ // when you rollover the star, change direction of spin
    spinDirection = spinDirection * -1; //toggles negative to positive, or vice versa
    }

    function clickStage(event:MouseEvent):void{ //this happens anytime you click ANYWHERE on stage...
    rotationSpeed = 10; //everytime you click anywhere, this "speed of spinning" is set  to it's maximum.
    //...might want to use larger number than 10
    }
    function updateMouse(event:Event):void { //this happens EVERY frame no matter what... so, poorly named function?
    star.rotation += rotationSpeed * spinDirection;
    if(rotationSpeed >0)  //this check will keep you from ever going below 0
    rotationSpeed--; //this way the speed of spinning will fall off over time.
    }

    * you mentioned wanting to speed up when the star is clicked, then ease off. but the quick code above would not do this exactly. it would just start at top speed, and slow down every frame, when you click anywhere in the stage.

    (you could add another variable, maybe a boolean called "rising", which is checked to decide if you are speeding up, or easing off. in your ENTER_FRAME handler, you could add to rotationSpeed if rising is true, or subtract from rotationSpeed if rising is false. you could also have an if statement that says when rotationSpeed reaches a certain number, then switch rising from true to false. you'd also need to reset rising to true when you click to start the spin)

    * might be worth looking into the tween() function.

    hope this helps.
    Last edited by warrenEBB; 02-23-2012 at 04:30 PM.

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