check if shape is bitmap in as3 - actionscript-3

I have many single frame movieclips on stage.
some of them are drawn using vector shapes in flash.
some of them are bitmaps loaded from an external .png file. note that in this case the display objects are of type shape, not of type bitmap.
I would like to export the movieclips containing vector shapes to svg documents. and the movieclips with bitmaps to bitmapData.
the problem is I am unable to differentiate between the 2 in actionscript.
I have only had success when converting the shape into Bitmap type with AS Linkage. however, this is not ideal as we would have to inspect all movieclips manually. Is there a way to differentiate between a shape that is vector and a shape that is bitmap?

I found a solution.
ensuring that there is not GraphicsBitmapFill in it was enough for my use case
for(var i :int = 0; i < target.numChildren; i++) {
var s:Shape = target.getChildAt(i) as Shape;
if (s) {
var g:Vector.<IGraphicsData> = s.graphics.readGraphicsData();
for(var j:int = 0; j < g.length;j++) {
var bitmap:GraphicsBitmapFill = g[i] as GraphicsBitmapFill;
if (bitmap) {
hasBitMapChildren = true
}
}
}
}

Related

AS2 carousel gallery to AS3

I'm making carousel gallery for my class, however we got a code written in AS2 and have to transform it into AS3. I have a problem with two fragments:
// create the objects for the circular motion
for (var i:Number = 0; i < numberOfObjects; i++)
{
this.createEmptyMovieClip('object'+i, i);
this['object'+i].loadMovie('p'+(i+1)+'.jpg');
}
Where I'm adding movie clips as objects, and then I want to use them as:
// create the motion along the circular path
this.addEventListener(Event.ENTER_FRAME, function()
{
// loop over all the objects
for (var i:Number = 0; i < numberOfObjects; i++)
{
thisObj = this['object'+i];
placeObj(thisObj,i);
displayObj(thisObj);
}
})
Of course I get an exception:
Scene 1, Layer 'actions', Frame 1, Line 85 1120: Access of undefined property thisObj.
I know that createEmptyMovieClip as well as loadMovie don't work in AS3, and I probably should make MovieClip as new MovieClip, however I still have troubles how to make this work. I'll be truly grateful for any advices.
You need to apply var keyword before you introduce a new variable.
Correct your as3 code this:
var thisObj:Sprite = this['object'+i];

Drawing BitmapData to Sprite still using bitmapdata itself?

I'm calling images from library to be use. But because I will be constantly add and remove the images, so I tried to copy the bitmapdata to a sprite and reuse the sprite.
private function loadImage():void {
for (var i:int = 1; i < typeAmount + 1; i++) {
SlotClass = Main.queue.getLoader('main_uiMC').getClass('slot' + i + 'bmp') as Class;
bmpDSlot = new SlotClass() as BitmapData;
bmpDSlotV.push(bmpDSlot)
}
}
private function bitmaping():void {
for (var i:int = 1; i < typeAmount + 1; i++) {
slotS = new Sprite()
slotS.graphics.beginBitmapFill(bmpDSlotV[i - 1], new Matrix, false, true)
slotS.graphics.drawRect(0, 0, bmpDSlotV[i - 1].width, bmpDSlotV[i - 1].height);
slotS.graphics.endFill();
bmpV.push(slotS)
}
Every time I duplicate the sprite, flashdevelop's profiler showed that the bitmapdata is being added as well. Even when I use removeChild to remove the Sprite, the memory usage won't decrease.
Is there a way to better copy the content of the bitmapdata, and can be completely remove when I remove the sprite?
*i will still be using the image, just that on that particular round i would like to remove the sprite that has the image.
You should use a single BitmapData object, create numerous Bitmap objects and operate these.
private function bitmaping():void {
for (var i:int = 1; i < typeAmount + 1; i++) {
slotS = new Bitmap(bmpDSlotV[i-1]);
bmpV.push(slotS);
}
Creating another Bitmap like this does not add another BitmapData, it uses existing one as reference.
Well your choice is to reuse the BitmapData. And because you need multiple instances of the same image, you need to create new one for each object that you would like to add. And this would be a Bitmap with the old BitmapData:
var originalBMD:BitmapData = instantiateItSomehow();
var first:Bitmap = new Bitmap(originalBMD);
var second:Bitmap = new Bitmap(originalBMD);
This way you would reuse the BitmapData and I think the memory will increase only because of the Bitmap objects, but the data itself should not be duplicated.

How do I change the hitbox of a movie clip in actionscript 3 to not use the bounding box?

We have an object (mc_robert) that collides with another object (mc_left). When the collision occurs, we have an action take place. What we're trying to figure out is how to change the collision box of the object (mc_robert) so that the objects overlap when the collision detects. We don't want the default collision box of the object (mc_robert). Any help that can be offered would be greatly appreciated.
This is what we have for our code currently:
var numX:Number = 0;
var numY:Number = -2;
addEventListener(Event.ENTER_FRAME,loop);
function loop(e:Event)
{
mc_robert.y += numY;
mc_robert.x += numX;
if (mc_robert.hitTestObject(mc_left))
{
numX = -2;
numY = 0;
mc_robert.rotation = -90;
}
}
For pixel perfect collision convert the shapes into BitmapData and use the BitmapData's Hit-test method.
If you don't want to do the above approach you can use hitTestPoint and create multiple points in a for loop to do the collision.
eg: Haven't tested the following code but the concept is there. This will create a bounding box, or you can use some trig to collide from a circle or custom create the points.
for(var y:int = 0;y<2;y++){
for(var x:int=0;x<2;x++){
var w:int = mc2.width*x;
var h:int = mc2.height*y;
if(mc1.hitTestPoint(mc2.x+w, mc2.y+h, true)){
trace("collides");
}
}
}
How ever the solution I would use for the best accuracy would be a BitmapData collision.

How do you select a random colortransform from an array and applying it to a movieclip in Flash ActionScript 3?

I am trying to make a Tetris game in ActionScript 3. I am using a movieClip and colorTransform array to create colorful, random, unique pieces. Randomizing the frame in the movie clip works well enough, but when I try to apply a random color tint using the clip's colorTransform property, I get this:
Tetris.as, Line 342 1067: Implicit coercion of a value of type Number to an unrelated type flash.geom:ColorTransform.
Here is some sample code:
private function LandTetromino():void
{
var cT:int = currentTetromino;
var landed:Tetris_Shapes;
for (var i:int=0; i<shapeBuilder[cT][currentRotation].length; i++)
{
for (var j:int=0; j<shapeBuilder[cT][currentRotation][i].length; j++)
{
if (shapeBuilder[cT][currentRotation][i][j]==1)
{
landed = new Tetris_Shapes();
landed.transform.colorTransform = Math.floor(Math.random()*allcolorTransforms.length);
landed.gotoAndStop(Math.floor(Math.random()*12));
addChild(landed);
landed.name="r"+(startingRow+i)+"c"+(startingCol+j);
boardArray[startingRow+i][startingCol+j]=1;
}
}
}
removeChild(tetrisShape);
dropTime.removeEventListener(TimerEvent.TIMER, OnTimeTick);
dropTime.stop();
CheckForCompleteLines();
}
allcolorTransforms is ColorTransform Array?
if right. correct follow code.
landed.transform.colorTransform = allcolorTransforms[Math.floor(Math.random()*allcolorTransforms.length)];

adding Bitmaps to various frames in movieClip in as3

I have a movieClip and I want attach to this movieClip bitmaps. I want attach each bitmap to different frame of movieClip. I have tried something like this, but it is not working. It is for memory game I am creating.
for(var i : int = 0; i < cardList.length; i++){
var helpVar : int = cardList[i].pictureOfCard;
cardList[i].gotoAndStop(cardList[i].pictureOfCard+2);
var bitmap : Bitmap = new Bitmap(bitMapArray[helpVar].bitmapData.clone());
cardList[i].addChild(bitmap);
cardList[i].gotoAndStop(1);
}
You could simply load all Bitmaps and only show the one, that should be visible right now. e.g.
function ShowFrame(nr:int):void{
for(i:int = 0; i<bitMapArray.length; i++){
bitMapArray[i].visible = false;
}
bitMapArray[nr].visible = true
}
My AS3 skills are rusty, so this might need some syntax correction, but it works in theory.
var i :int = 0;
processNext();
function processNext():void
{
cardList[i].addEventListener(Event.FRAME_CONSTRUCTED, onFrameConstructed );
cardList[i].gotoAndStop(cardList[i].pictureOfCard+2);
}
function onFrameConstructed( e:Event ):void
{
cardList[i].removeEventListener(Event.FRAME_CONSTRUCTED, onFrameConstructed );
var helpVar : int = cardList[i].pictureOfCard;
var bitmap : Bitmap = new Bitmap(bitMapArray[helpVar].bitmapData.clone());
cardList[i].addChild(bitmap);
if( i < cardList.length - 1 )
{
i++;
processNext();
{
else
trace("All done");
}
After some time I discover that what I was trying to achieve is probably not possible in as3. The way I solved my problem is that everytime i gotoAndStop to frame I need have there bitmap I addChild(bitmap) and everytime I gotoAndStop other frame where there should not be bitmap I removeChild(bitmap) from movieClip. (So it is very similar aproach to makeing bitmap visible and invisible)