dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: AS3 rotator / multi spin animationn controller - PROGRAMMING CHALLENGE

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2

    AS3 rotator / multi spin animationn controller - PROGRAMMING CHALLENGE

    Hi all

    This is fast becoming the biggest waste of my time ever!

    So im opening it up to the commuity as a programming challenge!!

    For something which i assumed would be very simple, writting a robust and solid multi turn spinner handle for flash in as3 has become the bane of my life.


    Feel free to download my attempts so far (attached), which cover the basics of a single rotation, and make it look like im further ahead than i actually am.


    The difficulty lies with the very specific things I personally need the spinner to do,and the fact its SO HARD to explain...


    Its actually used to control a piece of animation that is 72 frames long.

    I need the first "lap" of rotation to send the playhead to frames in the 1-36 range and the second rotation to send it to frames 37-72. So two full spins would take it to the final frame, one full spin would go half way to frame 36.

    problem is the rotator handle needs to be locked at the initial position, so a user can't drag it backwards to start with but can drag it backwards once theyve moved forwards. (think of turning on a hot water tap, many turns in one direction until it stops fully on, then the same turns back again to reach the original start position of fully off)

    I also need the rotator to be able to be used in anti clockwise and clockwise modes.

    The ideal is to be able to set the total number of animation frames to be controlled and the number of rotations required to reach the final frame, then have the spinner work everything else out for me.

    If anyone can solve this and provide me with a working example then i will be eternally grateful.

    Many thanks in advance!

    andi.
    Attached Files Attached Files

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Have you looked at minimalcomps? He has a generic knob widget which you may be able to use or modify.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    thanks for the link but unfortunately i've made more headway than minimalcomps' knob component.

    I need to be using aTan and rotations rather than the methods he is using.

    Thanks for the tip tho some other nice stuff there..

    Anyone else got anything?

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Guessing you've already figure it out, but I needed to do something similiar...

    Modified your original file, repositioning the grips in relationship to the registration point. Code shown below.
    Code:
    stop();
    // total number of degrees of rotation
    var limVal=840;
    // starting direction for rotation 'CW' or 'CCW'
    var theDir:String="CCW";
    // total number of frames you want to use (usage not part of this package)
    var animFrames:uint=36;
    //
    var minVal:int;
    var maxVal:int;
    var newPos:Number;
    var newAngle:Number;
    var oldAX:int;
    var oldAY:int;
    var newAX:int;
    var newAY:int;
    var basePos:int;
    var spins:uint;
    var goFrame:int;
    var baseVal:int;
    var tooMuch:Boolean=false;
    var animStep:uint=Math.floor(limVal/animFrames+1);
    
    function setupSpinner():void {
    	oldAX=0;
    	oldAY=0;
    	basePos=0;
    	if (theDir=="CW") {
    		handle.grip.gotoAndStop(1);
    		minVal=0;
    		maxVal=limVal;
    		baseVal=Math.floor(limVal/360)*360;
    	} else {
    		handle.grip.gotoAndStop(2);
    		minVal=0-limVal;
    		maxVal=0;
    		baseVal=Math.floor(limVal/360)*360*-1;
    	}
    	handle.rotation=0;
    	handle.grip.addEventListener(MouseEvent.MOUSE_DOWN, gripDown, false, 0, true);
    	dirBut.addEventListener(MouseEvent.MOUSE_DOWN, swapDir, false, 0, true);
    	resetBut.addEventListener(MouseEvent.MOUSE_DOWN, resetAll, false, 0, true);
    	box1.text=box2.text=box3.text=box4.text=box5.text=box6.text="";
    }
    function resetAll(e:MouseEvent):void {
    	handle.grip.removeEventListener(MouseEvent.MOUSE_DOWN, gripDown);
    	dirBut.removeEventListener(MouseEvent.MOUSE_DOWN, swapDir);
    	resetBut.removeEventListener(MouseEvent.MOUSE_DOWN, resetAll);
    	//gotoAndPlay(25);
    	setupSpinner();
    }
    function swapDir(e:MouseEvent):void {
    	theDir=(theDir=="CW")?"CCW":"CW";
    	setupSpinner();
    }
    function gripDown(e:Event):void {
    	if (tooMuch) {
    		tooMuch=false;
    		basePos=baseVal;
    	}
    	handle.grip.removeEventListener(MouseEvent.MOUSE_DOWN, gripDown);
    	stage.addEventListener(MouseEvent.MOUSE_MOVE, mMove, false, 0, true);
    	stage.addEventListener(MouseEvent.MOUSE_UP, mUp, false, 0, true);
    }
    
    function mUp(e:Event):void {
    	stage.removeEventListener(MouseEvent.MOUSE_UP,mUp);
    	stage.removeEventListener(MouseEvent.MOUSE_MOVE, mMove);
    	handle.grip.addEventListener(MouseEvent.MOUSE_DOWN, gripDown, false, 0, true);
    }
    
    function mMove(e:Event) {
    	newAngle=Math.atan2(mouseY-handle.y,mouseX-handle.x);
    	newAX = (Math.cos(newAngle)<=0) ? 0-1 : 1;
    	newAY = (Math.sin(newAngle)<=0) ? 0-1 : 1;
    	if (newAY!=oldAY) {
    		if (newAY<oldAY&&newAX==1) {
    			basePos-=360;
    		} else if (newAY>oldAY && newAX==1) {
    			basePos+=360;
    		}
    	}
    	newPos=newAngle*180/Math.PI;
    	if (theDir=="CW") {
    		newPos = (newPos<0) ? 360+newPos+basePos : newPos+basePos;
    		spins = (basePos<0) ? 0 : Math.floor(basePos/360);
    		if (basePos>baseVal) {
    			tooMuch=true;
    		}
    	} else {
    		newPos = (newPos>0) ? -(360-newPos)+basePos : newPos+basePos;
    		spins = (basePos>0) ? 0 : Math.floor(basePos/360)*-1;
    		if (basePos<baseVal) {
    			tooMuch=true;
    		}
    	}
    	spins = (spins > limVal/360) ? Math.floor(limVal/360) : spins;
    	if (newPos<minVal) {
    		newPos=minVal;
    	} else if (newPos>maxVal) {
    		newPos=maxVal;
    	}
    	oldAX=newAX;
    	oldAY=newAY;
    	handle.rotation=newPos;
    	goFrame=Math.floor(Math.abs(newPos)/animStep)+1;
    	box1.text="Rotation Degrees = "+Math.abs(Math.floor(newPos%360));
    	box2.text="goFrame = "+goFrame;
    	box3.text="cum Rot = "+Math.abs(Math.floor(newPos));
    	box4.text="Direction = "+theDir;
    	box5.text="spins Done = "+spins;
    	e.updateAfterEvent();
    	if (tooMuch) {
    		mUp(e);
    	}
    }
    // -----------------------------------------------
    setupSpinner();
    // -----------------------------------------------
    Last edited by dawsonk; 09-14-2010 at 04:40 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