ActionScript - BeginBitmapFill With BitmapData Library Asset? - actionscript-3

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

Related

Can't cast embeded MovieClip to MovieClip type

I'm having trouble trying to embed a MovieClip in an ActionScript file I'm composing in FlashBuilder.
public class ItRock extends Item
{
public static const ID:String = "rock";
[Embed (source="/../art/menu/console.swf", symbol="itRock")]
private var IconClass:Class;
public function ItRock(game:Game)
{
super(ID, game);
var icon = new IconClass();
// var icon : MovieClip = new IconClass();
// var icon : MovieClip = new IconClass() as MovieClip;
addChild(icon);
}
}
My console.swf file contains a symbol called itRock which is of type MOvieClip and set to Export for ActionScript. In my code, I want to create an instance of this symbol and add it as a child of my Item class(which extends Sprite). However, when I create an instance of the embedded class, I create an object with the type name of console_swf$831ea9c30fe7882fadc388b74e115654-652499362. I can add it as a child fine, but if I try to cast it to a MovieClip implicitly I get an error that cannot be converted to a MovieClip. If I try to cast explicitly, I just get null.
Any idea what I'm doing wrong here?
Okay, it looks like Flash will automatically convert your MovieClips to Sprites when it can. So my above code will work if I change the MovieClip classes to Sprite.
The below website describes this. It suggested that adding an extra frame to your symbol will force Flash to treat is as a MovieClip and not a Sprite - however, I've created a simple two frame animation and its still exported as a Sprite. (It even plays the animation - I thought Sprites were supposed to be just static images with no animation?)
http://chrismweb.com/2011/03/20/problems-with-embedding-swfs-in-actionscript-or-flex/

Collision detection kit with FlashPunk

I start creating my flash game using Flashpunk, for collisions i didn't want to use hitboxes because i had images (PNG) with transparent parts in them so i decided to use Collision Detection Kit, i have a problem when creating the collision list, it takes a display object as a parameter and doesn't accept flash punk spritemaps, i tried to cast the spritemap to a flash display object but it's not working, is there a way to use the CDK with flashpunk ?
override public function begin():void
{
_player = new Player(100, 100);// Entity
initCollision(_player.sprPlayer);// The Entity Spritemap
}
private function initCollision(player:Spritemap):void {
collisionChecker = new CollisionList(player); // Problem here
}
Well, you could create an empty BitmapData with the same width & height of your Spritemap and then "render" it to that BitmapData, like so:
var bmd:BitmapData = new BitmapData(64, 64, true, 0);
var sprite:Spritemap = _player.sprPlayer;
sprite.render(bmd, new Point(0,0), new Point(0,0));
collisionChecker = new CollisionList(bmd);
That should draw the spritemap's current frame to a BitmapData which you could then use for the CollisionList. The code above is an example that only demonstrates how to do this. For your actual code, it would be better to avoid constantly initializing new variables during your collision detection.

How to convert MovieClip into bitmapData

My question is quite simple -
How do I convert a MovieClip (that is dragged from the library) into bitmapData?
PS: I`ve searched a bit for this solution but the only results i get is the other way around - to convert bitmapData into MC/Sprite/bitmap
You need to make sure the MovieClip on stage has an instance name and then you can do the following:
var bitmapData:BitmapData = new BitmapData(myMovieClip.width, myMovieClip.height);
bitmapData.draw(myMovieClip);
// And to actually see it
var bitmap:Bitmap = new Bitmap(bitmapData);
this.addChild(bitmap);

AS3 Traces Bitmap as "[object Shape]" / Bug or feature, going loco

i need to use the GetPixel32 on an Object in a movieclip.
in order to get to that object i use:
var bitmap=clip.getChildAt(0);
//and then
bitmap.bitmapData.getPixel32(x, y);
however, even though the childobject is a png i get an error and using
trace(clip.getChildAt(0));
traces "[object Shape]"
so does Flash convert certain bitmaps into shapes?
please see the this fla ( http://www.sendspace.com/file/uycmm5 ) to test it yourself.
Any ideas?
Bitmaps placed in Flash's timeline are converted to shapes (with bitmapfill) on compilation,
(UPDATE)
unless the image in the library has a linkage name, in which case it works as expected and compiles to a Bitmap object.
You can however draw a new bitmap with that shape:
var shape:DisplayObject = clip.getChildAt(0);
var bmp:BitmapData = new BitmapData(shape.width, shape.height, true, 0);
bmp.draw(shape);
bmp.getPixel32(x, y);

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.