How could I compile the code for this Flash tutorial? - actionscript-3

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
}

Related

Edit complex component in Flash IDE in one ActionScript class

I'm new to Adobe Flash. I'd like to create a button or component in Flash Professional using MovieClip and add label to it, and build my own button with special functions.
And i'd like use ActionScript to edit code. I faced a problem: i can not edit all code of my component (that cotains several ui-elements) in one AS3 class. I create empty document, add rectangle to it, then convert it to symbol. Next, i go to Advanced ActionScript 3.0 Settings of IDE, add path to folder where i put my Class.
Then add MyClass to Document class in that window.
But when i add new elements to the document, it doesn't appear in MyClass.as
Is it possible to edit complex component in Flash IDE in one ActionScript class ?
Sorry for a long explanation and for my poor english.
I create empty document, add rectangle to it, then convert it to
symbol.
Make sure symbol is type: MovieClip. Give it a name you prefer (no space is good idea). Further down (see Advanced section?) there is section called Linkage click "Export for ActionScript" and remember the name shown in Class box (you can edit it) and just click OK. No need for folder paths etc.
Lets assume the name used for Class is : myComponent
Now you can use it in your code. To make a document class open the Properties panel (ctrl+F3) and just type a name example Main (no extensions) and press enter and then save FLA and secondly also save the class code (it will ask for a file name so use as Main.as).
Now If you click that pencil icon in properties you will be editing class code (Main.as) for the FLA document and it knows your component if you reference it by code:
public var test_comp : myComponent = new myComponent (); here test_comp is just a quick reference name. Use your own preferred reference name.
You can edit the MovieClip of myComponent by right-clicking and choosing "edit" (not "edit Class"). Now you can add/draw different things and even convert them to Graphic or MovieClip and give them instance names. A MovieClip object is like a mini-stage so it has its own unique timeline and you can work exactly like if this was the main stage (but without actually affecting main stage).
If you add something to Component MC such as drawing a circle (as some button?) and convert that circle to MC or Graphic type with instance name circle1, in the code you access that as:
test_comp.circle1.alpha = 0.5; since test_comp is your code's reference name for that myComponent MC in the Library.
After testing the above example... now begin your own component.
Remember... If an object is inside the MC access it via it's instance name and if you need to addChild it from the Library instead then use its AS3 linkage name.

How do I add a preloader to my game using flashDevelop?

Hi I have made a tile based game using a tutorial from this site http://www.wildbunny.co.uk/blog/2011...m-game-part-1/ My game is complete but I want to add a preloader to it.
A little background for a hopefully better understanding.
FlashDevelop is where all of my as3 files and classes are and I can also test my game here and I works fine.
Flash CS6 is where I have only one scene "scene 1" with one frame on it, this frame has all of my tile based images ( movieClips ) and animations in squares, if I double click a movieClip ( for example "player" ) it will go inside and I have got lots of frames ( jumping, running etc ).
I can't test my game from here, It shows errors (scenes are NAN). I think that's normal not sure - maybe off topic anyway.
I have looked on the internet for a tutorial on preloaders suiting my situation but can't find any using flash with flashDevelop and AS3.
You create a brand new project "Actionscript 3 with preloader", FD will generate you a preloader class *.as file, and a main class file with a [frame] directive in it. Copy the preloader *.as file into your current project, copy the directive from new project's Main.as into your project's Main.as, change the package name if needed in both places (in preloader's getDefinitionByName() call and in the directive - fully qualified class names required), add whatever visuals you want to your preloader class, and you're set.
The directive in my case looked like this:
[Frame(factoryClass="Geologist.Preloader")]
The package is Geologist, and the startup() function of the preloader was like this:
private function startup():void
{
var mainClass:Class = getDefinitionByName("Geologist.Main") as Class;
var theStage:Stage = this.stage;
theStage.addChild(new mainClass() as DisplayObject);
theStage.removeChild(this);
}

How can i associate a library symbol with an existing class

I am using Flash Develop for compilation ( Not Flash IDE )
Here is a sample code :
[Embed(source = 'assets.swf', symbol = 'app.view.CustomButton')
var customButton_Class:Class ;
var customButton_Instance ;
customButton_Instance = new customButton_Class();
The problem is that this "customButton_Instance" doesnot know anything about app.view.CustomButton ?? ( Actually this means there is no sense in setting the class as app.view.CustomButton in the assets.fla library )
The workaround i am following is :
var customButton:CustomButton = new CustomButton();
customButton.setView( customButton_Instance )
But i wanted somehow, the customButton_Instance should automatically associate itself with the customButton class. Any ideas pls ?
When using Embed you lose the symbol's associated class. That's how it works.
Instead, if you're using Flash Pro, choose to Publish a SWC that you can then add to your FlashDevelop project (in FlashDevelop: right-click > Add to Library), and then all the symbols will be visible in code completion like any class and you can just write new app.view.CustomButton() or create a custom class extending it.

how to access class from embedded swf

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.

What is the correct way to add components to the library of a Swf?

I've been having a problem that's plagued me many times in the course of developing a Flash project. It looks something like this:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.containers::BaseScrollPane/fl.containers:BaseScrollPane::drawBackground()
at fl.controls::TileList/fl.controls:TileList::draw()
at fl.core::UIComponent/::callLaterDispatcher()
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChildAt()
at fl.controls::BaseButton/fl.controls:BaseButton::drawBackground()
at fl.controls::BaseButton/fl.controls:BaseButton::draw()
at fl.core::UIComponent/drawNow()
at fl.controls::ScrollBar/fl.controls:ScrollBar::draw()
at fl.core::UIComponent/::callLaterDispatcher()
Now, in my case, this error stems from initializing components in code when they have not been explicitly added to the fla's component library in CS4. In the past, I have run into this issue when trying to dynamically create ScrollPanes in code. I have solved it by adding ScrollPane components to my Main.fla's library. This seemed to work for a while.
Now, I am trying to use an AstraFlash AutoComplete box. I have imported the proper fla files into CS4, and placed an AutoComplete box into my Swf. Everything builds fine, but the above error occurs when the Swf is loaded. My thought is that the AutoComplete box is trying to create a ScrollPane as part of its functionality. Ok, I understand this, so I add the ScrollPane component to the library as well with the same results.
Usually I would just mess with the library components/settings until I get rid of the error, but I'm sick of running into this, and I want to know the correct way to solve the problem. So, here are a few questions I have:
When are you required to add a component to a Fla's library rather than just creating the component in code?
Which Flas do you need to add the component to? Just the one using it? Or all of parents of that Fla as well?
Let's say the Autocomplete component requires a ScrollPane component. Why isn't this dependency recognized when I add the one component? Why must I explicitly add it?
What is the difference between adding a component to the library, and adding it to the library's 'Component Assets' folder? What is this folder's purpose?
I really need this AutoComplete component to work. Assuming the AS3 code is correct, and I am still getting the above error, what settings do you think are probably incorrect? Out of frustration, I have tried adding every possible component to the library, as well as to the library's component assets folder just to have a starting point, but I still get the error.
Any help is appreciated.
I'm not sure I understand you're setup. What I'm assuming is you have a child fla that contains some components and you need to create instances of those components in a parent(loader) fla.
It should work if you compile the classes from the child fla into the parent fla, but that would duplicate things.
The issue is when you load a swf, the classes sit in a different ApplicationDomain.
Simply put, that's the thing that manages classes in a swf so you don't have class collisions between a loader and loaded swf and some other security stuff.
I made a simple fla that holds a Button component(fl.controls.Button) and loaded that from a loader fla. I didn't add the Button component to the loader fla, but did create a button instance, using the loaded swf's application domain. Here's how:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
loader.load(new URLRequest('componentContainer.swf'));
function loaded(event:Event):void {
addChild(loader.content);
var SWFButton:Class = loader.contentLoaderInfo.applicationDomain.getDefinition('fl.controls.Button') as Class;
var button = new SWFButton();
button.label = 'Test';
addChild(button);
}
There is a page in Programming Actionscript 3.0 about thise kind of issues and how to use ApplicationDomain, but I didn't fully understand it to be honest.
HTH,
George