I would like to create a simple fast forward and rewind button. I am getting sporadic results with the rewind button. I would like the playhead to skip 3 seconds (72 frames) at a time when the user hits the fast forward or rewind button. They should be able to hit either buttons repeatedly. If playhead is under 3 seconds then it will go to the first frame.
Here's the code:
var FrameNumRewind:Number = this.currentFrame;
FrameNumRewind = 0
stage.addEventListener(MouseEvent.MOUSE_DOWN, page_watch);
function page_watch(event:MouseEvent):void
{
if (event.target == bnt_Fast)
{
var FrameNumFast:Number = this.currentFrame;
this.gotoAndPlay(FrameNumFast +=72);
}
if (event.target == bnt_Rewind)
{
//Will take the frame head to the first frame if less than 73 frames.
if(FrameNumRewind <=73)
{
SoundMixer.stopAll();
this.gotoAndPlay("1");}
}
else
{
this.gotoAndPlay(FrameNumRewind-=72);
}
}
What am I doing wrong? Is there a better way to do this?
I would appreaciate any help or any references.
You do not need to be using the two "Frame..." variables. You can use the currentFrame property instead. When you use specify this.gotoAndPlay("1"); you do not use quotation marks because the argument is a numeric value, not a string. You should assign the listeners to the buttons instead of the stage. Let each have its own event handler function. When you press down on the button assign an ENTER_FRAME listener to call a function for changing the frames, and when you release it, remove that ENTER_FRAME listener.
Ned,
Thanks for your response. But I am a bit confused by your response. Below is my interpretation of your response. There are still errors though but I don't know how to solve them. I only tested this for the fast foward button.
bnt_Fast.addEventListener(MouseEvent.MOUSE_DOWN, fastForwarda);
function fastForwarda (event:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, ff72);
function ff72 (event:Event):void
{
this.gotoAndPlay(this.currentFrame +=72);
}
}
bnt_Fast.addEventListener(MouseEvent.MOUSE_UP, fastForwardb);
function fastForwardb (event:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, ff72);
}
What am I doing wrong?
One thing you should take care of is that you should not nest named functions within other functions. Pull that ff72 function out by itself.
The currentFrame property is a read-only property, but you are trying to change its value using code in this line...
this.gotoAndPlay(this.currentFrame +=72);
So change that line to be
gotoAndPlay(currentFrame+72);
noticing that I took the "this." references out as well. They are more often unnecessary in AS3 scenarios.
Thanks Ned, it worked!
I also added a feature that will prevent the playhead from going to another scene if the current frame is <= 72 ( the number of frames it rewinds).
Here's the code for anyone's reference.
//Fastforward button code.
bnt_Fast.addEventListener(MouseEvent.MOUSE_DOWN, fastForwarda);
function fastForwarda(event:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, ff72);
}
function ff72(event:Event):void
{
gotoAndPlay(currentFrame+72);
if (watchSound_RayJayShort.play())
{
SoundMixer.stopAll();
}
}
bnt_Fast.addEventListener(MouseEvent.MOUSE_UP, fastForwardb);
function fastForwardb(event:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, ff72);
}
//Rewind button code.
bnt_Rewind.addEventListener(MouseEvent.MOUSE_DOWN, rewinda);
function rewinda(event:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, r72);
}
function r72(event:Event):void
{
if (watchSound_RayJayShort.play())
{
SoundMixer.stopAll();
}
if (currentFrame <=72)
{
gotoAndPlay(1);
}
else
{
gotoAndPlay(currentFrame-72);
}
}
bnt_Rewind.addEventListener(MouseEvent.MOUSE_UP, rewindb);
function rewindb(event:MouseEvent):void
{
stage.removeEventListener(Event.ENTER_FRAME, r72);
}
North America
Europe, Middle East and Africa
Asia Pacific