How to start Application from a simple AS file? - apache-flex

I want to use vkontakte's new wrapper feature, that enhances your application abilities by running under SWF wrapper.
This is a sample application that uses this mechanism. It uses pure action script to display it's contents rather than an mx:Application.
Using the wrapper on my mx:Application failed due to the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.managers::FocusManager/activate()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\FocusManager.as:702]
at mx.managers::SystemManager/activateForm()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:2493]
at mx.managers::SystemManager/activate()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:2451]
at mx.core::Application/initManagers()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Application.as:1152]
at mx.core::Application/initialize()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\core\Application.as:834]
at DummyApp/initialize()[C:\Users\Eran.HOME\Documents\Web Projects\MaxiMarketing\TestMarketing\src\DummyApp.mxml:0]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:2127]
at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:3396]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:3219]
at mx.managers::SystemManager/docFrameListener()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:3065]
So I figure I could create a wrapper to the wrapper that can launch my application and came up with this (DummyApp is the application I want to lunch):
package
{
import Components.SidePanel;
import flash.display.Sprite;
import flash.events.Event;
public class AppWrapper extends Sprite
{
public function AppWrapper() {
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
public function onAddedToStage(e: Event): void {
var mainApp:DummyApp = new DummyApp();
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
}
Unfortunately - it also failed, and the question remains, how to start Application from a simple AS file?

Are you trying to make a pure actionscript project or a flex application project (former does not use the Flex Framework, latter does)? You'd need at least an application mxml file to use the Flex framework. If you create an "Actionscript project" in Flex, the main application file (.as) will be your "document class" or wrapper. Here's a related post on using an Actionscript Application wrapper:
Possible to use Flex Framework/Components without using MXML?
You'll see here though that you still need to use a bit of mxml to "init" the actionscript class.

Vkontakte's wrapper is now supporting Flex, which makes this question obsolete.

Related

Adding an MXML component as a child of the main application using ActionScript

How can I add an MXML component as a child of the main application using ActionScript. It's not possible to instatiate it, is it? Assuming that behind every mxml file stands an actionscrpt3 class, I tried to import it but id didn't show up.
You'll want to familiarize yourself with the flex component lifecycle: http://msimtiyaz.wordpress.com/flex/adobe-flex-component-instantiation-life-cycle/
It explains the actionscript code behind the mxml components, and it's important to be familiar with, because if you implement your components incorrectly, it can really slow down your application.
Anyway, I think you may be confused about what imports do. Import statements make the code available to use in your code, but it wouldn't create a component. You'd need to create a component the same way you create any object in actionscript, and then you'll need to add that component to the display list to make it show up.
The appropriate place to do this is in the createChildren() function:
override protected function createChildren():void {
super.createChildren();
var myText:Text = new Text();//create a new object
this.addChild(myText);//add it to the display list
}

how to embed pure as3 bitmap assets with flex4 (worked with flex3)

In Flex3, I could compile pure as3 code and use "embed" tags to load in images. This was done by Flex making a BitmapAsset class.
I can still do this in Flex4.
However, there was a trick to fakeout flex3 and use my own mx.core.BitmapAsset class to remove some of the extraneous stuff Flex's BitmapAsset brings in with it. This is described here: http://www.ultrashock.com/forums/flex/embed-flex-assets-without-using-flex-123405.html
Unfortunately, I cannot get these tricks to work with Flex4 and get smaller file sizes. I end up with the error "VerifyError: Error #1014: Class mx.core::BitmapAsset could not be found."
This error leads me to this forum, and a solution as described there: http://tech.groups.yahoo.com/group/flexcoders/message/148762
Following this advice, I add -static-link-runtime-shared-libraries=true, and my swf loads without an error... but this means I am loading in the pieces of the flex framework I wanted to omit (and the file size says so too).
Is there a better way to fake out flex4 when it comes to using Embed?
Will something like work ?
[Embed(source="yourImage.jpg")]
private var ImageC:Class;
private var image = new ImageC();
Keith Peters has a nice article on the subject.
I've created a test project in Flex 4 with [Embed] and fake BitmapAsset.as and can't see the exception:
package
{
import flash.display.Sprite;
public class EmbedTest extends Sprite
{
public function EmbedTest()
{
addChild(new smile());
}
[Embed("smile.png")]
private var smile:Class;
}
}
Try to add -link-report link-report.xml compiler option and check link-report.xml file in bin-debug.
Do you have BitmapAsset.as there? If no, you may have excluded it by externs, external-library-path or load-externs.

How to hack private static field in Flex?

Is there a way to change private static field of an alien class?
For example:
package mx.managers {
public class TooltipManager ... {
private static var _impl:IToolTipManager2; // <- assign my own value here
...
}
}
In Java it is possible to do it using Reflection API. What about Flex?
No, that is not possible.
If you are looking into changing the implementation of the TooltipManager, have a look at the Singleton class in the Flex SDK. You'll need to create a custom implementation and register it via the Singleton class before the application initializes. The best is to override the application preloader and do the registration there.
Well, if you feel like you can handle the extra responsibility, you can monkey patch the class by copying the source into your own source tree with the same package and apply the necessary modifications. That way the flex compiler will use your implementation rather than the SDK implementation.
This technique is sometimes used as a last resort to fix issues which cannot be fixed otherwise. Drawbacks include issues such as forwards compatibility and unintended side effects in the same or other classes dependant on the class your editing.

Adding button in Actionscript code : Using Flex Builder

I created a new actionscript project using Flex Builder 3 and tried
to run the following file. I get this error :
Definitions: fl.controls:Button could not be found.
All I want to do is, to add a button to the application.
How could I do it?
package {
import PaperBase;
import org.papervision3d.objects.primitives.Cone;
import fl.controls.Button;
import fl.controls.Label;
import fl.events.ComponentEvent;
public class cone1 extends PaperBase {
public var cone:Cone = new Cone();
protected var sceneWidth:Number;
protected var sceneHeight:Number;
public function cone1() {
sceneWidth = stage.stageWidth
sceneHeight = stage.stageHeight;
init(sceneWidth*0.5,sceneHeight*0.5);//position of the cone
}
override protected function init3d():void {
cone.scale = 5;
cone.pitch(-40)
default_scene.addChild(cone);
}
override protected function processFrame():void {
cone.yaw(1);//rotation speed
}
}
}
The fl.* package is part of Flash Professional, not Flex. For Flex you should be using the components that are part of the mx.* package.
Now, that being said, I'm fairly sure it is possible to use Flash components in Flex. I'm just not sure how it's done off the top of my head.
Also, you don't need an actual button component to get a "button like" ui element - any class that extends InteractiveObject will do. This incldes Sprite and MovieClip.
Branden is correct the fl package is a part of the Flash IDE..I am not sure either but you may be able to add the package to your class path if you know where the package resides on your file system..i am guessing somewhere in C:/program files/adobe/flash
if you want to use components in flex builder I think you need make a flex project not an actionscript project
and change your imports to:
import mx.controls.Button;
import mx.controls.Label;
import mx.events.FlexEvent;
Also if you do not need to use components either you can use a Sprite for a button like branden said and you could just use a TextField for a label.
another option if you have the flash IDE is to make a SimpleButton, press F8 select button, click enter. then give it a linkage name by right clickin it in the library panel and selecting linkage name. then export the .swf and put the swf in the src folder for your project and embed it like this:
[Embed(source="flashfile.swf")]
public var myButton:Class;
You may even be able to export the Flash IDE components this way but not sure...actually I am not 100% positive if the [Embed] meta data works in an actionscript project or just flex projects so you will have to check and see.
It depends what version of the IDE you have, for CS4 and Mac the location would be /Applications/Adobe Flash CS4/Common/First Run/Classes
Add that folder or the relevant folder for your installation/OS to your classpath in flashbuilder/eclipse and it will interpret the class calls fine.
It makes sense if you're coding pure actionscript and dont want to use flex components, or employing a mixed coding and designing in IDE approach.
#philip the embed tag cannot be used in pure actionscript
Not sure why you would want to, but if you need to import the flash libs into flex,try dragging what you want to the stage in flash and exporting as a .swc file to import into your flex project.

MXML without Flex Framework/Components

The Flex compiler can compile "pure AS3" SWF files that don't contain any Flex Component bytecode. So,
Would it be possible to create a custom component framework (used in place of the Flex Framework), that can still be visually laid out using MXML (read: markup), and compiled down to a SWF without any dependencies on the Flex Framework itself?
Yes, it's possible. Your MXML files are essentially just a different way to specify classes. You can see what mxml files boil down to by compiling your project and providing -compiler.keep-generated-actionscript=true to mxmlc.
bar.mxml:
<?xml version="1.0" encoding="utf-8"?>
<flash:Sprite xmlns:flash="flash.display.*">
</flash:Sprite>
After compiling with mxmlc -compiler.keep-generated-actionscript=true bar.mxml, it turns into the following.
generated/bar-generated.as:
package {
import flash.display.Sprite;
// bunch of imports
public class bar extends Sprite {
public function bar() { super(); }
}
}
There are two different compilers: one that is used for compiling ActionScript code to AVM bytecode and another (mxmlc) that compiles MXML files into ActionScript code which is then in turn compiled by the first compiler. If you want to see what AS3 code is generated, pass the "-keep" parameter to the MXML compiler.
In theory it's possible to do what you suggest. My guess is that mxmlc keys heavily into features from the UIComponent class, so you'd probably have to hack on mxmlc a bit so that it didn't puke on non-UIComponent classes. Even still, since things like [Bindable] / data binding make use of Flex framework features (not plain Flash Player / AVM features) you would be rewriting an awful lot of code.

Resources