How to import FXG to a Shape? - actionscript-3

I can import FXG files as Sprites as follows:
import graphics.mypic; // graphics/mypic.fxg
var mysprite:Sprite = new mypic();
I don't always need the fancy things that come with Sprites. How can I import them to Shapes?

No, you can't cast them as Shape(s) - the internal compile-time FGX is built on top of Sprite. You can find that out by running
var tig:* = new tiger();
if (tig instanceof Sprite)
...
What George Profenza is referring to is runtime loading of FXG's

I don't know if there's a better way to do this know, but last year I've played with a nice FXGParser library that simplified things a lot.
Get the library:
svn export http://www.libspark.org/svn/as3/FxgParser/
Use it:
import fxgparser.FxgDisplay;
import graphics.mypic;
var fxg:XML = new XML(mypic);//this bit depends on how you load/embed the fxg xml
var mysprite: FxgDisplay= new FxgDisplay( fxg );
addChild( mysprite );
Goodluck!

Related

Scroll Container can't be added

I'm trying feathers for the first time and keep getting this error when I try to add a ScrollContainer to the stage
Scene 1, Layer 'Layer 1', Frame 1, Line 13, Column 21 1067: Implicit coercion of a value of type feathers.controls:ScrollContainer to an unrelated type flash.display:DisplayObject.
this is the code I'm using
import flash.display.MovieClip;
import feathers.controls.ScrollContainer;
import flash.geom.Rectangle;
import feathers.layout.HorizontalLayout;
import flash.display.SimpleButton;
import feathers.controls.Button;
var container:ScrollContainer = new ScrollContainer();
var layout :HorizontalLayout = new HorizontalLayout();
layout.gap = 20;
layout.padding = 20;
container.layout = layout;
this.addChild( container );
var yesButton :Button = new Button();
yesButton.label = "Yes";
container.addChild( yesButton );
var noButton:Button = new Button();
noButton.label = "No";
container.addChild( noButton );
is there any thing that I'm doing wrong?
thanks in advance!!!
Feathers runs on Starling only, so you can add Feathers controls only to Starling display objects, like starling.display.Sprite. You're trying to add a Feathers control to a Flash display object and those two can't be mixed.
You should configure Starling in your project. You can start from here http://wiki.starling-framework.org/feathers/flash-pro , http://gamua.com/starling/first-steps/, http://blogs.adobe.com/rgalvan/2011/11/adding-fp11-support-to-flash-pro-cs5-and-cs5-5.html
If you're keen on using Starling and Feathers, then you would be better off switching to a different IDE like Flash Develop. It's quite painful working with Starling in Flash Pro CS.

Saving customized movieclip as an image to local disk (diy generator)

dropbox.com/s/77euop1luqjreos/FINAL.fla
Ok i just gave up with this already. I cant think of any way to save the image created by a user. Its hard to explain but please check out the fla file of my work. Basically its a diy generator. The only thing missing is a save function. Ive read filereference but its always in a document class. My code is in the timeline. Please help I'm really stuck.
edit: I got a download button that is working now!! But it only saves a small part of the movieclip: Imgur
In order to create images, you need to access the pixel data of the MovieClip object. The data can be obtained by rendering a MovieClip into a BitmapData object and using this data, you can write your own encoder to convert it to any image format you’d like. Writing such encoder is not a trivial task and requires understanding of the image format algorithm, or you can use pre-written libraries. You can download the PNGEncoder and JPGEncoder , which is part of as3corelib, an open source project library.
Using the JPGEncoder, we can convert DisplayObject into ByteArray, suitable for saving into file. If using the JPGEncoder, it’ll look like this:
import com.adobe.images.JPGEncoder;
var jpgEncoder:JPGEncoder = new JPGEncoder(quality);
//remember bitmapData here is just an example (do not try to compile this code without declare this particular variable)
var byteArray:ByteArray = jpgEncoder.encode(bitmapData);
With the PNGEncoder, it’ll look like this:
import com.adobe.images.PNGEncoder;
//remember bitmapData here is just an example (do not try to compile this code without declare this particular variable)
var byteArray:ByteArray = PNGEncoder.encode(bitmapData);
Saving Into User’s Hard Drive
Using the FileReference.save() function, we can prompt the user to save the file with the following call.
var fileReference:FileReference=new FileReference();
//in case of JPGEncoder
fileReference.save(byteArray, ".jpg");
With the two combined, here’s an example how to use:
//remember to import
import flash.net.FileReference;
import com.adobe.images.JPGEncoder; //or import com.adobe.images.PNGEncoder;
import flash.utils.ByteArray;
import flash.display.BitmapData;
//where mc_canvas will be your MovieClip instance name
var bitmapData:BitmapData = new BitmapData(mc_canvas.width, mc_canvas.height);
bitmapData.draw(mc_canvas);
var jpgEncoder:JPGEncoder = new JPGEncoder(quality_slider.value);
var byteArray:ByteArray = jpgEncoder.encode(bitmapData);
//if you want to use PNGEncoder
//var byteArray:ByteArray = PNGEncoder.encode(bitmapData);
var fileReference:FileReference = new FileReference();
fileReference.save(byteArray, ".jpg");

Layering objects in Flash

I was wondering what was the best way to change the layering of objects in Flash? My project is made up of mostly small individual movie clips. I want to better organize my objects into layers so that one set of movieclips can be shown above another at a given time without any conflicts. Any advice on the best way to do this would be great.
EDIT: Getting errors with the following:
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
var backgroundLayer:Sprite = new Sprite;
backgroundLayer.name = "backgroundLayer";
var gameLayer:Sprite = new Sprite;
gameLayer.name = "gameLayer";
var menuLayer:Sprite = new Sprite;
menuLayer.name = "menuLayer";
public function Main()
{
addChild(backgroundLayer);
addChild(gameLayer);
addChild(menuLayer);
Errors are:
1120: Access of undefined property backgroundLayer.
1120: Access of undefined property gameLayer.
1120: Access of undefined property menuLayer.
Thanks.
Probably the easiest way to do this is to make a hierarchy of DisplayObjectContainers or its subclasses. (Sprite, MovieClip, etc.) Once these are added to the stage in the proper order, any children of these containers will be "within" the layer of the container.
To move an object between these layers in code, you just have to oldLayer.removeChild(thing); newLayer.addChild(thing);.
Whether or not this is the "best" way to do this depends on what you are doing, but this is a way that I've done game object layering in the past.

Convert drawn lines into bitmapdata - Bitmap.draw() + Sprite.graphics - wont work

EDIT: The convertation / copy process it self works, I just cant figure out how to tell the bitmapdata, which part of the stage to copy - I tried to solve that problem by movie the canvas to x=0 y=0 didnt show anychanges.
The only thing that showed a change was that I did move the canvas BEFORE drawing to zero, but this is totally buggy because the part of the drawing which has negativ coordinates wont be copied since the coordinate change only affect the bitmap if you do it before you start to paint
OLDER ENTRY:
I want to convert the Sprite.graphics into bitmapData, because I have a drawTool which allowes the user to paint lines, which are located inside the Sprite.grahpics I think.
I need to convert these lines to bitmapdata, because this allows me to deform them later on, but I cant use this
Bitmapdata.draw(Sprite.graphics);
And using only the Sprite instead of Sprite.graphics doesnt show any result =\
help is needed!
Use a matrix if you want to draw only a certain portion and from an origin other than (0,0). There's plenty in the Adobe docs on this, or a good example here:
http://www.quasimondo.com/archives/000670.php
Only use graphics when drawing. The actual Sprite object contains what you want, so following your convention, simply do:
BitmapData.draw(Sprite);
Although for a literal example:
var mySprite:Sprite = new Sprite();
// add lines etc using mySprite.graphics
var myBitmapData:BitmapData = new BitmapData(mySprite.width, mySprite.height);
myBitmapData.draw(mySprite);
I think, you've not completely understand usage BitmapData.draw().
BitmapData.draw() is a All DisplayObject(Sprite, MovieClip, Shape, Text, Video...) drawing possible. because they are have a IBitmapDrawable.(more information refer a adobe document Is the best teacher.)
If you want implement Paint Board. refer a below code. very simple Paint Board. but some help you.
try copy & paste.
import flash.display.Sprite;
import flash.events.Event;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
var isDraw:Boolean = false;
var brush:Sprite =new Sprite();
brush.graphics.beginFill(0x0000ff);
brush.graphics.drawCircle(0,0,5);
brush.graphics.endFill();
var canvasData:BitmapData = new BitmapData(600,400, false);
var canvas:Bitmap = new Bitmap(canvasData);
addChild(canvas);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDrawStart);
stage.addEventListener(MouseEvent.MOUSE_UP, onDrawStop);
stage.addEventListener(Event.ENTER_FRAME, render);
function onDrawStart(e:MouseEvent):void
{
isDraw = true;
}
function onDrawStop(e:MouseEvent):void
{
isDraw = false;
}
function render(e:Event):void
{
if(!isDraw) return;
brush.x = mouseX;
brush.y = mouseY;
canvasData.draw(brush,brush.transform.matrix);
}

Instantiate an Fxg on runtime using getDefinitionByName in Flex

i have a little problem with getDefinitionByName.
My purpose is to instantiate an FXG object(Number10.fxg) in a document mxml on runtime.
The name of the Class is in a string variable that is used by getDefinitionByName
to return the name of the class to insantiate. The code doesn't work even if doesn't send an error message. The code is as follows:
import assets.Number10;
import flash.utils.getDefinitionByName;
import mx.core.IVisualElement;
private function onClick(event:MouseEvent):void
{
var value:String = "Number10";
var ClassDefinition:Class = getDefinitionByName(value) as Class;
var ten:IVisualElement = new ClassDefinition() as IVisualElement;
this.contentGroup.addElement(ten);
}
I tried also with... var ten:IVisualElement = new ClassDefinition();
but nothing. It Doesn't work!
Please, Help me!
First of all, i refer to the adobe documentation pages that covering the topic so telegraphic. Here it is:
Option includes class [...]
Description Links one or more classes to the resulting application SWF file, whether or not those classes are required at compile time.
To link an entire SWC file rather than individual classes, use the include-libraries option.
Ok.In Flash Builder i go to the Additional compiler arguments where there is just this option
-locale en_US
So i add my option under this
-includes class = assets.Number10
or
-includes class assets.Number10
or
-includes class Number10
When the application runs i get the Error #2032.
I think that the option declaretion is wrong. I do not have a good reference for using option.
So...Help me!
How can i declare the Number10 class or the assets package with the other fxg object using the includes class option?
Ok! I find the solution...
Is to put a reference to Number10 class somewhere in the code, for instance:
import assets.Number10;
import flash.utils.getDefinitionByName;
import spark.core.SpriteVisualElement;
//case1
var myNumber:Number10;
//or
//case2
Number10;
private function onClick(event:MouseEvent):void
{
var value:String = "assets.Number10";
var ClassDefinition:Class = getDefinitionByName(value) as Class;
var ten:SpriteVisualElement = new ClassDefinition() as SpriteVisualElement;
this.contentGroup.addElement(ten);
}
and the code works :-)
This is a problem that comes from the way that Flex compiles its code. Flex compiles its code so that if a class is not used, it will keep this class off the final compiled program.
But the problems are not over yet! If i have hundreds of Fxg objects that could be instantiate, declaring all classes is little difficult and tedious.
So, how i can delclare in one time all classes of a package?
You can add classes to SWCs and SWFs using the include and includeClasses compiler options. Using these, you don't have to reference the classes in the code. Consult the documentation for proper usage.
Be sure to use the fully qualifed class name.
Also, the approach of casting your FXG class as an IVisualElement is new to me. I thought you had to use real classes in casting and the sort. Try using a SpriteVisualElement.
private function onClick(event:MouseEvent):void
{
var value:String = "assets.Number10";
var ClassDefinition:Class = getDefinitionByName(value) as Class;
var ten:IVisualElement = new ClassDefinition() as SpriteVisualElement.;
this.contentGroup.addElement(ten);
}