Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to get Started with Obj Oriented AS3? (Flash Help seems broken)

Guest
Jun 26, 2007 Jun 26, 2007
OK I dont want to be the noob that doesn't read the manual and asks stupid questions, but I'm stuck here. It's been a LONG time since I had any real flash experience (Flash 5 in depth and a little Flash 8). But now my job has lead me back into some flash work & I'm basically starting from scratch. I'm trying to just get a simple SWF to load an external class & have the class retrun a value from a method.

I define

package
{
public class Greeter
{
public function sayHello():String
{
var greeting:String;
greeting = "Hello World!";
return greeting;
}
}
}

in an .as file (which is located in the same directory as my .fla) and then in frame 1 of my time line I have:

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

When I try & test the movie I get 2 errors:

1046: Type was not found or was not a compile-time constant: Greeter.
TOPICS
ActionScript
323
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Jun 26, 2007 Jun 26, 2007
quote:

Originally posted by: Newsgroup User
I'm only getting one, which is "Incorrect number of arguments. Expected
no more than 0," and the reason for that is because the sayHello() method
isn't set up to receive any parameters.


Under most circumstances I'd have been skeptical of passing an argument when none is defined however I thought I'd heard (way back around AS 2) that AS was being based on JavaScript sort of rules so I thought perhaps that didn't matter. And unfortunately, that err...
Translate
LEGEND ,
Jun 26, 2007 Jun 26, 2007
CPruittCCB,

> var myGreeter:Greeter = new Greeter();
> mainText.text = myGreeter.sayHello("Bob");
>
> When I try & test the movie I get 2 errors:

I'm only getting one, which is "Incorrect number of arguments. Expected
no more than 0," and the reason for that is because the sayHello() method
isn't set up to receive any parameters. If you change your existing ...

public function sayHello():String

... to this ...

public function sayHello(msg:String):String

... just as a for-instance, that error goes away. Have you saved both the
FLA and the AS file? If you haven't saved your FLA yet, it won't know where
to find your Greeter.as file (that is, the Greeter class), which would
explain your "type not found" error.

Check Edit > Preferences, ActionScript category, and click the
ActionScript 3.0 Settings button to make sure your classpath setting has an
entry with nothing but a dot (.) in it. That dot means "Look in the current
folder" when Flash needs to start hunting for external classes. Otherwise,
you'll have to use the import statement to let Flash know where that Greeter
class is.

Another thing to consider, if you're expecting to see the word "Bob" in
the text field, is that you're not currently passing the incoming parameter
back out of the method (because there is no incoming parameter). Something
like this would do it:

package {
public class Greeter {
public function sayHello(msg:String):String {
var greeting:String;
greeting = "Hello, " + msg;
return greeting;
}
}
}


David Stiller
Co-author, Foundation Flash CS3 for Designers
http://tinyurl.com/2k29mj
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 26, 2007 Jun 26, 2007
1. there's no string to pass to your sayHello method
2. you must save your Greeter class file as Greeter.as
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 26, 2007 Jun 26, 2007
quote:

Originally posted by: Newsgroup User
I'm only getting one, which is "Incorrect number of arguments. Expected
no more than 0," and the reason for that is because the sayHello() method
isn't set up to receive any parameters.


Under most circumstances I'd have been skeptical of passing an argument when none is defined however I thought I'd heard (way back around AS 2) that AS was being based on JavaScript sort of rules so I thought perhaps that didn't matter. And unfortunately, that error wasn't my typo, its actually printed that way in the built in Flash help (see: Getting started with ActionScript > Example: Creating a basic application).

Thanks for clearing that up, although I wasn't getting the error you mentioned. The compiler never got that far before it bailed.

quote:

Originally posted by: kglad
2. you must save your Greeter class file as Greeter.as


OK, this turned out to be the problem... Just on a hunch I decided to consider case. Saving the file as [greeter.as/i] errors out but [ Greeter.as/i] does not. Is Flash really case sensitive with regard to file names? At any rate I've got my bearing a little better. thanks a lot.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 26, 2007 Jun 26, 2007
LATEST
flash is almost NEVER insensitive to case.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines