how to access class from embedded swf - actionscript-3

I am trying to get an instance of a specific class from an embedded swf, it so happens the class I need is also the Default Application extending Sprite for the swf. I am able to do this successfully if I load the swf however I want to embed it instead.
The class I want to load is also extending a custom interface. Here is what I have tried but is not working:
[Embed(source="resources/MySwf.swf")]
private var MySwf:Class;
private function someFunction() : void
{
var inst:ISomeInterface = new MySwf() as ISomeInterface;
}
I appreciate any pointers.
Thanks.

Docs for embedding are here:
http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html
You should be able to do something like:
[Embed(source='resources/MySwf.swf', symbol='TheExportNameInMyFlaLibrary')]
public var MySwf:Class;
Personally, I prefer to use the Flash IDE publish settings to be have Export as SWC checked. That way, you can just drop the SWC into your FlashBuilder projects' lib folder and be done. Don't have to worry about manually setting each class up like this.

Related

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().

Starling scaffold project showing blank texture

I am building an app using Starling and have imported the scaffold project however whenever I call getTextureAtlas() it doesn't show the image
this.mLogo = new Image(Assets.getAtlasTexture("powered_by_starling"));
this.mLogo.x = 0;
this.mLogo.y = 0;
this.addChild(this.mLogo);
There are no errors so I am guessing it can find the texture. If I change the name to something that doesn't exist it throws an error 'texture cannot be null'. I am also using
Assets.contentScaleFactor = Starling.current.contentScaleFactor;
and everything is pretty much a standard import of the scaffold as is however I am using Feathers UI screen navigator but I haven't had a blank image issue on other projects.
Edit: I can't seem to get the sprite to work within a class that extends feathers.controls.Screen
Is getAtlasTexture a function that you wrote? Are you using starlings asset manager? If that's the case, you will need a instance of that class, not static method call.
Anyway, try using the Texture class and the static method it offers. You don't need atlas(sprite sheet) for a Image - you will need it however for MovieClip.
Texture.fromBitmap
Texture.fromBitmapData
.... and others, check documentation
If your graphics are embedded, then use this:
var _img:Image = new Image(Texture.fromBitmap(new Assets.EmbeddedGraphic()));
Are you using atf? because the same happened to me until I used png and then it worked

How could I compile the code for this Flash tutorial?

I started reading this tutorial: http://active.tutsplus.com/tutorials/actionscript/creating-a-reusable-flash-uploader-with-actionscript-3-0-and-php/
I'm using FlashDevelop, and I pasted the full code into an ActionScript file. The errors I'm receiving are like this:
C:\Users\tempus\Documents\uploaderas\Uploader.as(30): col: 4 Error: Access of undefined property select_btn.
select_btn.addEventListener( MouseEvent.CLICK, browse );
^
C:\Users\tempus\Documents\uploaderas\Uploader.as(31): col: 4 Error: Access of undefined property progress_mc.
progress_mc.bar.scaleX = 0;
...
I understand that the errors appear because the objects have not been declared ( and they appear to be instantiated from somewhere ), but I don't understand how/what should I include to declare them. Could you point me towards a solution?
It's because the buttons are created in the Flash IDE (as the tutorial was meant to be compiled using the Flash IDE). Since the buttons don't exist in the code aspect you get that error.
You can either create the elements yourself via code, or use the Flash IDE and export a swc/swf of the neccessary UI elements and include that in your flashDevelop project. I'm assuming you'll want to do the latter -
in the Flash IDE, open the .fla, open the library panel, find the progress asset, right click it and bring up the properties. Check the "Export For ActionScript" option, then in the 'Class' field give it a unique name like "SelectBtn". Do the same for the 'progress' asset (only a different class name like 'ProgressBar'). Go to the flash publish settings, and on the flash tab select 'export swc'. publish the file and place the published swc in your flash Develop project folder (traditionally the lib folder of your project).
In Flash Develop, right click your swc and choose 'Add To Library'. (You may need to right-click again and go to options and choose the include completely option). Now you can access those classes you setup in Flash. Then in your code, declare and initialize the display assets:
public var select_btn:SelectBtn = new SelectBtn();
public var progress_mc:ProgressBar = new ProgressBar();
You'll also need to do that textField too. It would be easiest just to do it in your code though.
public var label_txt:TextField = new TextField();
Keep in mind you'll need to manually position and use addChild on all three elements this way. If you want to keep the positioning that's in flash, just select all the elements on the stage and press F8 to convert them to a MovieClip. Then in the library setup linkage the same as the others and give it a class name of something like "DisplayAssets" and export a new swc. Then your code would look like this:
public var select_btn:Sprite;
public var progress_mc:Sprite;
public function Uploader(){
var displayAssets:DisplayAssets = new DisplayAssets();
addChild(displayAssets);
select_btn = displayAssets.select_btn;
progress_mc = displayAssets.progress_mc;
//the rest of the code
}

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.

How can I access a function in one class from another AS3?

I have a AS3 MP3 player. The player class is called Mp3PlayerFrontEnd and controls the play and pause buttons. The playlist class is called PlaylistBoxItem and loads the track names into a playlist from XML. On each line of the playlist is a download button. When the download button is pressed, I want the player to pause. I thought I could just _player.pause(); from the playlist class but it doesnt work.
So my question is... How do I access the function in the MP3 player class from the playlist class?
I did not make this, I purchased it. I asked the guy who made it for help and he responded back...
"Normally you will just have to get access to the Mp3Player instance in PlaylistListboxItem class and call pause() on it in download function. I guess the easiest way to achieve this is to create a public static instance of a holder class that could be used access the player from wherever in code."
I do not know how to make a public static instance or where to put it.
Would I need to make a new class?
Where would I import it from?
How would I write the function?
Thanks.
Rich
whatever class is in control of everything needs a reference to your player.
So if your play list class is where you want to use play/pause/etc in the constructor add a place for a reference to the mp3player.
public class PlayListBoxItem
{
private var myMp3Player:Mp3Player;
// the class constructor
public function PlayListboxItem(myMp3Player:Mp3Player)
{
this.myMp3Player = myMp3Player;
}
}
now anywhere in the play list class you can access your mp3 player with myMp3Player.start() .stop(), .whatever public functions are there.
Make sure start and stop are public functions.