|
-
Senior Member
[Help] Hello World
hello every one, I trying to get move from AS2 to AS3. I get errors in AS3 that I don't understand. I saw this code on this form but I can't get it to work.
They are both in the same file directory
afx.as
Code:
package {
public class Greeter {
public function sayHello():String {
var greeting:String = "Hello World!";
return greeting;
}
}
}
afx.fla
Code:
var myGreeter:Greeter = new Greeter();
mainText.text = myGreeter.sayHello();
The Error
PHP Code:
5000: The class 'afx' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Thank you for the help
CEO OF
-
Senior Member
I don't know what afx is but definitely you must extend the greeter class:
either
public class Greeter extends Sprite
or
public class Greeter extends MovieClip
- The right of the People to create Flash movies shall not be infringed. -
-
Actually, if I read his code right, he does not need to extend Sprite or MovieClip, because the Greeter itself is not on stage.
Two things:
1. The code you posted as afx.as should be in Greeter.as.
2. You can either have an Afx.as file with the second snippet (and use that as the document class), or just put it in the actions of afx.fla (as I suspect you have done)
I think the error you are getting is because you did set the document class, but did not need to. And you set it to a class that defined Greeter instead of afx. Here's how I'd change it.
Greeter.as (same as your afx.as):
Code:
package {
public class Greeter {
public function sayHello():String {
var greeting:String = "Hello World!";
return greeting;
}
}
}
Afx.fla (NO document class listed. delete or rename existing afx.as file):
Code:
var myGreeter:Greeter = new Greeter();
mainText.text = myGreeter.sayHello();
Also , you must have a TextField on the stage with an instanceName of "mainText". Technically, you could do that with code instead, like so:
Code:
var mainText:TextField = new TextField();
addChild(mainText);
var myGreeter:Greeter = new Greeter();
mainText.text = myGreeter.sayHello();
On further reflection, that error indicates you've got some symbol in your library that thinks it's a Greeter. Why? You shouldn't need that.
Last edited by 5TonsOfFlax; 06-28-2007 at 10:15 AM.
Reason: yet another possibility
-
Senior Member
5TonsOfFlax!!! OH MY GOD "hello world" never looked so good!!! Thanks a lot for the help. I removed the document class from the fla. and renamed the afx.as to "Greater".
I thought that document class in the flash 9 IDE was what I had to use to link a class to a fla.
Does the class's name and it's .as file name have to be the same?
thanks again you gave me that first time cout<< statement type feeling
CEO OF
-
The document class is how you get your ROOT to be a particular class. By default it will be a MovieClip. So, if you're not doing anything you can't do with a MovieClip, you don't need to set the document class. In your case your root is doing only ordinary MovieClip stuff. Greeter is doing some trivial class-based stuff, so it needs to be its own class. If it was on the stage, it would need to extend something that extends DisplayObject, but it's not.
If you really named the file "Greater.as", then no, apparently they don't need to be the same . I'm not sure on this one, I always name mine the same because I'm from a Java background where it is most definitely required. I don't know how the compiler could find the proper class if they are not the same.
Last edited by 5TonsOfFlax; 06-28-2007 at 01:03 PM.
Reason: it's != its
-
I believe they are supposed to be the same, but I also thought all classes needed a constructor function too.
-
half as fun, double the price
A class and its file name should be the same. Constructors are required, but if you don't provide one yourself, it will be generated for you automatically behind the scenes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|