Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Buttons to Move Frames Forward and Reverse? 1

Status
Not open for further replies.

bsquared18

Technical User
Jun 10, 2001
329
US
Hi,

What I'm seeking should be straightforward for those of you who have mastered ActionScript. (I'm using Flash 8 Professional.)

I have 84 photographs that progressively show one complete rotation of a photographed object (a model airplane). What I'm envisioning are a pair of buttons. When the "forward" button is pressed, the plane would rotate clockwise, and stop rotating when the button is released. Same for the "reverse" button, but the action would be counterclockwise. In that way, the viewer can control exactly what angle to look at.

I assume I'd put each of the photographs on a Flash frame and use ActionScript on the buttons to make Flash go through the frames as described above.

Please let me know what the AS code would be, or perhaps show a link to someplace where it is explained.

Thanks!
Bill
 
at most basic 2 buttons fwd and back

set frame rate to a low value

code on frame 1

stop();
back.onPress = function() {
_root.onEnterFrame = prevFrame;
};
back.onRelease = function() {
_root.onEnterFrame = null;
};
fwd.onPress = function() {
_root.onEnterFrame = nextFrame
};
fwd.onRelease = function() {
_root.onEnterFrame = null;
};

this can be improved upon by checking that currentframe is less than 85 and greater then 1 to allow for loops


alternative is just to have 1 image and rotate it on button press
 
Thanks for the suggestions.

I tried the following suggestion that someone at another forum provided. It seemed to fit my needs. However, while the airplane rotates fine when the forward button is pressed, nothing happens when the backward button is pressed. Below are the steps I followed:

1. I drew a small dot off stage, made it into a movieclip object, and gave it the name apMC.

2. On the movieclip timeline, I created a keyframe for each photo and dragged the photos from the library onto the keyframes. I placed a stop() code on the first and last movieclip frames.

3. On the main timeline, I created three layers:

“Movieclip” Layer: Onto the first frame of this layer I copied/pasted the following code:

__________________________________________

forwardBtn.onPress=function(){
apMC.play();
}
forwardBtn.onRelease=function(){
apMC.stop();
}
backwardBtn.onPress=function(){
this.onEnterFrame=function(){
this._parent.apMC.prevFrame();
}
}
backwardBtn.onRelease=function(){
delete this.onEnterFrame;
}

___________________________________


“Button Labels” Layer.
“Buttons” Layer: I created the forward and backward buttons, giving them the instance names "forwardBtn" and "backwardBtn."

Any thoughts as to why the backward button doesn’t work?

Thanks!
Bill
 
Hmm. I've checked and double checked the instance labels. And the code I used was cut and pasted, so it's exactly as shown above.

Any thoughts as to what a relative newbie with ActionScript like me might have missed that keeps the backward button from working?

I'd be happy to e-mail the .fla file if you like.

Bill
 
Use "trace" for debugging.
Code:
backwardBtn.onPress = function() {
	[b]trace(this);[/b]
	this.onEnterFrame = function() {
		this._parent.apMC.prevFrame();
	};
};
Do you get the output?

Kenneth Kawamoto
 
Kenneth,

I just received an email from the person who provided the code, saying that the backward button needs to be a movieclip object instead of a regular button. When I made the change, that made the button work, although the action stops when the first frame is reached, unlike with the forward button, where the action goes past the last frame for continuous rotation.

I have a query to the developer asking if there's a way to fix this small issue. Any ideas?

Bill
 
> the forward button, where the action goes past the last frame for continuous rotation.

Really? It should stop at frame 1.

Anyway, as for the backward button, this will force the MovieClip to go backwards from the end when it reaches frame 1:
Code:
backwardBtn.onPress = function() {
	this.onEnterFrame = function() {
		var apMC = this._parent.apMC;
		if (apMC._currentframe != 1) {
			apMC.prevFrame();
		} else {
			apMC.gotoAndStop(apMC._totalframes);
		}
	};
};

Kenneth Kawamoto
 
Kenneth,

You're right; my comment "I placed a stop() code on the first and last movieclip frames" would indicate that the action would stop. I forgot that after writing that comment, I changed the code on the last frame to gotoAndPlay(2), so that the clockwise rotation would continue without pause.

I'm not sure how the code you provided fits with what I already have. Does it go at the end? Or does it replace some of the other code? I tried it a few different ways (replacing some code, adding it to the end, etc.) without success.

Just to be clear, what I'm trying to do is have continuous counter-clockwise rotation. Now, when the backward button is pressed and held, the plane rotates counter-clockwise until frame 1 is reached, and then it stops. If the plane is already on frame 1, then no movement occurs. I'd like the counterclockwise motion to continue past frame 1, moving to the final frame in the sequence and then going backwards through the frames in a continuous loop as long as the button is held down.

I wasn't able to have the clockwise motion continue using your code, but maybe I don't understand how it fits with the other code.

Bill
 
Erratum:

What reads:

"I wasn't able to have the clockwise motion continue using your code...."

Should read:

"I wasn't able to have the counter-clockwise motion continue using your code...."

Bill
 
> I changed the code on the last frame to gotoAndPlay(2)

That's the problem. Remove it - you only need "stop();" in the frame 1. You need nothing in the last frame.

Then rewrite your forward button code so that it does the opposite of backward code.


Kenneth Kawamoto
 
Thanks for trying, Kenneth, but my limited knowledge of AS isn't up to the task. I've taken too much of your time already. I'll work it out somehow; maybe live with what the buttons do now, which isn't bad, just not quite right.

Bill
 
Not worry I haven't spend much time!

The complete script would be:
Code:
forwardBtn.onPress = function() {
	this.onEnterFrame = function() {
		var apMC = this._parent.apMC;
		if (apMC._currentframe != apMC._totalframes) {
			apMC.nextFrame();
		} else {
			apMC.gotoAndStop(1);
		}
	};
};
forwardBtn.onRelease = function() {
	delete this.onEnterFrame;
};
backwardBtn.onPress = function() {
	this.onEnterFrame = function() {
		var apMC = this._parent.apMC;
		if (apMC._currentframe != 1) {
			apMC.prevFrame();
		} else {
			apMC.gotoAndStop(apMC._totalframes);
		}
	};
};
backwardBtn.onRelease = function() {
	delete this.onEnterFrame;
};
Also you have to remove your "gotoAndPlay(2)" from apMC as in my previous post.

Kenneth Kawamoto
 
Kenneth,

Please take a look at the .swf files that I've posted on my website at:


The top one, labeled Version A, uses the original code that I listed on 22 Mar 07 at 9:17, with the addition of the gotoAndPlay(2) code on the last frame. Without the gotoAndPlay(2) code, the rotation stops when the stop() code is reached on the first frame, and the button needs to be released and pressed again.

Note that on Version A, the airplane rotates to the left as long as the left button is pressed. The airplane rotates to the right as long as the right button is pressed, until frame 1 is reached; then the action stops for good. Releasing and pressing the button doesn't do anything.

The second image, labeled Version B, uses the code you posted on 22 Mar 07 at 18:04. Was I correct in assuming that when you said "complete script," that this is all the script that is supposed to be included?

In that case, I removed the gotoAndPlay(2) code from the last frame, as you instructed. In this version, the airplane will rotate to the right as long as the right-hand button is pressed. But nothing happens when the left-hand button is pressed.

Hope seeing these images helps to troubleshoot what is going on.

Thanks,
Bill
 
All you need to do is to change your forwardBtn from Button to MovieClip, just like you did on backwardBtn. Button does not have "onEnterFrame" event, that's why forwardBtn isn't working.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top