I've bought an external component composed by the MXP component and two external swf.
Obviously I haven't any source or fla file.
I've imported the component in my own project and it works fine (combined with the two external swf). Now I've debugged and decompiled one of this two external swf whith a stand alone program (SWF Decompailer) in order to find two functions I wanto to manage.
I've found their name [forceNextImage() and forcePrevImage()] and the class where they are declared.
The problem is that those functions are protected and I've the necessity to call them inside my project (the project where I've imported the components, of course). There's a way to do that? And how?
I hope I've been understandable even with my lacking english, but if not don't esitate to ask me..
Thanks in advance.
Fabrizio
Can you subclass it, and then call it from the subclass?
protected methods are callable from subclasses, so you can extend the component and expose them:
public class YourComponent extends Component
{
public function nextImage():void
{
forceNextImage();
}
public function prevImage():void
{
forcePrevImage();
}
}
Related
I'm loading an external SWF as you normally would and I am handling the COMPLETE listener with:
var documentClass:Object;
function onComplete(loadEvent:Event)
{
documentClass = Object(loadEvent.currentTarget.content);
}
This works perfectly and I can access variables and functions from the document class of the external SWF. However not all the other classes in the SWF's library aren't instantiated in the document class. I would also like to access variables and functions in these other classes, and I am currently using, for example:
var documentClass:Object;
var classOne:Class;
function onComplete(loadEvent:Event)
{
documentClass = Object(loadEvent.currentTarget.content);
classOne = loadEvent.target.applicationDomain.getDefinition("ClassName") as Class;
}
This also works. However, there are multiple other classes in the library which I want to access and it is extremely tedious to go through each of them using this method. I was hoping I could use getQualifiedDefinitionNames() (I'm using Flash CC and player 11.3 so it is available) but when I trace it, it doesn't seem to be working.
There has to be an easier way to access the other classes which I don't know of. Can anyone help?
Thank you,
James
I know global variables are supposed to be bad but is it possible to create global classes? I am creating an application and I want to have one class that handles sound. From any class I would like to be able to say soundhandler.playSound(); without having to pass references all over the place. It should just know it is there.
Any help greatly appreciated.
You're referring to static members.
Your class SoundHandler would have a static method called playSound(), which can be implemented like so:
package
{
public class SoundHandler
{
public static function playSound():void
{
// #todo Logic
}
}
}
Your playSound() method is now accessible via:
SoundHandler.playSound();
Note: You mentioned global methods being bad, however this is a perfect candidate for these and something I would actually recommend (as much as I hate using static).
Additional: ActionScript 3's Math class contains mostly static members e.g. Math.round()
Your question (comment): Do I need to initiate SoundHandler in the document class?
No, in fact you shouldn't make an instance of SoundHandler at all. The only requirement is that you must have SoundHandler imported in your current class to access it:
import yourpackage.SoundHandler;
I am trying to better understand the AS3 OOP structure and organization, but I am having some problems wrapping my head around it. I want to create multiple class files and it seems that best practice is not to and to put all classes in one file? I have searched for hours on the web and came up with little for good examples. Maybe seperate files is not the way to go while using AS3, but to me it only makes sense for modularzation. The files I have been playing with are:
Main.fla
Main.as (document class)
TestOne.as
TestTwo.as
TestThree.as
TestFour.as
TestFive.as
I have created a folder called classes to house all class files except the Main.as which resides with the FLA.
All five Test classes are the same code except the file name and class name.
Here is how I am importing the files:
Main.as
package classes
{
import flash.display.MovieClip;
import classes.TestOne;
import classes.TestTwo;
import classes.TestThree;
import classes.TestFour;
import classes.TestFive;
public class Init extends MovieClip
{
trace("This is Main Class");
var testOne : TestOne = new TestOne;
}
}
TestOne.as
package classes
{
import flash.display.MovieClip;
public class TestOne extends MovieClip
{
trace("This is TestOne");
public function testing():String
{
return "This is the testing method";
}
}
}
Are the above examples I created good AS3 OOP practices? I understand these are real basic classes, but I think it should get the point arossed.
I am using CS3
Might be good to look at the Flex SDK coding conventions and best practices. It is a point by point rundown of how you should be using ActionScript 3. There is quite a bit of OOP stuff scattered throughout, so take a good skim through it. I think it is worth any new or experienced AS3 devs to have a read, because there is a lot of useful information there.
OOP is a big subject but here is a good primer.
ActionScript 3 design patterns will be also useful for you.
As Nathan said, big subject.
The thing about having all classes in one file is related to the topic "visibility of a class" and "access modifiers".
The modifier is that what is written before the class keyword. It can be public or internal. Internal means, that only code that is placed in the same directory (package) of the internal class can create an object of it. Public means, any code can create an instance of the class. Random link via google: http://flexmaster.blog.co.in/2010/05/20/action-script-use-access-modifiers-with-classes-and-class-members/
You are allowed to have multiple classes in one file. But then only one can have the modifier public. All others need to be internal. If you leave out the modifier, a class is by default internally visible. An internal class in a multiple classes file can be accessed only from within this file.
There are two more modifiers: protected and private. Both are applicable only to properties or methods. As an advanced developer you can even define you own modifier by using namespaces.
The rules of OOP still apply to AS3
1) Encapsulation.
2) Inheritance.
3) Abstraction.
4) Polymorphism.
Apply them or not is your choice but it is good practice.
And don't forget the most important rule "KISS" (keep it simple stupid)
I also want to point out your code has an error
package classes
{
import flash.display.MovieClip;
public class TestOne extends MovieClip
{
trace("This is TestOne");// this line is not inside a function and will most undoubtly error out your app.
public function testing():String
{
return "This is the testing method";
}
}
}
I think you either did something wrong when you copied the code into SO or you're not placing the trace statements correctly. You need to place the trace("This is TestOne"); inside a constructor in TestOne.as, like this:
public function TestOne() {
trace("This is TestOne");
}
The same goes for the code inside the Init class, which now reads:
trace("This is Main Class");
var testOne : TestOne = new TestOne;
but should be(note the bracers after new TestOne):
public function Init() {
trace("This is Main Class");
var testOne : TestOne = new TestOne();
}
What happens when you run your SWF, is that the constructor of the class Init will:
1) Trace "This is Main Class" to your console.
2) It will construct a new object(thus the name constructor) by calling the constructor of the class TestOne.
If you were to add this line to the end of the constructor in the class Init:
testOne.testing();
you should see this in the console: "This is the testing method".
If you would now comment out the line: var testOne : TestOne = new TestOne(); and run the SWF again, you'll get an error telling you something is null. This is because you attempt to call the method testing on an object that does not exist.
I do realize this is primarily fixing some coding errors of yours, and not so much helping you understand OOP. But hopefully you can pick up some help regarding object construction. I see some answers already mention key OOP principle that you really should look into.
Edit:
Remember that duplicating code is never a good thing, so if you find that all of the classes TestOne - TestFive contain the same code, except for some minor detail. You should probably change them into one class.
In your case you could for example change TestOne so that the constructor accepts a String, and then in your testing function you could just trace that String. By changing your TestOne class into the following you effectively get rid of 4 other classes. You also encapsulate a String inside of the class TestOne.
package classes {
import flash.display.MovieClip;
public class TestOne extends MovieClip {
private var message : String;
public function TestOne(myMessage:String) {
message = myMessage;
trace("This is TestOne");
}
public function testing() : String {
return message;
}
}
}
First off I don't understand classes, how to "call" or "initiate" them. I'm class ignorant.
I have two .fla files. One of my .fla files consist of 15+ .as files; we'll call this one XML editor. The other .fla file consists of 10+ .as files; we'll call it the interface.
The xmleditor.swf loads the interface.swf.
Within the xmleditor.swf, a login screen appears and the enduser logs in as either a "user" or an "admin". The "user" or "admin" is stored in a public variable called "userType". The userType variable is created in one of the many xmleditor.fla .as files called Login.as.
Once logged in, xmleditor loads the interface.swf. interface.fla uses 10+ .as files. one is called nodeNames.as I need an if statement in nodeNames.as that is something like this:
if (Login.userType == "user"){
trace("do something");
}
I have the following FlashVars.as file but I have no idea what the steps are to make it work.
package extras.utils {
import flash.display.Sprite;
import flash.display.LoaderInfo;
/* In AS3, we need to have a display object on the stage to access FlashVars
* this class can be used once, and then referenced from anywhere as
* FlashVars.data.variableName
*/
public class FlashVars extends Sprite {
public static var data:Object;
public function FlashVars() { }
public function load():void { //Only needs to be called once
data = this.root.loaderInfo.parameters;
}
}
}
Should I use this FlashVars? and if so, how?
Or is there an easier way to access the variable?
well, from what i understand, you Login.as is a class. Then you have two ways of accessing the Login.userType variable : if you want to be able to call it with
Login.userType, you'll need it to be static in your class
public static var userType:String
it is then accessible using Login.userType from anywhere in your application, as long as you import Login.
But it is often considered bad practice to have too many static vars in your app, especially from different classes. so you may want to have an instance of your login class stored in a variable somewhere in your app, along with anything you need
var myLogin = new Login();
myLogin.userType = 'value';
But be aware that this way, every new Login() will carry it's own different userType, so you will want to pass along myLogin to any object needing it.
Object programming can be confusing, but is very powerful, i suggest you read about it (in books or on the web) since the whole thing can't be explained here.
Have fun!
I have an actionscript class with a static member variable defined.
public class A
{
public static var x:int;
}
When I try to access it from different parts in my code I don't get the same value in each spot.
A.x
I am accessing the variable in different modules that are loaded, so they are all in their own separate .swf file. Could this by why?
Seems like an application domain problem. The main swf and the modules seem to be accessing their own copies of the A class. You should probably change the way you load your modules.
Check this out:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/system/LoaderContext.html#applicationDomain
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/system/ApplicationDomain.html