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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Action Script Help

Status
Not open for further replies.

Tyekai

Programmer
Dec 2, 2008
14
0
0
US
First off... I'm new here so sorry if I do something wrong
And I know you're going to tell me that I need to not be lazy and do it myself and generally that would be the case but for once I have tried and thus have come here after failing to come up with a solution [That wasn't a run on >_>]

Anyway. I'm taking a Flash Animation class in high school.
We've covered basic motion and shape tweens and the like.
We made a ball bounce to music, Pac Man get chased by Blinky, something for our school website [that I believe was never put up], a fade, zoom, and ripple effect.

All of which require no Action Scripting.
I've programmed a bit of Basic and a lot more in C#/C++
So I do know what I'm doing... and I can write an algorithm for it... But my teacher doesn't know Action Script either... and gave us two videos to watch that didn't help too much.

So our assignment is to create a shockwave file that has 4 or more buttons. Each button should play a sound and an animation [not make the button animate but a separate movie clip]. I sketched it out and decided to make a guitar type document. I have a guitar with Guitar Hero-esque buttons. 5 buttons on the fret board. Each plays a different note. Another note plays when you click over the... circle part [on an acoustic guitar]. I then have a "Star Power" button that plays a whammy'd note and another one that plays a random riff. Cool, so we have the sounds down. But now comes into play the animation.

My highest note has a Megaman run across the screen [I cut each frame out from a sprite sheet]. My Star Power button has some fireworks come up from the bottom of the screen. The Catchy Riff button has Pac Man from my old assignment run across the screen.

The problem is that the animations only play as long as the button is held in.

What I would like it to do is play the movie clip when the button is released [not while it's down] and to only play the animation plays

After checking several websites and not getting help from our teacher [who has a Macromedia 8 textbook and that's it...]. I made a separate layer called actions and added:

stop();

To frame 1 of the actions layer.
So the animations stop after playing through once.

But when I added [wrong] code to make it play on the release of the button it stopped working. I ended up with this:

Star Power.onRelease = functions()
{
Fireworks.play();
}

stop();

It stopped working
In my mind that checks the Star Power button and then on the release of it, it calls the Fireworks movie clip and makes it play

The algorithm I started with had a counting variable, i, and an if statement that checked the value of i. If the value was < 1 it played. If not then the movie clip wouldn't. Then it reset the value of i in the second frame of the movie clip.

I tried several times... different ways and commented most of it out.
So rather than explain each line to you I'll just copy and paste what I've tried...

//var i:int; // Counting Variable

//import mx.core.UIObject; // Import packages
//
//// Declare the class and extend from the parent class
//class mypackage.MyComponent extends UIObject {
//{
// if (i >= 1) //Checks to see if it's the first time the firework has is playing
// {
// this.stop; //Stops the animation of the firework after one play through
// }
// else
// {
// i++; //Adds one to the counter of the firework
// }
//}

//if (StarPowerButton.click == true)
// fireworksplay();
//else if (StarPowerButton.doubleClick == true)
// fireworksPlayInfinite();
//
// public function fireworksPlay()
// {
// i = 1;
// fireworks.play;
// }
//
// public function fireworksPlayInfinite()
// {
// i = -99999;
// fireworks.addEventListener().play;
// }
//
// removeEventListener(MouseEvent.CLICK, fireworks.play);
// StarPowerButton.onRelease = StarPowerButton.play;

//StarPowerButton.onRelease = StarPowerButton.play;
//StarPowerButton.onRelease = this.play;

Star Power.onRelease = functions()
{
Fireworks.play();
}




Thank you to anyone who is willing to help me... Sorry I made you read so much I just didn't want to come off as not trying... or something I guess. I've been working for two or three classes [80 minutes each] on this and I really can't come up with anything...

Again sorry you read all that... but I guess it's time I get back to work on it...

Thanks in advance again.
 
You are trying to write AS1 in your timeline and also trying to write a class in AS3. That won't work. Try:
Code:
//AS2 on timeline
starPower.onRelease = function():Void {
   fireworks.play();
}
[tt]starPower[/tt] and [tt]fireworks[/tt] are MovieClip instance names.

Kenneth Kawamoto
 
Thank you very much

I put that in and changed my instance names in the code to match mine [stupidly named at the time... well just not good programming lol]

Anyway so I put that in the 'actions' layer, frame 1
and it still doesn't work.
Did I apply it to the wrong place?
Should I have applied it to the Star Power button's frame 1 instead?
Or perhaps the Fireworks?

Sorry I'd check... but forgot to check here until about 2 min before class [I have to go... like now]

I'll check tomorrow if there's no response

Again thank you soooo much for the help
 
Ok so I put it on the main time line under it's own layer labeled, actions.

It says this:

Star Power.onRelease = function():Void
{
Fireworks.play();
}

and then I put the: stop();
on the first frame of each animation
but it didn't work

It didn't stop on the first frame and it didn't play on button release...

I took the code you gave me out and it stopped on the first frame but of course that didn't leave any code that even attempted to play on release...

I'm sorry to bother you...
It's probably me whose screwing up...
 
Sorry I was just thinking...
I have my animation on the Down state of the button
Does that have anything to do with it?
Maybe it should be somewhere else...
 
o_O
Didn't think it worked like that

Um... ok
So I made a fireworkss layer
put the fireworks in it and gave it an instance name of fireworkss

modified my code to say:
Star Power.onRelease = function():Void
{
fireworkss.play();
}

But now I has more problems
fireworkss plays on start, and it loops [yes I went and gave it the stop(); so it's not that]


Would you mind giving me a step by step of what I should do if I were to open up a blank document and create this button and have the animation play on the release


So...
File... New
Draw a shape
Convert to symbol [button] with name of star power
give it an instance name of 'starPower'
create a new layer
insert new symbol [movie clip] with name of fireworks
give it an instance name of 'fireworks'
make the fireworks animation
make new layer
go to the actions screen on the new layer
type in:

starPower.onRelease = function():Void
{
fireworks.play();
}

go to the fireworks layer and in frame 1 type:
stop();

test movie
there should be a button and when i click it then let go
it should play the animation
correct?
Or am I forgetting something
 
Yes that sounds fine to me.

On the main timeline you should have two layers: the first layer contains the script:[tt]
starPower.onRelease = function():Void {
fireworks.play();
};[/tt]
Then the second layer contains the following two things on Stage: one is a Button with the instance name "starPower", the other is a MovieClip with the instance name "fireworks".

Double click the MovieClip "fireworks". You should have two layers on its timeline: the first layer contains the script [tt]stop();[/tt], and the second layer contains the animation.

Kenneth Kawamoto
 
I did that and it gave me this error

1046: type was not found or was not a compile-time constant

I looked it up on google
And it said that this error meant that the stuff in the library was the same as the instance names
So I renamed the stuff in the library

And I still got the same error...
 
I switched the drop down menu in the Actions screen from Action Script 3.0 to Flash Lite 2.0 Action Script
Which didn't work
So I tried Action Script 1.0 & 2.0
But again it didn't work...

I've reinstancenamed my starPower button to starPower_btn
Because apparently that helps.


Also is it possible to have both 3.0 and 1.0 & 2.0 in the same flash file?
Not for the same button or object.
But could I have button1 be coded in 3.0
and button2 be coded in 1.0 & 2.0
Or is that not possible cause the whole thing has to be coded in one language [like I would expect it to be]
 
Hm... So my teacher is making us code in AS3

He also told us recently that he wants one button to turn off all the other movie clips other than the one that's playing

So...
button1
stops movieClip1, movieClip2, movieClip3
plays movieClip4

Thing is we don't know how to make the play/stop functions specific to each individual movie clip.


So here's what I have so far......

button1.addEventListener(MouseEvent.CLICK, playMovie1);

function playMovie1(event)
{
play(); //needs to play movie clip 1
stop(); //needs to stop movie clip 2
stop(); //needs to stop movie clip 3
stop(); //needs to stop movie clip 4
}

button2.addEventListener(MouseEvent.CLICK, playMovie2);

function playMovie2(event)
{
stop(); //needs to stop movie clip 1
play(); //needs to play movie clip 2
stop(); //needs to stop movie clip 3
stop(); //needs to stop movie clip 4
}

button3.addEventListener(MouseEvent.CLICK, playMovie3);

function playMovie3(event)
{
stop(); //needs to stop movie clip 1
stop(); //needs to stop movie clip 2
play(); //needs to play movie clip 3
stop(); //needs to stop movie clip 4
}


button4.addEventListener(MouseEvent.CLICK, playMovie4);

function playMovie4(event)
{
stop(); //needs to stop movie clip 1
stop(); //needs to stop movie clip 2
stop(); //needs to stop movie clip 3
play(); //needs to play movie clip 4
}
 
AS3 is the right option :)

But I'd do it like this, which is much less typing:
Code:
//AS3 on timeline
for (var i:uint = 1; i <= 4; i++) {
	this["button" + i].addEventListener(MouseEvent.CLICK, buttonClick);
}

function buttonClick(e:Event):void {
	for (var i:uint = 1; i <= 4; i++) {
		this["movieClip" + i].stop();
	}
	
	this["movieClip" + e.target.name.substr(6)].play();
}

Kenneth Kawamoto
 
Ok I'll try that...
I just want to know how that works.
I know the for loop lol
But... for this part

this["button" + i]
this["movieClip" + i] // I know it's the same type of thing

How does that work as a call?
I've never seen something like that done before

and this

(e:Event)

what is the 'e:' part for?


And then just making sure what this does lol...

loop to rename the buttons with a number at the end of it
loop to rename the movie clips with numbers at the end of them
and stop all of them
then play movie clip and the button that have the same number which is found at the 7th letter of the name of the button

Correct?
 
It does the same thing as your script.

[tt]this["button" + i][/tt] is not renaming the button but rather "finding" the button.

If [tt]i[/tt] is [tt]3[/tt], for example, the below are the same:

[tt]this["button" + 3][/tt]
[tt]this["button3"][/tt]
[tt]button3[/tt]

Kenneth Kawamoto
 
Sorry for double posting... there's no edit thingy I guess

Ok so I did that and I ran it
er... Test Movie'd it

And it gave me an error whenever I released a button:

TypeError: Error #1010: A term is undefined and has no properties.
at Buttons_fla::MainTimeline/Buttons_fla::frame1()
 
Oh yea! lol I still have 2-4 in the buttons lol

And guess what
It works!!!!!
Thank you thank you thank you thank you thank you thank you thank you
THANK YOU!!!!

Finally after two weeks...
This really shouldn't have taken so long
Thank you Kenneth for sticking through this with me

*fiddles around with it a bit*
One last thing
The code we have stops the animation in the middle of it
Which is cool
But for some reason I thought it would make the animation go away lol
Like only one should be...

Never mind. I got it to work with a gotoAndStop(1); instead of stop();


Again though
Thank you so so so so so so so so so so soooooooo *resists erge to curse* much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top