I have a MovieClip called PopUp where I wrote some code.
I need to make another MovieClip that uses pretty much the same code...can I just use the PopUp class as the base class for my other MovieClip?
They look different but they have the same instances on the stage and stuff.
You can just create a new class called PopUpBase, and let your new MC's extend that class. Generally changing the base class in the IDE can cause some problems, so it's best practice not to, but to create classes (and .as files) for each MC that needs code.
Depending on what you're doing with this, it might be okay just to change the base classes to PopUpBase (in the IDE) :)
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 am trying to get a reference to an object that is placed on starling stage named "_shooterHero" from an external class which is "bullet".
I have tried using getChildByName with:
starling.core.stage and starling.current.nativeStage, but without any luck
The code right now:
var hero= Starling.current.stage.getChildByName("_shooterHero");
trace (hero.name)
There are a few ways to solve your problem:
Make hero publicly accessible. When you create a new hero (wherever that is) you can also add public getter/setter methods to allow your external class to access it.
Create a "model" class that can be referenced by the singleton getInstance() pattern. Then proceed as in (1).
Are you sure the sprite has been added to the stage? You can check this by doing Starling.current.stage.getChildByName(hero); although for this would need to have access to the hero variable.
What I am trying to do is add a MovieClip which I have created, but deleted from the actual stage itself, from a seperate class to a sprite which I declare in the document class.
The link between the main document class and the others works fine, as well as references to the stage, from the class in question.
It is only one line of the function which is giving me the error: Type was not found, or is not compile-time constant:SequenceA
I have already tried changing the settings for 'Export for Actionscript' in the library properties and still get the same message every time.
Pretty lost with this one so any help is appreciated. Cheers!
What have you set as the "Class" in your Export for ActionScript settings? It looks like you're trying to refer to an instance rather than the class itself. For example, mc is an instance of the MovieClip Class.
Assuming you've chosen the Class "SequenceA", you would do this instead:
var seqA:SequenceA = new SequenceA();
There's a little bit more detail about objects and classes here.
I wrote a nice little game which works well in its .fla form. Because I wanted to include some menus with options, saves, etc, I followed the advice from http://www.actionscript.org/resources/articles/965/3/Better-Flash-Navigation-using-AS3-Classes/Page3.html I managed to get navigation from the menus right, but I really don't know what to do with the contents of my fla.
Ideally, I think that it should sit in a class on its own (called Game.as) which if you looked at the link above would be linked from the Main.as (which is the linked class [that navigates the menus] from the main .fla file which holds all the assets). Am I right to think that the constructor class in Game.as would be very similar to an init() function in my .fla file ? In which case, the rest of the code for the game would be separate functions (called methods for classes, right?) stored outside the constructor. Or am I completely wrong and the game should not be in class on its own but instead sitting inside the Main.as because this very class is linked to the original .fla where all the assets are? All I need is a little nudge in the right direction because I can't find this info in any of my books and I don't know how to phrase it for google. Thanks in advance. Nik
You need to Export all the MovieClips and assets you want to access from classes in your FLA file. Open the properties of your MC or Bitmap and mark the export and write the package name into it.
http://www.flashandmath.com/howtos/as3link/
To the rest:
Every object is another class. This is object oriented programming. Your Menu is a class, your menu items are different classes. Your game is a class, your game items are different classes and so on.
With this you can create a class hierarchy and can "load" items you need at the specific situation.
http://www.adobe.com/devnet/actionscript/articles/oop_as3.html
Am I right to think that the constructor class in Game.as would be
very similar to an init() function in my .fla file ? In which case,
the rest of the code for the game would be separate functions (called
methods for classes, right?) stored outside the constructor.
Yeah, you're right.
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.