Sprite inside DataGrid - actionscript-3

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

Related

How do I access dynamic text on the stage from inside a movieclip which is inside another moveclip in AS3?

Before asking, I've read other articles for similar issues and have not been able to get it working. I have a dynamic text on the stage with the instance name of txtX. Then in action script in frame 1 of the main timeline I add an existing movie clip, mc1, using code:
var mc = new MC();
addChild(mc);
I then add another movieclip, mc2, with action script on frame 1 of the main timeline I add it as a child to mc1.
var mc2 = new MC2();
mc.addChild(mc2);
Now, in the class (created class for export AS) for mc2 I am trying to modify the text of the dynamic text but cannot for the life of me figure out how to reference it. I want to say:
stage.txtX.text = "blah blah";
or even
parent.parent.txtX.text = "blah blah";
but I usually get an error similar to:
Access of possibly undefined property txtX through a reference with static type flash.display:DisplayObjectContainer.
The error above is for the parent.parent.txtX.text line. Please tell me what I'm doing wrong. Thanks. Also I know that the variable names are nonsensical but it's just for my example. In my code the names make much more sense.
When you put a text field on your main timeline and give it an instance name of txtX, it actually is not a direct child of stage. The stage would be it's grandparent (the main/root timeline would be it's parent).
As such, if mc1 & mc2 are it's siblings (also on the main timeline), you could access your text field like so:
MovieClip(parent).txtX.text = "Hello";
Alternatively, you can access the root (main) timeline with the root keyword:
MovieClip(root).txtX.text = "Hello";
And that should work on any timeline no matter how deep/nested.
I figured it out finally. Not sure if it was the correct way to do it but in the parent MoveieClip, I created a TextField in code:
import flash.text.TextField;
import flash.text.TextFormat;
public var txtX:TextField;
txtX = new TextField(); txtX.x=0; txtX.y=0; addChild(txtX);
public function changeTxt( t )
{
tf = new TextFormat();
tf.size=4;
tf.color = 0xFFFF00;
txtX.text = t;
txtX.setTextFormat(tf);
}
Then in my MovieClip child class I called the parent's method like this:
MovieClip(parent).changeTxt( "Hello" );
A lot more work than I'd hoped for but it seems to work.

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.

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");

How to use VGroup or HGroup in pure actionscript3?

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.

AS3 - How to assign names or tags to loader?

I have a list of images that i’ve loaded with the Loader class, but I’m having a tough time assigning them unique names.
I need unique names because I want to remove certain images after a while.
Is there a way to assign loaders a name or some unique tag so i can remove them later? thanks.
Here's part of my code
for (var i = startnum; i < endnum; i++){
var thumb = panels[i].#THUMB;
var thumb_loader = new Loader();
thumb_loader.load(new URLRequest(thumb));
thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
thumb_loader.name = i;
thumb_loader.x = (thumb_width + 20)*i;
}
I tried to use getChildByName in another function..
var myLoader:Loader = getChildByName( "1" ) as Loader;
myLoader.unload();
But it's giving me
Error #1009: Cannot access a property or method of a null object reference.
I tried to put thumb_loader as global variable, and do this
var myLoader:Loader = thumb_loader.getChildByName( "1" ) as Loader;
But it's still not working.
Thanks.
All display objects in ActionScript 3 have a name property. Whenever you create a Loader object you can assign a name to it like so:
var myLoader:Loader = new Loader();
myLoader.name = "myUniqueName";
myLoader.load( .... );
addChild( myLoader );
If you'd like to refer to the loader by the name you gave it, use the getChildByName() method.
var myLoader:Loader = getChildByName( "myUniqueName" ) as Loader;
myLoader.unload();
Please be mindful that getChildByName() will only work after you've added the Loader(s) to the display list using addChild(). Otherwise, you'll have to create something to store the references to the Loader objects in, such as an Array and refer to the loaders via that Array. For example, outside your loop you could create an Array named loadersArr. In your loop you would do:
loadersArr["uniqueName"] = thumb_loader;
Then you can refer to your loaders with your unique name through the loadersArr Array.
var loaderToUnload:Loader = loadersArr["uniqueName"];
loaderToUnload.unload();
Without seeing more of your code, its difficult to understand the scope in which this code resides and where any other code that may try to reference these Loaders resides.
Not sure I 100% understand your problem, but why not put them in an object map rather than a list and generate unique names for them if you don't have them...
var img:Image;
var img_map:Object = new Object();
var last_added:int = 0;
for each (img in yourListOfImages)
{
img_map["img_"+last_added] = img;
last_added++;
}
Depending on your environment (Flex or Flash) you can use a UID generator instead of my simplistic unique names above.
package
{
import flash.display.Loader;
public dynamic class DynamicLoader extends Loader
{
public function DynamicLoader()
{
super();
}
}
}
I believe the Loader class is a sealed class so you would want to create this class and use it instead of the normal Loader class to give it any attribute you want. I also believe that without using this DynamicLoader instead of the normal Loader, the Loader class does have the name property available to it.