How to use VGroup or HGroup in pure actionscript3? - actionscript-3

I'm developing a flash app by using free Flex SDK and text editor and compiling in command line.
I want to use VGroup or HGroup in my actionscript to manage positions of DisplayObjects.
I wrote the following code:
import spark.components.*
import flash.text.*
var group:VGroup = new VGroup;
var text:TextField = new TextField
text.text = 'abc';
var sprite = new Sprite;
sprite.graphics.lineStyle(2, 0x000000);
sprite.graphics.drawRect(0, 0, 100, 100);
stage.addChild(group);
group.addElement(sprite); // runtime error
group.addElement(text); // compile error
But adding Sprite to VGroup causes runtime error:
TypeError: Error #1034: Type Coercion failed:
cannot convert flash.display::Sprite to mx.core.IVisualElement.
And adding TextField to VGroup causes compile error:
Error: Implicit coercion of a value of type flash.text:
TextField to an unrelated type mx.core:IVisualElement.
How to use VGroup or HGroup in pure AS3?
What is the difference between DisplayObject and IVisualElement?
UPDATE:
I tried the 1st way of www.Flextras.com's answer, SpriteVisualElement and StyleableTextField.
I wrote the following code:
package {
import flash.display.*
import spark.core.SpriteVisualElement
//import spark.components.supportClasses.StyleableTextField // compile error
import spark.components.VGroup
import flash.text.*
[SWF(backgroundColor=0xffffff, width=500, height=500, frameRate=12)]
public class VGroupTest extends Sprite {
function VGroupTest() {
//var text:StyleableTextField = new StyleableTextField
//text.text = 'abc';
var sprite1:SpriteVisualElement = new SpriteVisualElement;
sprite1.graphics.lineStyle(2, 0x000000);
sprite1.graphics.drawRect(0, 0, 100, 100);
sprite1.width = 200
sprite1.height = 200
var sprite2:SpriteVisualElement = new SpriteVisualElement;
sprite2.graphics.lineStyle(2, 0xff0000);
sprite2.graphics.drawRect(0, 0, 200, 200);
sprite2.width = 300
sprite2.height = 300
var group:VGroup = new VGroup;
group.gap = 10
group.width = 400
group.height = 400
this.stage.addChild(group);
// the following codes show nothing
//group.addElement(text);
group.addElement(sprite1);
group.addElement(sprite2);
// the following codes show 2 rectangles
//this.stage.addChild(sprite1)
//this.stage.addChild(sprite2)
}
}
}
But
import spark.components.supportClasses.StyleableTextField
caused the following error
40 Error: Definition spark.components.supportClasses:StyleableTextField could not be found
And no SpriteVisualElement is shown on the screen.
Am I missing something?

You're using the right conceptual approach. However, elements in a group (or VGroup or HGroup) must implement IVisualElement, which neither Sprite nor TextField implement.
You have a few options to consider:
Use a SpriteVisualElement instead of a Sprite; or use a StyleableTextField instead of a TextField.
Wrap up the Sprite or TextField as a child of UIComponent; then use that UIComponent as the element in the group.
Use MX Containers, such as HBox or VBox.
Use a UIComponent instead of a Group. You'll have to write your own layout code in updateDisplayList() for this to work.
My preference is the first approach, followed by the fourth approach. Approach 2 adds a lot of extra coding, and approach 3 is undesirable due to the dependency on the MX/Halo architecture.

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.

StageWebView in an AS3-Script

I allreday read a lot of helpful stuff in that forum. Now it's my first time I ask for specific help.
I'm pretty new to Flash and have a problem I struggle for more then a week now. The most efficient and elegant way for my problem is to put a StageWebView-Call into an as-File.
That would be the plan:
In my flash-File: Show a PDF document "xyz" and put it on the stage.
I alreday tried it with Switch-Case - But then I have trouble to get rid of the PDF's.
That was my Idea:
First the new as-File...
package {
import flash.display.MovieClip;
import flash.media.StageWebView;
import flash.geom.Rectangle;
import flash.filesystem.File;
import flash.display.Sprite;
import flash.display.Stage;
public class mypdf {
public var MyWebView:StageWebView
public var file:String
public var pdf:File
public function mypdf(ActFile:String) {
MyWebView = new StageWebView();
file = ActualFile; //MARKING #1
pdf = File.applicationDirectory.resolvePath(file);
MyWebView.stage = stage; //MARKING #2
MyWebView.viewPort = new Rectangle (200, 200, 400, 400);
MyWebView.loadURL(pdf.nativePath);
}
}
}
Than I want to call that in my flash-File...
stop();
var mynewpdf:mypdf = new mypdf("test.pdf");
Two erros are shown:
1120: Access of undefined property error ActualFile (at Marking #1)
1120: Access of undefined property error Stage (at Marking #2)
With a lot more work I could avoid the first error by defining a lot of different as-Scripts for each pdf.
My main problem is the second error.
It would be really nice if someone had any good ideas.
Bye,
Stephan
The second error means that you need to pass the stage to the web view. Either pass it to mypdf class as parameter, or make mypdf DisplayObject (extend Sprite for example) and add it to stage.
I'm not sure this will solve your issue anyways - I think StageWebView can simply display html. The PDF is displayed in your browser because an external plugin for that is launched.
In AIR the situation seems different: http://sujitreddyg.wordpress.com/2008/01/04/rendering-pdf-content-in-adobe-air-application/
StageWebView is wont support for nativePath, instead of using this, you can try with pdf.url. And StageWebView also having support for open .pdf files.
public function mypdf(ActFile:String) {
MyWebView = new StageWebView();
file = ActualFile; //MARKING #1
pdf = File.applicationDirectory.resolvePath(file);
MyWebView.stage = stage; //MARKING #2
MyWebView.viewPort = new Rectangle (200, 200, 400, 400);
addChild( MyWebView );
MyWebView.loadURL(pdf.url);
}
Because, StageWebView will support file:/// format, but in nativePath we got C://.., so, this will help you. Or
Simply convert your StageWebView to Display object, and then added it to your container by using addElement().
You can convert it by,
var _view:SpriteVisualElement = new SpriteVisualElement();
_view.addChild( MyWebView);
this.addElement( view );
To test by, simply call this method in added_to_stage method to test if stage is having or not. This error will come if stage is not setted means also.

AS3 - types and classes

I'm not exactly sure but i'm guessing my problem has to do with how i'm declaring my variables.
Is the below code legal in AS3?
var fish1:Fish = new Fish;
var fish2:Fish = new Fish;
var fish3:Fish = new Fish;
var fish4:Fish = new Fish;
addChild(fish1);
addChild(fish2);
addChild(fish3);
addChild(fish4);
fish1.x = 0;
fish2.x = 150;
fish3.x = 300;
fish4.x = 450;
i'm getting compiler errors for each line of addChild saying:
Main.as, Line 14 1180: Call to a possibly undefined method addChild.
Main.as, Line 14 1120: Access of undefined property fish3.
and for every line where i'm specifying the x coordinates of my fish i'm getting
a compiler error saying
Main.as, Line 15 1120: Access of undefined property fish4.
the fish variables are of type Fish and I've defined them in my library in my .fla file.
Thank you in advanced!
Your Class needs to subclass some form of DisplayObjectContainer, of which MovieClip and Sprite are two possible choices (find out, be sure).
But I suspect that the real probem is you're writing Class code like it's timeline code. I think you probably have strict mode off, which is why you're ot getting helpful compile-time errors which would help anyone who's familiar with AS3 (though probably not you) to figure out immediately that your code should look more like
class Main extends Sprite {
public var fish1:Fish = new Fish();
public var fish2:Fish = new Fish();
public var fish1:Fish = new Fish();
public function Main() {
addChild(fish1);
addChild(fish2);
addChild(fish3);
//not going to type this crap.
//positioning code (and addChild) is a waste of time.
//that's what the stage is for!
}
}

Sprite inside DataGrid

I have a DataGrid (listDataGrid) on stage, with DataProvider (listDataProvider) as source. I want to be able to add Sprites into one of the DataGrid's columns, which would change color depending on where the mouse is clicked.
I have a separate class to handles CellRenderer, and to add data from a static MovieClip class (present in library before run time). However, I have no clue how to achieve the same kind of functionality for dynamic content.
Edited:
According to flash docs, CellRenderer can also be a type Sprite. As such, I was looking around to see if I can create a DataGridColumn to render a Sprite that I create at run time.
My code looks like this:
import fl.data.DataProvider;
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import flash.display.Sprite;
var newBox:Sprite = new Sprite;
newBox.graphics.beginFill(0);
newBox.graphics.drawRect(0,0,20,20);
newBox.graphics.endFill();
var dp:DataProvider = new DataProvider;
var _testDataGrid:DataGrid = testDataGrid;
testDataGrid.rowHeight = 20;
_testDataGrid.dataProvider = dp;
var colA:DataGridColumn = new DataGridColumn("A");
var colB = new DataGridColumn ( "sprite" ) ;
colB .cellRenderer = Sprite;
colB .dataField = "sprite";
_testDataGrid.addColumn(colA);
_testDataGrid.addColumn(colB);
dp.addItem({A:"test", sprite:"newBox"});
This results in a following error:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at fl.controls::DataGrid/drawList()
at fl.controls::DataGrid/draw()
at fl.core::UIComponent/callLaterDispatcher()
Have no clue where to take it from here :x
try this:
dp.addItem({A:"test", sprite:newBox});
i.e. no quote marks

ActionScript - BeginBitmapFill With BitmapData Library Asset?

I've imported an image asset (Background.jpg) to my Flash CS5 library and exported it to ActionScript as class Bitmap with a base type of BitmapData.
the following code returns the following error:
backgroundTexture = new Shape();
backgroundTexture.graphics.beginBitmapFill(Background);
backgroundTexture.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
backgroundTexture.graphics.endFill();
1067: Implicit coercion of a value of
type Class to an unrelated type
flash.display:BitmapData.
so what's the error?
You just need an instance of the Background BitmapData object:
backgroundTexture.graphics.beginBitmapFill(new Background());
Background is a reference to the class. new Background() creates an instance of the class.
I have more experience with Flex than Flash, so I don't know the UI details, but I believe what you want is:
var background:BitmapAsset = new Background() as BitmapAsset;
backgroundTexture.graphics.beginBitmapFill(background.bitmapData);
This is assuming that your UI generates the following ActionScript or its equivalent:
[Embed(source="Background.jpg")]
public var Background:Class;
See:
http://www.adobe.com/devnet/flash/quickstart/loading_images_library_as3.html
http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html