actionscript-3airstarling-frameworkcitrus-engine

Using Starling and CitrusEngine classes with AIR


I'm creating AIR application with AS3, Citrus Engine and Starling. Here are two different concepts of creating TextField:

/* 
Using native AS3 component.
import flash.text.TextField; 
*/
var meaningTitle = new TextField();
meaningTitle.text = 'Meaning';
addChild(meaningTitle);

/* 
Using Starling framework.
import starling.text.TextField; 
*/
var meaningTitle = new TextField(0, 0, 'Meaning');
addChild(meaningTitle);

By using Starling framework I can't achieve text formatting and other cool features of AS3 TextField. If I creating TextField with native AS3 component and then adding it to the scene I getting this error:

Type Coercion failed: cannot convert flash.text::TextField@37aada9 to starling.display.DisplayObject.

I found one solution, it says I have to use this:

... as DisplayObject

But this solution also doesn't work. Please, help me. All I want is TextField formatting.


Solution

  • Starling or any Stage3D based framework are incompatible with classic Flash DisplayObject. That is why addChild() fails and as DisplayObject fails too since they are not the same type of Object. Starling makes it worse for beginner by giving the exact same class names to corresponding object. So first, don't feel bad, dozens of beginners have fallen in the same name trap before when using Starling.

    Now to your problem, use the classic TextField, format it the way you want then draw it to a BitmapData and pass that to Starling in a Starling Image (using Texture.fromBitmapData()). The Starling TextField is there for convenience only as it is only for very basic uses.

    More advanced TextField can be found with the Feather framework (sort of Starling based component framework).

    Finally a little advice since you are a beginner, don't use Citrus, you probably do because you think that's what everybody is doing but that would be a mistake. Citrus is a very weak typed framework that often makes it harder for beginner to get anything done. Many pro also can't work with it since it's mostly weak typed everywhere. It is imo a very overrated framework.