开发者

How to use an external actionscript file with flex

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 t开发者_开发知识库o call the function checkCode() What do I do wrong?


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


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


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜