A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: click and drag rotation (similar to qtvr)

  1. #1
    Senior Member
    Join Date
    Oct 2009
    Posts
    112

    click and drag rotation (similar to qtvr)

    I have an array of dials (like an oven knob). when you click one, you can drag up-down or left-right to make it rotate back and forth
    HOW IS IT DOING THIS?
    each dial basically a movie clip with a series of 20 or so frames, each with a different image. as you click and drag the mosue over it, it SCROLLS through the frames.
    Code:
    var KNOBarray:Array=new Array(knob1,knob2,knob3);
    
    var currentKnob:MovieClip;
    for (var iKNOB:uint; iKNOB< KNOBarray.length; iKNOB++) {
        var KNOB:MovieClip=getChildByName(KNOBarray[iKNOB]) as MovieClip;
       	KNOB.buttonMode = true;
    	KNOB.addEventListener(MouseEvent.MOUSE_DOWN, knobPRESS);
    	KNOB.stop();
    }
    
    function knobPRESS(e:MouseEvent):void {
    	currentKnob=e.currentTarget as MovieClip;
     	 startX = currentKnob.mouseX;
     	 startY = currentKnob.mouseY;
     	 startFrame = currentKnob.currentFrame;
     	 stage.addEventListener(MouseEvent.MOUSE_MOVE, knobMOVE);
     	 stage.addEventListener(MouseEvent.MOUSE_UP, knobRELEASE);
    }
    
    function knobRELEASE(evt:MouseEvent):void {
    	  stage.removeEventListener(MouseEvent.MOUSE_MOVE, knobMOVE);
    	  stage.removeEventListener(MouseEvent.MOUSE_UP, knobRELEASE);
    }
    
    function knobMOVE(evt:MouseEvent):void {
    	turning=true;
    	directionANDspeed = Math.round(((currentKnob.mouseX - startX)+(currentKnob.mouseY-startY))/15);
    	travelDistance = startFrame + directionANDspeed;
        if (travelDistance > currentKnob.totalFrames) {
        	currentKnob.gotoAndStop(travelDistance % currentKnob.totalFrames);
      } else if (travelDistance < 0) {
    		currentKnob.gotoAndStop(currentKnob.totalFrames +(travelDistance % currentKnob.totalFrames));
      } else {
    	currentKnob.gotoAndStop(travelDistance);
      } 
    }
    i am also using a system of binary sockets like so:
    to send information: socket.writeUTFBYTES("sent information");
    torecieveinformation:socket.readUTFBytes(bytesAvailable){
    ("process recieved information");
    }
    i know it looks complicated but it's not, its just like using "trace"
    My question is is there a way to send out a string everytime the dial hits a new frame? for example, when someone turns the dial to the right i want it to write (or trace) : "+"
    and when someone turns the dial to the left i want it to write (or trac): "-"

    i can figure out how to read the readUTFBytes so thats no problem, i jsut have no idea how to tell it to send out a plus sign everytime it's turned to the right, and a minus sign everytime it's turned to the left
    let me know if you need more explanation

  2. #2
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    im rather confused, you know how to use sockets, but you dont understand basic data transfers :S for one, why are you using an image for every angle of the dial? flash comes with rotation tweening you know.

    flos

    ps
    Last edited by cancerinform; 12-01-2009 at 06:24 PM.

  3. #3
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    when i say its like using trace, i mean just imagine saying

    Code:
    "s.writeUTFBytes("+");"
    would be the same as saying

    Code:
    "trace("+");"
    in terms of the effect it would have on my program

    i said that because im guessin a lot of ppl dont understand binary sockets. and yea i can use binary sockets cause i figured it out, but i dont know basic data transfer cause im new to AS3

    anyone got any comments that might help thanks. let me know specifically if i can clarify on anything else

    Also I chose to scroll through images rather than motion tweening for two reasons.
    1st motion tweening is easy when you want an image to sit on the stage and spin endlessly. NOT so easy when you want to control the rotation with a mouse click. I found this difficult, so i chose scrolling through images because i foudn a good tutorial for it
    2nd when i scroll through images, i can also use my binary sockets to tell flash to jump to a certain frame on the DIAL's TIMELINE, rather than having to worrya bout x/y positions on the stage
    Last edited by mattwatts15; 12-01-2009 at 05:48 PM. Reason: tweening response

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Basically, you're already determining when the knob was moved, and which direction within your knobMOVE function. I believe the sign of the variable distanceANDspeed will be the thing to send.
    Code:
    if (distanceANDspeed > 0){
      s.writeUTFBytes("+");
    }else{
      s.writeUTFBytes("-");
    }
    Why aren't you rotating a static image instead of using multiple discrete frames? It would be smoother, and probably conceptually simpler.

  5. #5
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    thanks thats half-working

    for some reason though when i turn the dial counter clockwise, it doesnt output a "-" everytime, it only starts to ouput the "-" after it crosses the 0 frame..

    so i would need to say somethin like, if nextFrame is greater than prevFrame, or if nextFrame is less than prevFrame...only i dont think i can write it that way, because nextFrame only means "the next Frame continuing in the sequence", not "whichever frame is hit next"
    make sense?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It should write something every time through using that code. Either it's greater than 0 or it's not. Do you mean that it sometimes outputs a + when you expect a -? Have you tried starting from a frame other than 0? The way it is now, it will only output relative to the drag start position, not the last position. To determine the position relative to the last, you would have to save the mouse coordinates each time through the move function and compare them to the previous ones.

  7. #7
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    right....how do you save a mouse position

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Create variables in a scope larger than the function. This could be lastx:Number, lasty:Number. Or you could put them in a Point object.

    Code:
    var KNOBarray:Array=new Array(knob1,knob2,knob3);
    
    var currentKnob:MovieClip;
    var lastx:Number;
    var lasty:Number;
    var turning:Boolean = false; //you reference this, but it's not defined in your code.
    
    for (var iKNOB:uint; iKNOB< KNOBarray.length; iKNOB++) {
        var KNOB:MovieClip=getChildByName(KNOBarray[iKNOB]) as MovieClip;
       	KNOB.buttonMode = true;
    	KNOB.addEventListener(MouseEvent.MOUSE_DOWN, knobPRESS);
    	KNOB.stop();
    }
    
    function knobPRESS(e:MouseEvent):void {
    	currentKnob=e.currentTarget as MovieClip;
     	 startX = currentKnob.mouseX;
     	 startY = currentKnob.mouseY;
     	 startFrame = currentKnob.currentFrame;
     	 stage.addEventListener(MouseEvent.MOUSE_MOVE, knobMOVE);
     	 stage.addEventListener(MouseEvent.MOUSE_UP, knobRELEASE);
    }
    
    function knobRELEASE(evt:MouseEvent):void {
    	  stage.removeEventListener(MouseEvent.MOUSE_MOVE, knobMOVE);
    	  stage.removeEventListener(MouseEvent.MOUSE_UP, knobRELEASE);
              turning = false;
    }
    
    function knobMOVE(evt:MouseEvent):void {
    	var directionANDspeed:int  = Math.round(((currentKnob.mouseX - startX)+(currentKnob.mouseY-startY))/15);
            var whichDirection:int;
            if (turning){
              whichDirection = Math.round(((currentKnob.mouseX - lastX)+(currentKnob.mouseY-lastY))/15);
            }else{
              whichDirection = directionANDspeed;
            }
    	turning=true;
    	travelDistance = startFrame + directionANDspeed;
        if (travelDistance > currentKnob.totalFrames) {
        	currentKnob.gotoAndStop(travelDistance % currentKnob.totalFrames);
      } else if (travelDistance < 0) {
    		currentKnob.gotoAndStop(currentKnob.totalFrames +(travelDistance % currentKnob.totalFrames));
      } else {
    	currentKnob.gotoAndStop(travelDistance);
      } 
      lastx = currentKnob.mouseX;
      lasty = currentKnob.mouseY;
    }

  9. #9
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    turning:Boolean is referenced in the knobMOVE function.

    i'm confused what you did with my code...i fixed the errors in it but now the dials arent' moving at all. i went through line by line to compare what u changed but i codulnt figure it out, cause u got rid of some of my vars. can u explain what u did? i see where ur goin with the lastx adn lasty vars, but u also took out the parts that said s.writeUTFBytes("+");
    sorry i think u wanted me to figure it out but im lost..

  10. #10
    Senior Member
    Join Date
    Oct 2009
    Posts
    112
    im not sure if my last post showed up so here it is again

    turning:Boolean is referenced in the knobMOVE function.

    i'm confused what you did with my code...i fixed the errors in it but now the dials arent' moving at all. i went through line by line to compare what u changed but i codulnt figure it out, cause u got rid of some of my vars. can u explain what u did? i see where ur goin with the lastx adn lasty vars, but u also took out the parts that said s.writeUTFBytes("+");
    sorry i think u wanted me to figure it out but im lost..

Tags for this Thread

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