Creating a class for a Flash symbol in Haxe? - actionscript-3

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.

Related

Need some help getting started with OOD in ActionScript 3

So being new to AS3 but not development in general, during a recent project that I just started I hit an immediate snag where my normal method of OOD is causing errors in AS3.
I created a layer in Adobe Flash called code just to keep everything separate and in frame one under actions I used the following code to get started:
var GameContainerSize:int = 400;
class GameInfo {
var GameID:int;
var HomeTeam:String;
var VisitingTeam:String;
function GameInfo()
{
}
}
This simple code immediately causes an error though
Scene 1, Layer 'Code', Frame 1, Line 4 1131: Classes must not be nested.
And my best guess is this is because the timeline and all code on it exists within a class already. So what should I be doing if I want to develop this program using class objects, or is this even possible in flash?
You can't define classes on the timeline. Each timeline is exported as a class, var and function declarations become members of the class, and frame code is moved to its own frame functions which are called when the timeline reaches that frame. Thus the error is saying "you can't put a class in a class" because the timeline already is a class.
To define classes you must use external .as files with package blocks:
// GameInfo.as
package {
public class GameInfo {
public var GameID:int;
public var HomeTeam:String;
public var VisitingTeam:String;
public function GameInfo(){ }
}
}
In Flash Pro you can link a class to a timeline (document or symbols). Such a class must extend Sprite or MovieClip.
You can also refer to any of your classes from any frame script. For example, you could put on frame 1 of your main timeline:
var gameInfo:GameInfo = new GameInfo();
Typical OOP would involve using external .as class files for almost everything, with only minimal function calls on your timeline frames, like stop() at the end of a timeline or calling a custom function you've added to the class linked to the timeline.
I created a layer
That's not ideal. The problem is that it will not get you any closer to understanding the code, because it isn't code. It's a feature of the flash authoring environment. You might as well spend a day with the drawing tool drawing something.
to keep everything separate and in frame one under actions
Organisation is important, but layers aren't the way to go. As each class definition goes into its own external file, you have to create additional files anyway if you want to create more classes. And you want to create more classes because having only one is horrible object oriented design. Being consistent here is important. That's why people in the comments suggested to use a document class and have no code on any frames. All code is organised in classes. No exceptions1.
Having said all that, I highly advice against using Adobe Flash to learn As3. There's too much obfuscation going on behind the scenes. If you want to learn how to code As3, use a code editor. (or plain text editor + compiler if you prefer). Learning what settings dialog has to be adjusted in order to get what you want is not going to get you any closer to understanding OOD in As3.
I also see package, is this kind of like a namespace and does it need to be named, if not what is its purpose?
No, packages are packages and namespaces are namespaces. Apples and oranges.
Packages are used to organize classes just like a structure of
folders is used to organize the .as fiels of those classes. In fact,
the package is pretty much exactly that folder structure, for example:
flash.display is for all display related classes
flash.events is for events
A namespace allows you to control access to members of a class. Namespaces are very rarely used in As3. Here's an article from grant skinner about namespaces. Personally, I never needed to use namespaces. If you are jsut getting started, you can very well ignore them for now.
That sounds perfect! except I cannot get it to launch on my Win10 machine. I may just end up outsourcing this at this ratio
flash develop is the alternative editor. It's free, fast and lightweight.
my normal method of OOD
You want to extend your definition of normal with
always explicitly specify the access control modifiers
member names start with small letters
public class GameInfo {
private var gameID:int;
private var homeTeam:String;
private var visitingTeam:String;
function GameInfo()
{
}
}
1admittedly, there are top level functions that are pretty much free floating functions without a class. If you want to do oop, this is not what you want. It's for the occasional independent utility function. Stick to classes for now.

AS3 Document class - having to import classes which are directly accessible in Timeline code

I am trying to get started with Document class in AS3 with Flash CS4.
There is some existing code in the timeline, but for now I've been trying to write just the new code in the Document class.
I have used classes and functions like URLRequest, Event, navigateToURL, ContextMenu etc in the timeline directly without importing them explicitly. And this works.
Once I associated the document class, by keeping the above mentioned code in the timeline frame itself, compilation errors were thrown for the mentioned classes. I had to import them - in timeline or in the Document class - for the code to compile correctly.
What is the fundamental difference here, which enables timeline code to access these classes directly without importing? Can this be done while having a Document class also?
For reference, my Document class basic code looks like this:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip { }
}
Document classes need to have import statements. Infact, for the most part timeline code does too; I can't seem to be able to get any timeline code to work with the classes you listed without the appropriate imports.
It's worth saying that explicit imports are a good thing; they allow you to manage your code and more understand where any potential savings can be made. For the most part flash (or your IDE of choice) will do them for you if you use auto-complete (Ctrl + Space in Flash).
There are ways to avoid certain imports via class paths, but really, I would bite the bullet and just use import statements.

Actionscript 3 getDefinitionByName not seeing classes

I've worked around this with a hack for now, but would rather know if there's a right way to do it. I have a function that churns out MovieClips, which are tiles for a map that I then attach to the stage. It determines which class of tile to use based on a string variable, like this:
// symbolID holds our class name, determined by logic above
var newClass:Class = getDefinitionByName(symbolID) as Class;
var newtile:MovieClip = new newClass();
This works, but only if an instance of the class already exists somewhere else in the code. It could be anywhere-- in the document class, in some buried function of a helper class, it doesn't seem to matter. If not, Flashdevelop throws error 1065, "Variable (the variable) is not defined". I mention that I'm using Flashdevelop because it seems like it might be compiler-specific, but I'm not sure.
My hack fix is to do this:
var a:baseTile;
a as anotherTile;
a as aThirdTile;
and so on, which works, but definitely isn't ideal if I'm going to have hundreds of these tile classes eventually.
Edit: I should add that these movieclips are coming from a .swc file, which comes from Flash Professional.
You have to use the 'hack'.
getDefinitionByName() can only work with classes that exist at runtime. Unfortunately, if you don't make use of a class, it won't be compiled and won't exist at runtime.
Library symbols make getting around this a little easier. If you check the box that has them available automatically at a given frame, you can just make sure your getDefinitionByName() calls are done during or after that frame.
While SWC libraries can include specific classes to include in the build path, Flash compiler will not link unreferenced classes to a SWF; therefore, requires a linkage library.
Example linkage to retain classes:
/** linkage library */
private static const classA:ClassA;
private static const classB:ClassB;
private static const classC:ClassC;
Another option would be to load the classes from a RSL (Runtime Shared Library).
Basically yes, you need to have a strict reference of that class somewhere in your code. You can even make that reference "unreferenced" elsewhere. I have a hundred of classes like this, and I had to make a single Array of these classes, located somewhere inside the project. I have placed it aside the function that calls getDefinitionByName() to make sure the classes are available in that function.
private static const dummy:Array=[Rock01, Rock02,...,Gem01,Gem02,...];
So you can use such an Array listing all your tiles that you have in your project and want to be accessible by getDefinitionByName().

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.

ActionScript 2 - ActionScript 3: common subset?

I'd like to automatically generate ActionScript classes for a flash client side of one of my projects. (These projects have a formal way of describing my models that is already used to generate SQL and a admin interface).
Now, the ActionScript should/could be compatible with ActionScript 2 and 3. Is there a description of a maximal common subset of features available somewhere?
I think you will be stumped right of the bat, because AS2 and AS3 declare there classes/packages differently.
If you are looking to create Classes from templates, then I would suggest using FlashDevelop. It has a get templating system.
ActionScript 2 Class/Package example:
class com.yourpackage.YourClass extends MovieClip {
function YourClass() {
//contructor
}
}
ActionScript 3 Class/Package example:
package com.yourpackage {
public class YourClass extends MovieClip {
public function YourClass():void {
//contructor
}
}
}
But this is all just syntax. There are a lot more fundamental deferences between the languages than just formatting. There is also a different way of thinking.
One possible alternative to enable you to do this, would be to use Haxe.