2

I'm building a Flash 4 Builder project and want to use an external actionscript file. Here is the structure I used...

http://img704.imageshack.us/img704/794/schermafbeelding2010121b.png

So, I want to be able to connect "actionscript.as" to the "OrderApp.mxml" file.

I add this <fx:Script source="assets/actionscript/actionscript.as"/> to my OrderAp.mxml file and a function in actionscript.as looks for example like this:

public function checkCode():void{
    if (txtToegangscode.text == "moia") {
        lblFeedback.text = "ok";
        txtToegangscode.enabled = false;
        btnGaNaarPersonen.visible = true;
        btnGaVerder.visible = false;
    } else {
        lblFeedback.text = "wrong"; 
    }
}

When I want to add some components, like "Toegangscode.mxml" I keep getting errors like "1120: Acces of undefined property lblFeedback". When I try to call the function checkCode() What do I do wrong?

Michiel
  • 7,855
  • 16
  • 61
  • 113
  • you try to add some components to your flex app after the script is added? – www0z0k Dec 15 '10 at 04:28
  • is package defined in this file? could you provide its structure? also maybe the compiler gets confused because actionscript is a reserved keyword :) however i've never heard about it – www0z0k Dec 15 '10 at 05:33
  • Hm... Defined package? What doe you mean? – Michiel Dec 15 '10 at 17:07
  • Please see my edits as I believe this will correct your problem. Sorry for not seeing this before. – Todd Moses Dec 16 '10 at 14:28

4 Answers4

2

You probably already found the answer you were looking for, however, there is this link to Adobe's site that has all the info you or other readers need.

http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf61c8a-7ff4.html

user580865
  • 21
  • 2
1

Problem solved... Apparently, you have to use a different .as file for every component! Nevertheless thanks to everyone who helped me out!

Michiel
  • 7,855
  • 16
  • 61
  • 113
0

EDITED:

Sorry I did not look carefully at your question.

Your problem is that the *.as file does not know what your components are:

You need to pass the components to the function like so:

public function checkCode(txtToegangscode:TextInput, lblFeedback:Label):void{
    if (txtToegangscode.text == "moia") {
        lblFeedback.text = "ok";
        txtToegangscode.enabled = false;
        btnGaNaarPersonen.visible = true;
        btnGaVerder.visible = false;
    } else {
        lblFeedback.text = "wrong"; 
    }

This will allow your *.as file to access the properties in those components.

OLD:

Here is the documentation: http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_4.html

You use the source attribute of the tag to include external ActionScript files in your Flex applications. This provides a way to make your MXML files less cluttered and promotes code reuse across different applications.

Do not give the script file the same name as the application file. This causes a compiler error.

The following example shows the contents of the IncludedFile.as file:

// usingas/includes/IncludedFile.as
public function computeSum(a:Number, b:Number):Number {
    return a + b;
}

The following example imports the contents of the IncludedFile.as file. This file is located in the includes subdirectory.

<?xml version="1.0"?>
<!-- usingas/SourceInclude.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script source="includes/IncludedFile.as"/>

    <mx:TextInput id="ta1st" text="3" width="40" x="170" y="24" textAlign="right"/>
    <mx:TextInput id="ta2nd" text="3" width="40" x="170" y="52" textAlign="right"/>

    <mx:TextArea id="taMain" height="25" width="78" x="132" y="82" textAlign="right"/>

    <mx:Button id="b1" label="Compute Sum" 
        click="taMain.text=String(computeSum(Number(ta1st.text), Number(ta2nd.text)));" 
        x="105" 
        y="115"
    />

    <mx:Label x="148" y="52" text="+" fontWeight="bold" fontSize="17" width="23"/>
</mx:Application>

The source attribute of the tag supports both relative and absolute paths.

The source attribute of the tag and the include directive refer to files in different ways.

The following are the valid paths to external files that are referenced in an tag's source attribute:

Relative URLs, such as ../myscript.as. A relative URL that does not start with a slash is resolved relative to the file that uses it. If the tag is included in "mysite/myfiles/myapp.mxml," the system searches for "mysite/IncludedFile.as".

For an ActionScript include directive, you can reference only relative URLs. Flex searches the source path for imported classes and packages. Flex does not search the source path for files that are included using the include directive or the source attribute of the tag.

Todd Moses
  • 10,969
  • 10
  • 47
  • 65
  • Thanks for your answer! But no problem solved here... For starters, my Flash Builder 4 doesn't accept the "" tag. It converts it to an "" tag. And assuming in your button, you have an -click="checkCode()"- click argument. And you have to use the function given in my openingpost. How do you communicate between actionscript files and components? – Michiel Dec 15 '10 at 17:16
0

Looks like you are missing the double quote at the start of the string?

lblFeedback.text = wrong";

should be...

lblFeedback.text = "wrong";

Why not put this code into a class then you can detect any compile errors?

Chris Bos
  • 335
  • 1
  • 6
  • Hmm... Typo. I do use 2 quotes... The functions are in an actionscript class... But I keep getting this error: "1120: Acces of undefined property lblFeedback" – Michiel Dec 15 '10 at 21:44
  • I'm a bit confused as to how you are trying to use it? – Chris Bos Dec 15 '10 at 22:12
  • Where is lblFeedback defined? In Toegangscode.mxml or in the OrderApp.mxml? Also only OrderApp.mxml adds the actionscript source code right? – Chris Bos Dec 15 '10 at 22:20
  • lblFeedback is in the component Toegangscode.mxml. The actionscript source is only defined in OrderApp.mxml, because actionscript.as files only supports one component, right? – Michiel Dec 16 '10 at 22:52
  • You could write the code in the actionscript file to be more generic then you can include it in more components if you wanted to. Anyway I think your problem is the lbl is defined in Toeganscode but is being referred to in OrderApp as if it was a child in OrderApp... it should be something like this txtToegangscode.lblFeedback.text = "ok"; – Chris Bos Dec 17 '10 at 14:32