Cat
Cat

Reputation: 29

how to stop a rotating movieclip at different angles with other movieclips following, actionscript 3

I have a manometer, this needs to spin from a minimum value to a maximum. right now I have the manometer as a picture and the arrow as a movieclip. I've got it spinning at the right speed, but don't know how to stop it at the lowest/highest pressure.

It's suppose to work like this:

I have two movieclip/buttons; one for simulating more pressure, and one for less. when the user presses the "more pressure" movieclip/button the pressure begins to rise and the arrow inside the manometer begin to turn. At the same time as the pressure rises, another movieclip ("stamp") will push uppwards.

then another movieclip/button, "less pressure" simulates pressure drop; when pressed, the same point as the arrow stopped at when pressure rised, will start sinking towards minimum, and the stamp will go down again.

so, when the user presses "more pressure" pressure rises towards maximum, and as soon as the user stop pressing the button, the animation stops (both the stamp and the arrow). And if the user presses "lower pressure", the arrow starts sinking from where it stopped.

heres my code so far: pil1 = manometerarrow, the stamp = stamp, and "less pressure"/"more pressure" = Lpress / mpress

addEventListener (Event.ENTER_FRAME, rotate);
function rotate(event:Event):void
{
  pil1.rotation = pil1.rotation+1;
}

Upvotes: 1

Views: 806

Answers (2)

net.uk.sweet
net.uk.sweet

Reputation: 12431

ymutlu is on the right track. The mouse down event will only execute once when the mouse is depressed. To make the object rotate continuously while the mouse is depressed you need to increment or decrement the rotation of the object on each frame. I think the following should do the trick:

import flash.events.MouseEvent;
import flash.events.Event;

var rotate = 0;

Hpress.addEventListener(MouseEvent.MOUSE_DOWN, Hpressed); 
Hpress.addEventListener(MouseEvent.MOUSE_UP, removeEnterFrame);
Lpress.addEventListener(MouseEvent.MOUSE_DOWN, Lpressed);
Lpress.addEventListener(MouseEvent.MOUSE_UP, removeEnterFrame);

function Hpressed(e:MouseEvent):void 
{ 
    rotate = 1; 
    addEnterFrame();
} 

function Lpressed(e:MouseEvent):void 
{ 
    rotate = -1;
    addEnterFrame();
}

function addEnterFrame():void
{
    this.addEventListener(Event.ENTER_FRAME, update);
}

function removeEnterFrame(e:MouseEvent):void
{
    this.removeEventListener(Event.ENTER_FRAME, update);
}

function update(e:Event):void
{
    pil1.rotation += rotate;
} 

Upvotes: 1

ymutlu
ymutlu

Reputation: 6715

hold to varaible that states if max button down or min button down, and check it in enterframe loop. Edited answer on your comment, hope you can sort it out.

addEventListener (Event.ENTER_FRAME, rotate);
function rotate(event:Event):void
{
   if(isMaxDown) // true when max button down
     pil1.rotation = Math.min(presMax,pil1.rotation+1);  // presMax is max value that pressure can go
   if(isMinDown) // true when min button down
     pil1.rotation = Math.max(presMin,pil1.rotation-1);// presMin is min value that pressure can go
}

// isMaxDown and isMinDown are global values.
Hpress.addEventListener(MouseEvent.MOUSE_DOWN, Hpressed);
Lpress.addEventListener(MouseEvent.MOUSE_DOWN, Lpressed); 
Hpress.addEventListener(MouseEvent.MOUSE_UP, H_up);
Lpress.addEventListener(MouseEvent.MOUSE_UP, L_up); 
function H_up(e:MouseEvent):void {
 isMaxDown=false;
} 
function L_up(e:MouseEvent):void {
 isMinDown=false; 
} 
function Hpressed(e:MouseEvent):void {
 isMaxDown=true;
} 
function Lpressed(e:MouseEvent):void {
 isMinDown=true; 
} 

This code would help you but prob this is not a path to fallow to do somthing like that.

Upvotes: 0

Related Questions