Avatar of cubical38
cubical38
 asked on

AS3: continially fire a function when mouse is held down?

I need to be able to continue to scroll through a sequence of images when the mouse is held down.  Right now when you click the button it only goes to the nextFrame or prevFram as you can see.  Please review my code and let me know if there is a simpler way of doing this.  

This is a sequence of 24 images that are run through causing the appearance of of a 360 rotation.  Drag works perfectly but what I need to do is be able to click and hold the mouse down and cause the piece to run through the images and if they hit the last frame to continue to rotate through...

Under a tight deadline so any help is greatly appreciated...
stop();
 
import gs.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
try {
	photos.stop();
	pre_mc.y = -300;
	var startX:Number;
	var startFrame:int;
	var changeDistance:int;
	var travelDistance:int;
 
	TweenGroup.allTo([moveLeft_btn, moveRight_btn], .5, {alpha:.5, ease:Strong.easeIn});
 
	moveLeft_btn.addEventListener(MouseEvent.MOUSE_DOWN , moveLeftMouseDown);
	moveLeft_btn.addEventListener(MouseEvent.MOUSE_UP , moveLeftMouseUp);
	//stage.addEventListener(MouseEvent.MOUSE_UP , moveLeftMouseUp);
 
	function moveLeftMouseDown(MouseEvent) {
		photos.gotoAndPlay.nextFrame();
 
	}
	function moveLeftMouseUp(MouseEvent) {
 
	}
 
	moveRight_btn.addEventListener(MouseEvent.MOUSE_DOWN , moveRightMouseDown);
	moveRight_btn.addEventListener(MouseEvent.MOUSE_UP , moveRightMouseUp);
	//stage.addEventListener(MouseEvent.MOUSE_UP , moveRightMouseUp);
 
	function moveRightMouseDown(evt) {
		photos.gotoAndPlay.prevFrame();
 
	}
	function moveRightMouseUp(evt) {
 
	}
 
	moveLeft_btn.buttonMode = true;
 
	moveLeft_btn.addEventListener(MouseEvent.MOUSE_OVER,changeColor);
	moveLeft_btn.addEventListener(MouseEvent.MOUSE_OUT,changedColor);
 
	function changeColor(MouseEvent):void {
		moveLeft_btn.alpha = 1;
	}
	function changedColor(MouseEvent):void {
		moveLeft_btn.alpha = .5;
	}
	moveRight_btn.buttonMode = true;
 
	moveRight_btn.addEventListener(MouseEvent.MOUSE_OVER,changeColor1);
	moveRight_btn.addEventListener(MouseEvent.MOUSE_OUT,changedColor1);
 
	function changeColor1(MouseEvent):void {
		moveRight_btn.alpha = 1;
	}
	function changedColor1(MouseEvent):void {
		moveRight_btn.alpha = .5;
	}
 
	photos.buttonMode = true;
	photos.addEventListener(MouseEvent.MOUSE_DOWN,
	  pressHandler);
 
	function pressHandler(evt:MouseEvent):void {
		startX = photos.mouseX;
		startFrame = photos.currentFrame;
		photos.addEventListener(MouseEvent.MOUSE_MOVE,
		    moveHandler);
		stage.addEventListener(MouseEvent.MOUSE_UP,
		    releaseHandler);
	}
	function releaseHandler(evt:MouseEvent):void {
		photos.removeEventListener(MouseEvent.MOUSE_MOVE,
		    moveHandler);
		stage.removeEventListener(MouseEvent.MOUSE_UP,
		    releaseHandler);
	}
 
	function moveHandler(evt:MouseEvent):void {
		changeDistance = Math.round((photos.mouseX
		    - startX) / 18);
		travelDistance = startFrame + changeDistance;
		if (travelDistance > photos.totalFrames) {
			photos.gotoAndStop(travelDistance %
			      photos.totalFrames);
		} else if (travelDistance < 0) {
			photos.gotoAndStop(photos.totalFrames +
			      (travelDistance % photos.totalFrames));
		} else {
			photos.gotoAndStop(travelDistance);
		}
	}
} catch (error:TypeError) {
	trace("IOTypeError catch: " + error);
} finally {
	trace("Finally!");
}

Open in new window

Adobe FlashWeb Graphics Software

Avatar of undefined
Last Comment
cubical38

8/22/2022 - Mon
scooby_56

on mouse down add an Enter Frame Event
each time the frame-event fires check which button is depressed and scroll appropriately

on mouse up, remove the Enter Frame Event
cubical38

ASKER
Okay cool that worked great, but one more thing.  How do I determine the end of the sequence to return to the first or last frame depending on direction?

I tried and if else, but I dont think I am getting the syntax correct...
Here is my code:




moveLeft_btn.addEventListener(MouseEvent.MOUSE_DOWN , moveLeftMouseDown);
	moveLeft_btn.addEventListener(MouseEvent.MOUSE_UP , moveLeftMouseUp);
	//stage.addEventListener(MouseEvent.MOUSE_UP , moveLeftMouseUp);
 
	function moveLeftMouseDown(MouseEvent) {
		stage.addEventListener(Event.ENTER_FRAME, enterFrame);
	}
	function enterFrame(evnt:Event):void {
		photos.nextFrame();
	}
	function moveLeftMouseUp(MouseEvent) {
		stage.removeEventListener(Event.ENTER_FRAME, enterFrame);
	}
 
	moveRight_btn.addEventListener(MouseEvent.MOUSE_DOWN , moveRightMouseDown);
	moveRight_btn.addEventListener(MouseEvent.MOUSE_UP , moveRightMouseUp);
	//stage.addEventListener(MouseEvent.MOUSE_UP , moveRightMouseUp);
 
	function moveRightMouseDown(MouseEvent) {
		stage.addEventListener(Event.ENTER_FRAME, enterFrames);
	}
	function enterFrames(evnt:Event):void {
		photos.prevFrame();
	}
	function moveRightMouseUp(MouseEvent) {
		stage.removeEventListener(Event.ENTER_FRAME, enterFrames);
	}

Open in new window

ASKER CERTIFIED SOLUTION
scooby_56

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
cubical38

ASKER
Perfect!!!  I was forgetting my cot syntax "photos.currentFrame" ect...  Works like a charm...
http://cubicdev.com/hilti/hiltirotation/hilti360.html
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23