Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Closed Thread
 
Thread Tools Rating: Thread Rating: 6 votes, 3.33 average. Display Modes
Old 11-07-2007, 07:30 AM   #1
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default ActionScript 3.0 101

Hello all,

Since I've seen a lot of the same threads about the same (basic) questions in the last couple of days I thought of combining them in one thread.

I'll try to give both as2 and as3 examples (where applicable) as we go, so you can see the difference clearly. Also I'll try to include a link to the relevant livedocs page.

There is one post per general problem, so one can easily refer to the right post.

Feel free to contribute.

- FB


Index:
1. AS3 basics
1.1 Adding ActionScript
1.2 What does function x?
1.3 Buttons
1.4 onEnterFrame
1.5 Attaching library items at runtime
1.6 Dragging MovieClips
1.7 Changing MovieClip levels
1.8 Accessing a loaded SWF
1.9 Types and typecasting


To do:
- Dynamic paths to MCs (http://actionscript.org/forums/showthread.php3?t=152182)
- Events (general explanation)
- Classes (general explanation) (http://actionscript.org/forums/showthread.php3?t=153914)
- Calling AS Code from Javascript (http://actionscript.org/forums/showt...php3?t=152812)
- Animated buttons and AS (http://actionscript.org/forums/showt...php3?t=153336)

Last edited by FrodoBaggins; 11-27-2007 at 07:23 AM. Reason: added the index, to do list
FrodoBaggins is offline  
Old 11-07-2007, 07:32 AM   #2
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default What does function x?

If you wonder what a certain function does and what parameters it takes, have a look in the LiveDocs or select the function in flash cs3 and hit F1.
FrodoBaggins is offline  
Old 11-07-2007, 07:43 AM   #3
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default Buttons

Creating button functionality has changed a bit since as2. In as2 you'd write:
ActionScript Code:
var myButton:Button = my_bttn; myButton.onRelease = function() {   // functionality here... }

AS3 is a little bit different, since a whole lot needs to be done using so called event listeners, they wait for a certain event to happen and than call a function. So in as3 you'd write this instead:
ActionScript Code:
// my_bttn is the name of the button on your stage var myButton:Button = my_bttn; myButton.addEventListener(MouseEvent.CLICK,buttonPressed); function buttonPressed(e:MouseEvent) {   // functionality here... }

A little breakdown of the addEventListener function.
The first parameter defines the event to listen to, in our case a mouse click (MouseEvent.CLICK)
The second parameter defines what function gets called when the event happens, in our case buttonPressed. NOTE: don't add the parenthesis "()" to this.

Button on LiveDocs: http://livedocs.adobe.com/flash/9.0/...ls/Button.html
Event on LiveDocs: http://livedocs.adobe.com/flash/9.0/...nts/Event.html

Last edited by FrodoBaggins; 11-07-2007 at 09:01 AM.
FrodoBaggins is offline  
Old 11-07-2007, 07:50 AM   #4
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default onEnterFrame

Another thing used a lot in as2 has changed in as3, using onEnterFrame.

as2:
ActionScript Code:
this.onEnterFrame = function() {    // your functionality here... }

Once again events say hi here. We add an event listener to the stage that gets fired every time a frame is entered. This way our as3 code looks like this:
ActionScript Code:
this.addEventListener(Event.ENTER_FRAME,EnterFrameHandler); function EnterFrameHandler(event:Event):void {    // your functionality here... }

Event on LiveDocs: http://livedocs.adobe.com/flash/9.0/...nts/Event.html

thanks to p0c: http://actionscript.org/forums/showthread.php3?t=152015
FrodoBaggins is offline  
Old 11-07-2007, 07:57 AM   #5
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default Adding ActionScript

I should have posted this one as first, but it kinda slipped my mind.

Since as3 it is no longer possible to add code directly to a movieclip, button, etc... Some of you might find this frustrating and maybe even completely absurd, but it has always been good practice not to seperate your code from your graphics.

Instead create an empty locked and invisable layer on your timeline and put all code there. This way everyone will know where to find your code and there is no more hidden code, say code on a button that is in an mc in an mc, such a pain to find...

PS: I will refrain from going into the whole timeline coding vs classes subject here.

Last edited by FrodoBaggins; 11-07-2007 at 08:51 AM. Reason: typo
FrodoBaggins is offline  
Old 11-07-2007, 08:06 AM   #6
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default Attaching library items at runtime

as2:
ActionScript Code:
var myObject:MovieClip = this.attachMovie("idName", "newName", depth);

In as3 it's this:
ActionScript Code:
var myObject:LinkageIdentifier = new LinkageIdentifier (); addChild(myObject);

'LinkageIdentifier' should be changed to the Class identifier name that you gave it in the linkage dialogue box.

Thanks to matbury: http://www.actionscript.org/forums/s...31&postcount=2

Last edited by FrodoBaggins; 11-07-2007 at 10:25 AM.
FrodoBaggins is offline  
Old 11-07-2007, 10:56 AM   #7
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default Dragging MovieClips

The startDrag function has changed a bit since as2. Where it used to take 0, 1 or 5 arguments it now takes 0, 1 or 2.

as2:
ActionScript Code:
myMC.onPress = function () {    // left, top right, bottom are optional    this.startDrag(lockCenter, left, top, right, bottom); } myMC.onRelease = function () {    this.stopDrag(); }

as3:
ActionScript Code:
myMC.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) myMC.addEventListener(MouseEvent.MOUSE_UP, mouseReleasedHandler); function mouseDownHandler(event:MouseEvent):void {     myMC.startDrag(); } function mouseReleasedHandler(event:MouseEvent):void {     myMC.stopDrag(); }

Just as in as2 in as3 there are some problem with dragging on a low framerate. To overcome this you can use updateAfterEvent(), so there is no need to increase your framerate when that could cause undesired effects.
I'll only show the as3 example here.

ActionScript Code:
myMC.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) myMC.addEventListener(MouseEvent.MOUSE_UP, mouseReleasedHandler); function mouseDownHandler(e:MouseEvent):void {     myMC.startDrag();     stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } function mouseReleasedHandler(e:MouseEvent):void {     myMC.stopDrag();     stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } function mouseMoveHandler(e:MouseEvent):void{     e.updateAfterEvent(); }

Now to constrain the movement of our dragable object we need to send a rectangle object to the startDrag function.

ActionScript Code:
myMC.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) myMC.addEventListener(MouseEvent.MOUSE_UP, mouseReleasedHandler); // create Rectangle object var bounds:Rectangle = new Rectangle(0,0, 100, 100); function mouseDownHandler(e:MouseEvent):void {     // changed for limited movement.     myMC.startDrag(true, bounds);     stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } function mouseReleasedHandler(e:MouseEvent):void {     myMC.stopDrag();     stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } function mouseMoveHandler(e:MouseEvent):void{     e.updateAfterEvent(); }

Kudos to Panel: http://actionscript.org/forums/showp...97&postcount=8, http://actionscript.org/forums/showp...06&postcount=8

Sprite's startDrag function on the LiveDocs: http://livedocs.adobe.com/flex/2/lan...tml#startDrag()

Last edited by FrodoBaggins; 11-08-2007 at 07:08 AM. Reason: added url to livedocs, typos
FrodoBaggins is offline  
Old 11-07-2007, 11:24 AM   #8
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

-bump-

Nice one Frodo!

I've got a bee in my bonnet about Flash help and LiveDocs - I think a lot of people out there are getting frustrated with either:

* not knowing how to find ALL the necessary syntax and classes to do something or

* it being such a chore to root though all the class and package references that they may as well post a thread to get a specific answer to a specific question just because it's quicker and easier.

Adobe are famous for their useless 'help' files. Macromedia were much better. It looks like Adobe aren't going to break with the tradition!

Personally, I can't wait to get my hands on some decent reference material.
__________________
http://matbury.com/
matbury is offline  
Old 11-07-2007, 11:48 AM   #9
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default

Thanks Mat.
I'm trying to make things as clear as possible and personally I do think the liveDocs are pretty good. I agree they could be better, but at least there is documentation on all functions.
But let's discuss this further in your thread: http://actionscript.org/forums/showthread.php3?t=152163
FrodoBaggins is offline  
Old 11-07-2007, 12:56 PM   #10
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default Changing MovieClip levels

In as2 you could simply use swapDepths to set a MovieClip to a new depth.
ActionScript Code:
// use geNextHighestDepth() to set the MC on top. var newDepth:Number = 100; my_mc.swapDepths(newDepth);

as3:
ActionScript Code:
// container is the DisplayObjectContainer where the clip is located // clipToSwap is the DisplayObject, // newDepth is the index to move the clip to container.setChildIndex(clipToSwap, newDepth);

To set it to the highest available depth we'd use:
ActionScript Code:
container.setChildIndex(clipToSwap, container.numChildren-1);

It is important to set it to numChildren-1, any higher would generate an error.
Quote:
RangeError: Error #2006: The supplied index is out of bounds.
Thanks Slowburn and Panel: http://actionscript.org/forums/showthread.php3?t=144688

Once again the link to the liveDocs concerning this topic. DisplayObjectContainer's setChildIndex method: http://livedocs.adobe.com/flash/9.0/...setChildIndex()
Have a look around this document to see what other options you got.

Last edited by FrodoBaggins; 11-07-2007 at 02:42 PM. Reason: added url to livedocs
FrodoBaggins is offline  
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
shared whiteboard with actionscript 3.0 flashyBird ActionScript 3.0 2 10-12-2007 05:13 AM
Animations in ActionScript 3.0 teenup ActionScript 3.0 6 09-17-2007 12:11 PM
PHP, Actionscript 3.0 and characters latrixie ActionScript 3.0 7 08-29-2007 08:41 AM
Actionscript 3.0 Programmer Insanemitch Projects and Positions 0 08-28-2007 08:21 PM
Preloader for actionscript 3.0 rysotope ActionScript 3.0 3 07-10-2007 04:58 PM


All times are GMT. The time now is 11:49 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.