View comments | RSS feed

Event-handling examples

Here are a few more concrete examples of events to give you an idea of some of the common event elements and possible variations available when you write event-handling code:


Flash CS3


Comments


ASEVBill said on Dec 20, 2007 at 4:14 PM :
I tried the method above for making a button that opens a web page, but I get the following error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

I can not seem to find a way around this.
djtechwriter said on Dec 21, 2007 at 4:40 PM :
ASEVBill,

You need an instance of a button with instance name linkButton. In other words,
1. Drag a button component from the Components panel, User Interface set of components.
2. Give it the instance name linkButton in the Properties panel.
3. Paste the code from this page into the Actions panel for the first frame in the Timeline.
4. Test movie.
mcofano said on Jan 2, 2008 at 10:19 AM :
Sir / Madam:

I am using Flash CS3. I have a defined a button as follows:

Name: btnMenuCurve1
Class: btnMenuCurve1
Base Class: flash.display.SimpleButton

Export for Action Script is CHECKED
Export in First Frame is CHECKED

I created a layer to hold the Action Script called "ACTIONS"

There is an instance of this button on the stage with instance name "btnMenuCurve1"

In the first frame of the ACTIONS layer, I modified the sample code for navigating to a website by clicking a button:

CODE BEGINS:
*******************
import flash.events.MouseEvent;

function gotoLC(event:MouseEvent):void
{
var lcURL:URLRequest = new URLRequest("http://www.LaurenCard.com/index.html/");
navigateToURL(lcURL);
}

btnMenuCurve1.addEventListener(MouseEvent.CLICK, gotoLC);
******************
CODE ENDS:

I received the error:

1046: Type was not found or was not a compile-time constant: btnMenuCurve1.

I then (based upon a google of the error above:) added the code

public class btnMenuCurve1{}

and received the error:

1114: The public attribute can only be used inside a package.

Please help.

Thanks!
adbe_paul said on Jan 2, 2008 at 11:46 AM :
Although it's obviously not clear from the error message, the problem is that your instance name is the same as the class name for the symbol, so there's a conflict between the two.

If you rename your button to something like "btn" and change the last line of code as follows, it works:
btn.addEventListener(MouseEvent.CLICK, gotoLC);

Alternatively, you can just rename the class name in the button symbol's Linkage dialog. For instance, it's a very common convention to start class names with an upper-case letter, so you could change the class name to "BtnMenuCurve1" and the code will also work (without changing the symbol name or your code). It's enough for the two names to be different only by one letter, and since ActionScript 3.0 is case sensitive, the difference can be as small as making one letter uppercase.
If you make that change then of course your class name won't match the symbol name, so that's a tradeoff to consider.

Assuming you're going to have several different buttons with different symbol and class names, I would suggest using the same name for the symbol name and class name, and picking a different (but similarly consistent) naming scheme for the instance names. While, as I mentioned, it works to have names that are different only in terms of upper and lower case, it makes the code harder to read so it's not something I'd recommend.
sneakyimp said on Jan 21, 2008 at 9:44 PM :
I find the use of 'this' confusing in the first example inside function playMovie. Is this behavior different than one would expect in Actionscript 2? I'm not sure, but I would expect 'this' in that context to refer to playButton or some object related to the event that was dispatched.
extdw_doc said on Jan 28, 2008 at 3:46 PM :
You can find out by using a trace() statement such as:
trace("the value of this is: " + this);

In both instances in the example, it refers to:
[object MainTimeline]
sneakyimp said on Jan 28, 2008 at 3:55 PM :
Or you guys could add a comment indicating that this refers to the main timeline. It would certainly make your example clearer.
djtechwriter said on Jan 30, 2008 at 8:01 AM :
sneakyimp,

Actually, it depends on the context for the code, because "this" refers to the parent container. In the case where you paste it in the Actons panel of the Timeline without attaching it to other display objects, then "this" does refer to the main timeline. However, that isn't always the case. Refer to the entry for "this" in the ActionScript Language Reference for more information:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#this

In fact, if you find yourself confused about keywords, statements, methods or properties, it's generally a good idea to use the ActionScript Language Reference in tandem with Programming AS3 as you learn.
unPull said on Feb 14, 2008 at 6:01 PM :
Using the following Actionscript 3 for set up with a simple email button in
Flash CS3:

linkButton.addEventListener(MouseEvent.MOUSE_DOWN, contact);

function contact(event:MouseEvent):void
{
var contact_req:URLRequest = new
URLRequest("mailto:v@unpull.com");
navigateToURL(contact_req);
}

This apparently works to open the users mail browser, but also opens a
troublesome "blank" internet browser window. I am using a CS3 manual
from Todd Perkins, but cannot determine how to defeat this extra window.
swartz1999 said on Mar 4, 2008 at 5:54 PM :
unPull, the navigateToURL() function has a second, optional parameter, and in your case you want to set it to "_self", as in: navigateToURL(contact_req, "_self"). This is documented in the language reference entry for flash.net.navigateToURL().

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000023.html