Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Play Again button w/ AS3

Explorer ,
Dec 18, 2008 Dec 18, 2008
how would I code a play again button using AS3 since you cannot place the code on the button itself anymore. Would the code still be gotoAndPlay(1);?

would I need to give the button an instance name and what would I need to change the code to?

thanks in advance!
TOPICS
ActionScript
2.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 18, 2008 Dec 18, 2008
DPSwebmaster,

> how would I code a play again button using AS3 since you
> cannot place the code on the button itself anymore.

You can either set your publish settings for AS1/AS2 and proceed as
you're used to, or -- if the SWF benefits from AS3 -- you'll put the code
into a keyframe that corresponds to the keyframe in which your button
appears. If your button is on frame 100, you'll put your code in frame 100,
so that the code "sees" your button at that frame.

Use the Propery inspector to give you button symbol (or movie clip
symbol) an instance name, then use that instance name to "speak" directly to
that button.

Here's a quick run-down:

http://www.quip.net/blog/2007/flash/making-buttons-work-in-flash-cs3


David Stiller
Co-author, Foundation Flash CS4 for Designers
http://tinyurl.com/5j55cv
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2008 Dec 18, 2008
I think I get the gist of what I need to do now in AS3 (BTW this does need to be in AS3 for the rest of my code).

However it's not working. I don't get any errors...just doesn't work as intended. I have given my button an instance name of 'PlayAgain' and placed the AS code on frame 1 of my timeline.

code is below
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 18, 2008 Dec 18, 2008
DPSwebmaster,

> I have given my button an instance name of 'PlayAgain' and
> placed the AS code on frame 1 of my timeline.

As long as that button (with the instance name PlayAgain) appears in
frame 1 as well, you should be good.

> PlayAgain.addEventListener(
> MouseEvent.MOUSE_UP,
> function(evt:MouseEvent):void {
> gotoAndPlay(1);
> }
>

There should be a closing parenthesis on that [... this one: ); --
which is basically the other half of the "sandwich" that begins with
addEventListener( ...]. But other than that, your code looks fine. Be
advised that you can also use a named function, if you like:

PlayAgain.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
function mouseHandler(evt:MouseEvent):void {
gotoAndPlay(1);
}

Do you see how those are effectively the same thing? The
addEventListener() method accepts two parameters: a) an event to listen for
and b) a function to perform. In the examples I've shown, one of them
illustrates an anonymous function; the other illustrates a named function.

In any event, to make sure your code is responding at all, it may help
to use a trace() function. This is nothing more than a mechanism to print
text to the Output panel, but it's great for debugging:

PlayAgain.addEventListener(
MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
gotoAndPlay(1);
trace("I've been clicked!");
}
);

Try that revision (only one line has changed), and when you click on the
button, you should see the message "I've been clicked!" in your Output
panel. If you don't, we'll know that this event listener isn't getting
assigned properly.


David Stiller
Co-author, ActionScript 3.0 Quick Reference Guide
http://tinyurl.com/2s28a5
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2008 Dec 19, 2008
well, the trace works, but still cannot get the play again to work. Nothing happens, and I tried the different code that was provided. It may have to do with the fact that the entire animation is being done in AS3, so I may need to reference it differently. I basically have on the stage an image on one layer and the button on another layer and the AS on it's own layer.

the entire AS code is as follows:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2008 Dec 19, 2008
DPSwebmaster,

> well, the trace works, but still cannot get the play again
> to work.

I suspect that's because, without realizing it, you're asking the wrong
timeline to start again from frame 1.

> It may have to do with the fact that the entire animation
> is being done in AS3, so I may need to reference it
> differently.

Aha! Well yes, if the animation is scripted, then it may not have
anything to do with the timeline in which it appears. The gotoAndPlay()
method instructs the targetted timeline to go to a certain frame and play
from there. It's a means of navigation timeline animation, as opposed to
scripted animation.

Since you're dealing with custom code, you'd have to know how that code
accomplishes what it does. And then your button would instruct the code to
reset itself.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2008 Dec 19, 2008
well, I know that I basically just need to get this part to repeat itself again once the button is pressed

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2008 Dec 19, 2008
DPSwebmaster,

> well, I know that I basically just need to get this part
> to repeat itself again once the button is pressed

Honestly, from the code you're showing, it's hard to say, if not
impossible. What part of the code determines which image is showing? All I
see here is an enterFrame handler that affects the height of a mask and then
the alpha of a masked image. I would *guess* you have an array of images
somewhere, perhaps with a variable the determines which element in that
array is the current image. If so, your button might set set that variable
to 0 (the beginning of an array).

Your images might be stored in XML, which would be similar, but
different syntax. Your images might be inside a nested movie clip. You
haven't explained any of that. :)


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2008 Dec 19, 2008
no, in this case the images aren't stored in XML. I just have several images that have been converted into movieclips and are placed on frame 1 of the timeline and given instance names 'myMask', 'origImage','maskedImage'. So with the code I attached I know that I have a mask which moves from the bottom of the image to the top of the image, and then stops. I just need to repeat that somehow when a button is pressed.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2008 Dec 19, 2008
DPSwebmaster,

> ... with the code I attached I know that I have a mask which
> moves from the bottom of the image to the top of the image,
> and then stops. I just need to repeat that somehow when a
> button is pressed.

Based on the code you've shown, the animation stops when the mask's
height is greater than the image's height. At that point, the
removeEventListener() method is called, which kills the enterFrame handler.
I would guess, then, that all you need to do is: a) kill the enterFrame
handler in case it's currently running, b) set the masks' height toa very
low number, and c) restart the enterFrame handler:

PlayAgain.addEventListener(
MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
myMask.height = 1;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
);

Or, you could use a named function as your CLICK handler, because you're
already doing that for your enterFrame handler:

PlayAgain.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler);(evt:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);

myMask.height = 1;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
};

Either one of those amounts to the same thing.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 18, 2008 Dec 18, 2008
Try this
myButton1.addEventListener(MouseEvent.CLICK,powerButton1);
function powerButton1(event:MouseEvent):void {
myButton1.gotoAndPlay(1);
}

myButton1 is the instance name and this will work,

if you still have troubles, create a frame label inside the button call it PlayAgain and try this.

myButton1.addEventListener(MouseEvent.CLICK,powerButton1);
function powerButton1(event:MouseEvent):void {
myButton1.gotoAndPlay("PlayAgain");
}

If you have any more troubles feel free to visit my website and shoot me an email.
Web Design Company
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 22, 2008 Dec 22, 2008
LATEST
this worked great for me. Thanks David!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines