write inline code within documents that has a class assigned - actionscript-3

I download an AS3 package and I'm trying to add an eventListener to it. I'm adding this event in inline code. But I get the following error:
1046: Type was not found or was not a compile-time constant: MouseEvent.
Since I don't know how to write classes, my question is: Can I write inline code when the document has a class assigned to it? If so, why am I getting the above error?

Did you
import flash.events.MouseEvent;
?

Related

AS3 Error: Access of undefined property Event

I'm making a game in Flash CS4 using AS3 and I've been trying to make a Play button that works. I already know how to make it work but while debugging I always get "Access of undefined property Event" in compiler errors.
Here's my code:
// imports
import flash.events.Event;
import flash.events.MouseEvent;
// code
stop();
pbtn.addEventListener(EVENT.CLICK, startPlay);
function startPlay(event:MouseEvent):void
{
gotoAndStop(2);
}
I already set the instance name of the button to "pbtn" so it can't be that. I tried both MouseEvent and just Event for the startPlay() function.
I know this must be a really dumb question but I just can't manage to fix this.
pbtn.addEventListener(EVENT.CLICK, startPlay);
Just change that line to this
pbtn.addEventListener(MouseEvent.CLICK, startPlay);
And its case sensitive so don't change any letter case

Flash Action Script 3: how to get hex value from a color picker?

this is my code.
dropColor.addEventListener(ColorPickerEvent.CHANGE, updateValue);
function updateValue(e:ColorPickerEvent){
trace(dropColor.hexValue);
}
It doesn't work.
1046: Type was not found or was not a compile-time constant: ColorPickerEvent.
Could someone tell me why?
You need to add an import for your ColorPickEvent.
import fl.events.ColorPickerEvent;
Also, depending on which compiler you are using, you most likely need the fl.swc :
http://evolve.reintroducing.com/2007/10/30/tips-n-tricks/fl-package-swc/
You'll need to add that to your project, follow the directions on that page.

AS3: Access button from class

So im quite new to AS3 but have worked with AS2 a lot before.
I have created a button and placed it on my stage then inside my class i have added this:
test.addEventListener(MouseEvent.CLICK, buttonClicked);
function buttonClicked(ev:MouseEvent):void
{
trace("Clicked");
}
Now this does not work as it can't find the stage, the only way i can get this to work is if i put the listener on the same frame as the button & not in the class.
But there must be away around this.
Thank you.
Eli
Update - adding Error messages
If I keep the above code all in the external class these are the errors i get.
Line 22 1120: Access of undefined property test. Line 22 1120: Access
of undefined property myButtonClick.
If you have created a document class with timeline then your "test" button must be in first frame. Because document class starts executing from first frame. You can access your button instance only when its available in stage.
Oh, I forgot to mention. You have to declare those instances as public variable in your document class.
public var test:SimpleButton;
Please go thru below and let me know which of the way you were having.
1) Are you having Document class?
There is a field Class in the Document Properties under the Publish tab of the flash IDE, if you are giving your class name in that field then it is called as Document Class
If you are having document class then you can create listener to your button even in the constructor button. Flash won't throw any errors like you got.
2) If you are instantiated your class in the first frame, it won't have the properties of stage till you add that to the stage using addChild. Also it won't have access to your button. And so it will throw the error, The access of undefined property.
Have you assigned the instance of the button on the stage the name "test"? The error message you posted seems to say there is nothing with the name "test" to assign the event listener to.
To check, click on the button the stage and look at the 'Properties' tab: will show in a text box near the top if it needs assigning.
Now the second error you posted means you're referring to something called "myButtonClick" without first declaring/initialising a variable/function with that name. You will either need to declare it or correct it if you meant to refer to something else.
Fixed.
I was being rava stupid as normal, forgot to put them inside the init :|
For the people who might come across this problem.
Working Code:
public function Main()
{
// constructor code
test.addEventListener(MouseEvent.CLICK, myButtonClick);
}
function myButtonClick(ev:MouseEvent):void
{
trace("button Clicked);
}
Anyway thanks guys for the help sometimes its just the simplest answers are the correct ones.
Eli

Weird Instance Behavior When Using Its Class in the Document Class

I'm going to try to describe the issue as clear as possible:
The document class (Main.as) loads a SWF file and places it on the stage.
The SWF already has a child instance on its stage with a defined class of its own (Child.as).
If I import the SWF child's class (Child.as) in the document class (Main.as) with the sole purpose of accessing a static property, then the child's instance on the stage goes crazy (i.e. runs in a loop) without throwing any error or warning at compilation or run-time.
Note: The import of the child class alone doesn't do anything (probably because it is discarded for not being used), but actually mentioning the class anywhere in the public document class triggers the weird behavior of the child instance on the stage.
Just to make myself clear, the child instance works just fine, without any problems as long as I do not use its class in any way in the document class.
My question: Do you have any idea what would make the child instance on the stage behave as if an error occurred even if there's no error or warning messages? Or a possible workaround?
Try not importing the class and getting it through (once the swf is loaded)
myLoader.contentLoaderInfo.applicationDomain.getDefinition("Child").STATIC_PROPERTY
I agree, it is not a very nice answer but a work-around. I'd be glad if someone found a real answer to this question.

Adobe Flash CS3: DataGrid component is broken?

My datagrid component appears to be broken.
When I drag one out onto the stage, its just a square. Nothing in it. Just a square...
Then, when I tried to programmatically add it to the stage, using an example straight off the docs, it throws some errors:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/DataGrid.html
import fl.controls.DataGrid;
var myDataGrid:DataGrid = new DataGrid();
Errors:
1172: Definition fl.controls:DataGrid could not be found.
1046: Type was not found or was not a compile-time constant: DataGrid.
1180: Call to a possibly undefined method DataGrid.
This sounds very bad, especially considering that I need to use the DataGrid to complete my program.
What am I doing wrong and how can I fix it? This is AS3, right in frame 1 of the main timeline.
enable "Components" panel -> drag datagrid component inside the library panel -> it should work now