I have a main class in which 3 other class objects are managed and instantiated and are all extended from Sprite. My issue is getting these loaded correctly as I have external assets(images,and sounds) being loaded into these 3 classes. I have a simple .fla that is referencing the "Main". How would I go about ensuring the appropriate class instance is being loaded properly before it is added to the stage?
I have tried making the three classes inherit from the Loader class to retrieve the bytesloaded/total for the class but this causes other issues in the code that require attributes from Sprite. Would I need to preload ALL assets inside the class first, thus making the instantiated object on "Main" preloaded? Thank you for any help/advice.
Thank you all very much for your help. This particular application was being deployed in a learning management system. The issue was the application its self was freezing due to a .js script used for SCORM communication. However, it was very useful for me to learn how flash is managing instantiation, I really appreciate the help both of you provided.
Related
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.
I'm new to coding and AS3. I was reading about adding things to the stage using AS3 and learned about the addChild method. Reading more I found that there are different ways to use it. I also read that some ways are better than others, and that some ways are not good at all and better avoided.
I don't trust those sources though. As a coding newbie I come asking for help, StackOverflow. I'm about to start an addChild heavy project and I want to start it with the right implementations. I trust you, so let me ask you this: What's the correct use of addChild?
I just want to add things to the stage, but I read that it's not good to add them directly to the stage (without further argument though).
stage.addChild()
this.addChild()
addChild()
Are there other ways? Which one should I use?
Thanks for your time. :)
As the top-level container for all display objects in the display list hierarchy, there is only one Stage no matter how many SWF files are loaded into the runtime. So, generally, objects should not be added to the Stage, directly, at all. The only object the Stage should contain is the root object.
Generally, you should not use: stage.addChild()
Adding a DisplayObject to the display list should be performed within the scope of a DisplayObjectContainer.
Each SWF file has an associated ActionScript class, known as the main class of the SWF file which extends a display object. From this class or any child within the hierarchy you may call addChild().
The following are equal, and would add a child within the scope of the current display object container.
this.addChild()
addChild()
The this keyword explicity defines scope; however, is generally implicit when left off.
While a display object added via addChild() is added to the front (top) of all other children, to add a child to a specific index position, use the addChildAt() method.
References:
Display Programming
Basics of display programming
Adding display objects to the display list
I develop a project in FlashBuilder. The graphics and UI elements i create in the Flash IDE and give them Export Classnames. then I put the SWC in the library paths of the FlashBuilder project and create the UI elements by instanciation.
Now I want to add a Preloader for the application. I follow this article, which works:
http://pixelpaton.com/?p=4642
My question is now: I also need some graphics for the preloader. But how do I ensure that the graphics for the preloader will be loaded first, such that the preloader class can start as soon as possible?
The compiler will figure out the dependencies for your preloader class, and load them first.
For example:
public function Preloader()
{
addChild(new UIElementFromFlash());
}
Flash Builder will know UIElementFromFlash needs to be loaded before Preloader. It will also load Preloader before your main class and its dependencies as long as you have the Frame metadata tag from the article.
I'd suggest to make 2 swf files if it's possible. Make a loader.swf which loads your application swf and displays the progress and your loading animation/graphics.
See an example here: http://www.republicofcode.com/tutorials/flash/as3preloader/
Then you'll have to add URLLoader class and load your application.swf.
Try looking here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html
Like Gio suggested, having two SWF files is the best practice.
However, one other solution is to use the -frame two argument of the Flex compiler. All your definitions (code and assets) will be added to the second frame of the movie, allowing your main class to listen for the loading progress of the application and display animations.
Be careful to not reference anything from the main application in your first frame, to prevent adding more weight to it.
More detail on this technique here:
http://www.andymoore.ca/2009/08/flexsdk-3-3-how-to-make-a-flash-preloader-in-as3/
I'm building a flash app (just AS3 with FlashDevelop) and I'm having some trouble keeping things loosely coupled around the event system. I've done a lot of reading about central event systems and static eventdispatchers but they don't quite fit the bill for me.
What I'm building is similar to a video player. I have a Player class which is the parent of all the other little parts of the app. The Player class extends Sprite and I've currently designed it so that you could instantiate multiple Player's and put them on the stage. I also have a Controller class which extends EventDispatcher and I dispatch all my events through this class. It's a central event class.
The problem is that I need to pass around a reference to this class so that all the other classes can dispatch and listen through it. Passing around the reference works but it's the exact opposite of loose coupling. I know I could make a static EventDispatcher that all the classes could see but then if I had two or three Player's on the stage they would all hear each others events.
How can I create a type of sandbox that will allow all the child classes of a Player instance to be aware of the central dispatcher without passing a reference or making it static?
I'd suggest to use my static dispatcher, this one has an ID mechanism that allows to tell which Player instance dispatches an event.
It turns out what I was really trying to understand was loose-coupling using something like dependency injection.
I never ended up doing this in any AS3 projects as I don't do much of it anyway. Having done more C# recently I more easily grasped this concept using libraries such as StructureMap and Ninject.
For AS3 you could use a framework like Robotlegs. This will probably change the way you code AS3 and may not be for all developers/situations.
I have two ActionScript 3 projects, game(.swf) and minigame(.swf). At runtime the main game loads the minigame via Loader. I also have a shared library (SWC) of event classes, included by both, which minigame will need to dispatch and game will need to listen to.
First: Is this possible this way?
Second: What will happen if I compile the minigame, then change the event classes so they're incompatible, then compile the main game. Will Flash crash when trying to load the minigame SWF? (I hope so)
Third: And what will happen if I change the event classes, but in a way that preserves interface-level compatibility?
If you have classes named the same but not sharing the same functionality just use a different class path for each (i.e com.game.* com.minigame.*), this will avoid all conflicts.
Basically if a class named the same in several SWFs only the one loaded first will be used.
On the other hand if you really want to share the same classes between both of the SWFs, make sure you only include them in the main SWF (you can choose include/exclude in the publish settings of Flash IDE or in the compiler settings of Flex SDK). This will make the whole loading process a bit lighter and you will avoid weird conflict related bugs.