Search code examples
actionscript-3flash-cs3

Script copied from adobe adobe tutorial and still not working zero result


This is a script which asks for and validate a user name against a predefined list of names.

I copied the whole script from tutorial but result is nothing i cannot even understand why no errors ! I learn by first copying the script and afterward understanding it but unfortunately the result is nothing with no errors ! i am new to programming so please try to explain things

script in frame 1 :

var myGreeter:Greeter = new Greeter();
mainText.text = myGreeter.sayHello("")

script in an action script file named greeter:

package
{
public class Greeter
{
/**
* Defines the names that should receive a proper greeting.
*/
public static var validNames:Array = ["Sammy", "Frank", "Dean"];
/**
* Builds a greeting string using the given name.
*/
public function sayHello(userName:String = ""):String
{
var greeting:String;
if (userName == "")
{
greeting = "Hello. Please type your user name, and then press the Enter key.";
}
else if (validName(userName))
{
greeting = "Hello, " + userName + ".";
}
else
{
greeting = "Sorry, " + userName + ", you are not on the list.";
}
return greeting;
}
/**
* Checks whether a name is in the validNames list.
*/
public static function validName(inputName:String = ""):Boolean
{
if (validNames.indexOf(inputName) > -1)
{
return true;
}
else
{
return false;
}
}
}
}

Solution

  • Presumably you're referencing Adobe's Getting started with ActionScript: Creating a basic application.

    This older tutorial is more geared towards Flex 3, in which you would implement a front-end presentation to wire to the Greeter class.

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" 
        layout="vertical"
        creationComplete="initApp()">
    
        <mx:Script>
            <![CDATA[
                private var myGreeter:Greeter = new Greeter();
    
                public function initApp():void 
                {
                    // says hello at the start, and asks for the user's name
                    mainTxt.text = myGreeter.sayHello();
                }
    
            ]]>
        </mx:Script>
    
        <mx:TextArea id="mainTxt" width="400" backgroundColor="#DDDDDD" 
                     editable="false"/>
    
        <mx:HBox width="400">    
            <mx:Label text="User Name:"/>    
            <mx:TextInput id="userNameTxt" width="100%" 
                          enter="mainTxt.text=myGreeter.sayHello(userNameTxt.text);"/>
        </mx:HBox>
    
    </mx:Application>
    

    By simply pasting the Greeter class, you would not receive errors since no part of this class is executing. Nothing from that class is being called.

    This is a rather poor tutorial, and combined with being dated it would be advisable to look elsewhere for tutorials and examples such as: