Having a nightmare with AS3 classes and using them - actionscript-3

I really thought I understood the whole AS3 class thing but I have been having a major problem with the most simple of tasks and it is killing me!
Basically, I have my Document Class and am trying to create an object of another class. Both files are in the same folder.
I am currently using:
var sForest:simpleForest = new simpleForest();
In the hope of referencing a class that has this code (I took out everything it does and it still has the same problem):
package ActionScript
{
public class simpleForest {
public function simpleForest() {
trace ("hi!");
}
}
}
And I keep getting these errors:
1046: Type was not found or was not a compile-time constant: simpleForest.
1048: Method cannot be used as a constructor.
I know I am missing something small and stupid but I can't figure it out at all!

Related

Using a separate .fla file as a MovieClip in a larger project

Just to preface: I haven't used Flash in a long long time, however, I am still aware of the environment.
Backstory: I created a small .fla to perform actions on a MovieClip on the stage (in my case, a health/HP bar). I made the health effect using a Document Class (HealthBar.as).
The question: What I'm trying to figure out now is how, in a totally separate .fla, to create multiple instances of these health bars and be able to access the methods in Document Class HealthBar.as from the Document Class in this new .fla
If I am doing this incorrectly in the first place, feel free to yell at me, and let me know how doing something like this SHOULD have been done.
Thanks for any help
You're halfway there with a document class. Now you just need to make it into a proper class in your com.domain.className (or drop the .as file into the same directory as your fla). While creating classfiles is trivial, online examples seem to muck it up, so here's Adobe's official demo (bleh).
That said, creating more healthbars basically looks like this...
Class
package {
public class HealthBar extends Sprite {
public function HealthBar() {
// constructor
trace("Healthbar created")
}
}
}
Document Code
import HealthBar;
for (var i:int = 0; i < 10; i++) {
var randomHealthBar:HealthBar = new HealthBar(); // <-- magic sauce
addChild(randomHealthBar);
}
// traces: "Healthbar created" 10 times
Shouldn't you be able to copy the movieclip from the healthbar .fla into your main .fla and then give it an actionscript linkage of HealthBar?
Then you should be able to call new HealthBar whenever you need one in your main file.

1180: Call to a possibly undefined method DisplayObject

A few days back I created a simple point and click game and the following error occurred
1180: Call to a possibly undefined method DisplayObject.
This is my code:
poster.addEventListener(MouseEvent.MOUSE_DOWN, clickposter);
stop();
function clickposter (event:MouseEvent):void
{
removeChild(DisplayObject(event.target));
}
Now the weird thing is, this code worked fine at first but as soon as I added a document class to my project it stopped working and gave me the 1180 error.
My document class is nearly empty:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
}
}
}
I have searched around a bit but wasn't able to solve this error.
Importing DisplayObject into your document class will fix this:
import flash.display.DisplayObject;
I'm afraid I don't know exactly what causes the issue as the code for the poster object is separate to the document class code (I assume), sorry it's not the most informative answer but it'll get your code working at least.

How to extend class that is inside a loaded swf

I've got an swf loaded externally to my app with loader component. It is loaded to the Application Domain of my loader. I've got a class in that swf wich I would like to extend and override some functions from it. Is it possible somehow?
Here is some code to explain what I want(of course, it is fully incorrect and wold not work):
public var ClassFromLoadedSwf:Class = ApplicationDomain.currentDomain.getDefinition("path.to.needed.class") as Class;
public class MyClass extends ClassFromLoadedSwf
{
override protected function initMovie() : void
{
//function logic goes here
}
}
Thank you for your answers and sorry for my bad english.
No you can't. Basically you don't understand the meaning of ApplicationDomain. Using a loader and Applicaiton Domain you just merge some code to your SWF in Runtime. Before it reaches the Runtime state, it can't be accessed. So at the Compile time you can't do what you're trying to do now. But you can try to use SWC instead of SWF. You just include it in your Project Library and that's all. You'll be able to access all the classes at compile time. Try reading this Article. It will help you understand the meaning of SWCs.

Creating a class for a Flash symbol in Haxe?

I am having trouble incorporating graphical assets created in Flash with my Haxe code.
In the Flash IDE, I've created a symbol with the linkage name "MySprite". I compile this into assets.swf. I know that to use symbols in this .swf from my Haxe code, I need to add the following option when using the Haxe compiler:
-swf-lib assets.swf
I'd now like to write a class called "MySprite" which is associated with this symbol, like so:
class MySprite extends Sprite {
public function new() {
// ...
}
}
Basically, I'd like to achieve something similar to the technique presented in this tutorial:
package {
import flash.display.*;
[Embed(source="assets.swf", symbol="MySprite")]
public class MySprite extends Sprite {
public function MySprite() {
// ...
}
}
}
It's unclear from the Haxe documentation whether this can be done, or what the syntax is for doing it.
I think so, but I'm not sure, Haxe doesn't override classes from assets.swf with the classes you declared. There was a discussion about it on the mailing list (the old one, not in the Google groups), and this was the decision... I don't know why this decision was made.
You could still do it with SamHaxe. At least back in the days I was able to. Unfortunately, SamHaxe was abandoned, and if there are bugs or something not working as you need it - you are pretty much on your own. The good thing about Sam is that it's relatively small project. It's written in Haxe and I was able to build it from sources.
You could also try: http://code.google.com/p/hxswfml/ The project seems to be functional and the author used to reply to users. It might be a tad more complex though. I'm quite sure it was possible to do, but you will probably need to ask the author / figure it out yourself.

1119:Access of possibly undefined property Click through a reference with static type Class

I am making an edutainment game using flash cs5, I'm really new at using flash, in fact we never yet tackle it at school, but I insist on learning about it.
In my codes, I encountered this error
C:\Users\acer\Desktop\JikanLibrary\Main.as, Line 16 1119: Access of possibly undefined property Click through a reference with static type Class.
This is the code I used in my program
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
var startPage:StartPage;
var jikanBookshelf:JikanBookshelf;
public function Main()
{
startPage = new StartPage;
jikanBookshelf = new JikanBookshelf;
startPage.jikanBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);
addChild(startPage);
function onJikanBookshelf(event:MouseEvent):void
{
addChild(jikanBookshelf);
removeChild(startPage);
}
}
}
}
The error is in this line
startPage.jikanBookshelf.addEventListener(MouseEvent.Click, onJikanBookshelf);
Since I'm really new at flash, I really don't know what went wrong with my codes, it was working a while ago before I put the mouse event. I hope someone could help me.
ActionScript is a case sensitive language. This means that Click is not the same as CLICK. So what you need here is MouseEvent.CLICK
"Why is CLICK all uppercase? Most property names aren't.", you might ask.
That's because CLICK is a static constant property of MouseEvent and the convention amongst ActionScript (and many other languages) programmers is that static constants are written in all uppercase to distinguish them visually from other variables.
'static' means it's a property of the MouseEvent class, not of an instance of MouseEvent.
'const' means it's not a variable: you can't change it's value.
It's a name conflict problem: The class definition name is same as the object name.
The problem in your script is that you have a class definition name startPage and you are trying to create an object of same name startPage.
You have to change the object name to something different. Let say startpage1.