[Tutorial] AS3 Buttons and Functions

A place for tutorials on how to get the most out of Flash

Did this help you understand Action Script 3?

Yeah, I guess.
5
83%
Maybe if it had some clarification. (I might reply asking for help)
0
No votes
Nope, still not a clue.
1
17%
 
Total votes : 6

[Tutorial] AS3 Buttons and Functions

Postby ShadeFire » Thu Jun 30, 2011 12:30 am

I have (obviously) been a lurker on these forums for a while now, and have finally decided to DO SOMETHING for the community. By making this tutorial (of sorts) I hope to help someone in understanding a few basics of AS3. I have also included two files: one which is already coded as a reference point for how the code works, the other blank for people to practice with.

Tutorial.zip
Coded
(482.11 KiB) Downloaded 71 times


Tutorial_Blank.zip
Blank
(480.94 KiB) Downloaded 80 times


Before anything else, I have to say a few things. First, in AS3, you cannot place code on an object, and must refer to it's instance name. Second, if you have code remotely changing frames inside a movie clip, you have to make sure all instances of the movie clip are on the same layer and have the same instance name, otherwise when moved to a new frame, the movie clip reverts to the beginning state.

--{Codes}--

Instance Names/Labels/Paths

Instance Names are sorting mechanism Flash uses to define different objects. Say two of the same object are placed on the same workspace and you only want to change one of them. So Flash does not get confused by which one you mean, it has you call each INSTANCE a different name. This can be done by clicking on the object (not the frame the object is in) and opening the properties window.

Labels are names given to keyframes. To label a keyframe, select it and open the properties window.

Paths are required by code to know what to work in. A path is always written with instance names separated by periods. The name of the scene is excluded. Say I wanted to toggle cum on/off on a leg in a game with multiple characters, my path might look something like (right_thigh.peach_thigh.(instance name)) with the (instance name) referring to that of the cum.

Buttons

A button can be placed anywhere in the .fla. I prefer to keep mine in "scene 1" for simplicity. You then need Flash to monitor the button for a click. to do this, you must define where the button is, and a function that clicking the button will execute. The following is this code.
Code: Select All Code
import flash.events.MouseEvent;                  ------------//you need to add this before anything else.

button_1.addEventListener(MouseEvent.CLICK, button_1_clk);

function button_1_clk(event:MouseEvent):void{
   (~what the function does~)
}

button_1 = The path to the button followed by the button's instance name. (e.g. sprite_1.button_1) Separate with periods. If the button is in "scene 1," don't designate a path, only the instance name.
button_1_clk = The ID of the function that is excecuted once the button is clicked.
(~what the function does~) = The command you want to be excecuted. Copy paste from below.


Common Types of Commands

All of the following commands can be either manually executed (button, if-or statement, etc.), or automatically executed (when the frame is played). For the former, insert the code into the formula above. For the latter, simply place in the action window alone.

1) gotoAndPlay

This command goes to the designated label (e.g. "fuck," "cum," etc.) or frame # (omit the quotations if it is a #)and begins playing. If going to a frame inside a movie clip, the path must be designated at the beginning.
Code: Select All Code
gotoAndPlay("frame_1");

frame_1 = The frame label or # you want to go to and play at.

2) gotoAndStop

This command goes to the designated label (e.g. "fuck," "cum," etc.) or frame # (omit the quotations if it is a #) and does not begin playing. Good for changing outfits (or cum). If going to a frame inside a movie clip, the path must be designated at the beginning.
Code: Select All Code
gotoAndStop("frame_1");

frame_1 = The frame label or # you want to go to and stop at.

3) visible

This command toggles visibility of movie clips.
Code: Select All Code
sprite_1.visible = true/false;

sprite_1 = Path to movie clip and instance name.
true/false = either true or false.

4) stop

Stops at the frame that contains it or at a frame designated ahead of time once the playhead reaches it. Place the frame label or # inside the parentheses if it is wanted. I recommend JUST PLACING IT WHERE YOU WANT TO STOP!
Code: Select All Code
stop();
"Anything is funny, as long as it is happening to someone else."~Anonymous
User avatar
ShadeFire
Newly Registered
 
Joined: Sun Dec 19, 2010 10:32 pm

Re: [Tutorial] AS3 Buttons and Functions

Postby KillerCow » Thu Jul 07, 2011 12:26 am

Some really useful stuff here, thanks for the post.

Also, what are the odds that we signed up the same day and both lurkers...
Working on something
Image
User avatar
KillerCow
Newly Registered
 
Joined: Sun Dec 19, 2010 7:24 pm

Re: [Tutorial] AS3 Buttons and Functions

Postby kojiro » Sun Jul 24, 2011 11:47 pm

ShadeFire Wrote:3) visible

This command toggles visibility of movie clips.
Code: Select All Code
sprite_1.visible = true/false;

sprite_1 = Path to movie clip and instance name.
true/false = either true or false.


is there anyway to make what you made invisible, visible by clicking it again?
kojiro
 
Joined: Sat Jul 16, 2011 4:35 am

Re: [Tutorial] AS3 Buttons and Functions

Postby GolanTrevize » Fri Jul 29, 2011 8:55 am

kojiro Wrote:
ShadeFire Wrote:3) visible

This command toggles visibility of movie clips.
Code: Select All Code
sprite_1.visible = true/false;

sprite_1 = Path to movie clip and instance name.
true/false = either true or false.


is there anyway to make what you made invisible, visible by clicking it again?

If you dont want to use an If sentence, I,m not sure if you can write:

sprite_1.visible = !(sprite_1.visible)

But if it doesnt work you can try:

sprite_1.visible = Math.abs(sprite_1.visible - 1)

It´s not very usual, but should work
Current project: Dark moon (Artists needed)
User avatar
GolanTrevize
 
Joined: Wed Jul 20, 2011 3:32 pm

Re: [Tutorial] AS3 Buttons and Functions

Postby kojiro » Fri Jul 29, 2011 10:08 pm

the second code gives me some weird boolean error about a wrong number. but the first code works. when i click it it becomes invisible but i cant click it again to make it visible. i want to be able to make it a button click that makes it invisible and when clicked again it is visible
kojiro
 
Joined: Sat Jul 16, 2011 4:35 am

Re: [Tutorial] AS3 Buttons and Functions

Postby BlueLight » Sat Jul 30, 2011 12:29 am

So a boolean is true or false, 1 or 0, on or off. It depends on what language your using. But basically you always going to have a on or off state.
If your getting a error i'm guessing you either have it referenced wrong(You is just pointing randomly in the air and aimed at no one really) or it doesn't have a valid value.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users