Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

how to copyPixels from an Image

New Here ,
May 18, 2014 May 18, 2014

Hello.

My question is ..... well the Tittle of the post says it all.

How do i copyPixels from a image that i have onstage?

I know that when you use .draw it is simple - 1st thing you do is pass the source of the item you want to copy from, no matter if it is bitmapData, Bitmap, Sprite, MC or something else,

but  when with copyPixels it is asking specifically for bitmapData..... and that is my problem, what if my source is a MovieClip dragged from the library to the stage.

I did read the http://help.adobe.com referance for the copyPixels but there they create directly the bitmapData like this var bmd2:BitmapData = new BitmapData(80, 40, false, 0x0000CC44);

and then copy from it. But how do you copyPixels from a MC .

Im trying something like this

var newPieceImage1:Bitmap=new Bitmap(new BitmapData(100, 100));

newPieceImage1.bitmapData.copyPixels(movingMC.bitmapData, new Rectangle(0, 0, 100, 100), new Point(0,0));

where the  movingMC is actually the picture from the library

Hope you understand my question.

PS: i also would like to ask if i want to copyPixels from a mc with Moving objects in it, how do i do it ?

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 18, 2014 May 18, 2014

this, 'because I think it is the better way of displaying what is happening  (what is moving) on an element that is taking that data from another element', isn't a compelling justification.  that doesn't explain what you think is better.

i'm not trying to give you a hard time.  but i suspect you're making your app much more complex for no good reason.

in general, it's not better to create a sequence of bitmaps instead of using a movieclip.

the only justfication i know of is to improve performance. 

...
Translate
Community Expert ,
May 18, 2014 May 18, 2014

do you have an actionscript reference to your on-stage image?

if not, you need one.  how you do that depends on how the object was added to your stage.

if yes, use that reference to create your bitmapdata instance.

p.s.  to copy pixels from any object you need to create a bitmapdata instance with those pixels.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2014 May 18, 2014

Well my library mc is exported for ActionScript with the name of Box


my whole code is this

import flash.display.MovieClip;

import flash.display.Bitmap;

import flash.display.BitmapData;

var movingMC:MovieClip = new Box();

movingMC.x = this.x;

movingMC.y = this.y;

addChild( movingMC );

var newPieceImage1:Bitmap=new Bitmap( new BitmapData(100, 100) );

newPieceImage1.bitmapData.copyPixels( movingMC.bitmapData, new Rectangle(0, 0, 100, 100), new Point(0,0) );

var sectionFromPicture_1:Sprite = new Sprite();

sectionFromPicture_1.addChild( newPieceImage1 );

sectionFromPicture_1.x = 300;

sectionFromPicture_1.y = 300;


var sectionFromPicture_1:Sprite = new Sprite();

sectionFromPicture_1.addChild( newPieceImage1 );

sectionFromPicture_1.x = 300;

sectionFromPicture_1.y = 300;


but it gives me  : TypeError: Error #2007: Parameter sourceBitmapData must be non-null.

what Do i do ? convert the movingMC into bitmapData?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2014 May 18, 2014

i think (but am not sure), the title of your thread is misleading.  it doesn't appear that you're trying to copy pixels from an image.  you want to copy pixels from a multi-frame movieclip, correct?  and that's your only issue, correct?

if yes and yes, first create multiple bitmapdata instances of each frame of your multi-frame movieclip using a loop like enterframe.

second, movieclips don't have a bitmapData property.  so this movingMC.bitmapData will be null.

to create a bitmapdata instance from a movieclip, use the draw() method.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2014 May 18, 2014

to both questions - yes, I am trying exactly that.

But as I asked ppl how to approach this issue they told me to do it with copyPixels.

The reason why is cause it is more performance/processor friendly, and I started to search for a sulution.

I actually did it with the draw() method but as they said .... it was quite agressive from terms of performance.

I did something like this

   public class Main extends MovieClip
{
  
private var myRainbow:Rainbow = new Rainbow();
  
private var mySprite:Sprite = new Sprite();

  
private var bmd:BitmapData;
  
private var countainer:Sprite = new Sprite();
  
private var bm:Bitmap = new Bitmap();

  
public function Main()
  
{
  
this.addChild( mySprite );
  mySprite
.addChild( myRainbow );
  mySprite
.x = stage.stageWidth * 0.5 - myRainbow.width * 0.5;
  mySprite
.y = stage.stageHeight * 0.5 - myRainbow.height * 0.5;

  bmd
= new BitmapData( 50, 50 );
  bm
.x = 0;
  bm
.y = 0
  countainer
.addChild( bm );
  addChild
( countainer )
  countainer
.x = 100
  countainer
.y = 100

  countainer
.addEventListener(MouseEvent.MOUSE_DOWN, onClick)
  
this.addEventListener(Event.ENTER_FRAME, onFrame);
  
this.addEventListener(MouseEvent.MOUSE_UP, drop);
  
}

  
private function onFrame( event:Event )
  
{
  bmd
.draw( mySprite, new Matrix( 1, 0, 0, 1, 0, 0 ), null, null, new Rectangle( 0, 0, 50, 50 ) );
  bm
.bitmapData = bmd;
  
}
  
//dont mind the other methods.
}

and actually i did do the job.

So is there a way of doing this with copyPixels ? or is the copyPixels method the right one for this job ?

PS : I need to be performance friendly cause Im testing it on a tablet and when i have more then 3-4 elements that draw the same thing from the main MC it is starting to drop the fps below 20-30

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2014 May 18, 2014

are you using blitting to enhance performance?  if yes, create those bitmaps prior to use and store them in an array as suggested by ocbfd.  if no, explain why you're creating those bitmaps.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2014 May 18, 2014

well Im not using blitting cause of the size of the main MC (that from which the others copy) - > it is 400x300.

And I think that if I use the blitting technic it would take a lot of memory and as far as I have seen blitting is used for small objects - like an AI enemy or maybe some particle effects like stars or bubbles, but havnt seen it for something like big picture sized elements.

So is blitting the right way to do it ? If yes how do I approach the problem with the size of the main MC ?

PS: you can see what i have done with code above i have provided, in this link https://drive.google.com/file/d/0B5u6hrR2w7UEYmFHbHdlWnlWNjA/edit?usp=sharing ( it is a simple swf file ).

As you can see - the main MC is quite big and the others ( the ones that copy the main can also be moved) only copy a fragment of the main MC.

Also by using draw even now when i start the SWF from my PC  the fps almost never gets up to 60

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2014 May 18, 2014

if you're not blitting, why are you creating a sequence of bitmapdata objects of moving MC.

(and i don't generally download files unless i'm hired.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2014 May 18, 2014

because I think it is the better way of displaying what is happening  (what is moving) on an element that is taking that data from another element.

( also i have uploaded my code and the swf on this link http://wonderfl.net/c/4qie/edit so now it doesnt need to be downloaded.

PS: there wasnt a way to upload the actual movingMC so I had to create it dynamically ( the createMainMC represents it but it is actually a more complex MovieClip that cant be just coded ( it has like some cars moving, people moving , etc. ) ).

I`ve tried to make things as simple as to show what Im currently trying to do.

But as I have said, when i test this code ( and have the real/comlpexed movingMC ) it is starting to loose performance.

So am I supposed to use blitting with that or something else ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2014 May 18, 2014

this, 'because I think it is the better way of displaying what is happening  (what is moving) on an element that is taking that data from another element', isn't a compelling justification.  that doesn't explain what you think is better.

i'm not trying to give you a hard time.  but i suspect you're making your app much more complex for no good reason.

in general, it's not better to create a sequence of bitmaps instead of using a movieclip.

the only justfication i know of is to improve performance.  but unless you're publishing for a mobile device performance isn't usually an issue.

in any case, in exchange for improved performance (ie, blitting) you increase your apps complexity and you increase its memory use.  and it's easy to exceed the available system memory.

on the other hand, perhaps you just want to learn how to blit or partially-blit an app.  that's a good reason to pursue this approach.

if so, make sure you're not going to exceed system memory:  how large (100 x 100?) is movingMC?  how many frames does it contain?  will you need to create transformed bitmaps (because you're rotating/changing alpha/ etc) also?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2014 May 18, 2014

Ok thanks a lot for your answers and time, it was really helpful for me. I guess I will need to blitt it just it would be a ton of memory usage.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2014 May 18, 2014

I'm an animator/artist and programmer.  Most coders aren't doing the artwork.  Flash is far more powerful when it's creating the animations at runtime vs. using pre-generated artwork.  Everybody will point you to spritesheets, but that is because they don't do both programming and artwork.

Think about it.  You can modify all the artwork at runtime.  You don't have to create spritesheets for every variation, just draw it in flash at runtime.  So your user wants a green car, blond hair, and a black jacket.  You don't need to create sprites, just code it.

Think about the size of your app too.   All those sprites have to be included or downloaded.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2014 May 18, 2014
LATEST

you're welcome.

the amount of memory used depends on the size of your movieclip, the number of frames and whether any transformed movieclips are needed

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2014 May 18, 2014

You can do whatever you want with normal movieclips in flash - draw, dynamic text, import images - then turn the movieclips into a bitmap for gpu optimization.  When you're creating the movieclips, or when creating bitmaps out of the movieclips, you get a cpu/gpu hit.  So don't so it during play, do it during loading screen (like in games).

Here's a function that returns a bitmap of a movieclip:

bmpMain = convertBMP(mcTest.sTest, 752, 115);

mcTest.removeChild(mcTest.sTest);

mcTest.sTest = null;

mcTest.addChild(bmpMain);

private function convertBMP(pClip: MovieClip, pWidth: int, pHeight: int): Bitmap {

  var bd: BitmapData = new BitmapData(pWidth, pHeight, true, 0x00000000);

  bd.draw(pClip, null, null, null, null, true);

  aBitmaps.push(bd);

  var bmp: Bitmap = new Bitmap(bd, "auto", false);

  bmp.smoothing = true;

  return bmp;

}

Remove the original moviclip and all references to it.  It pushes the bitmap into an array for reference:  aBitmaps   If you want to reference the bitmap directly, add it to a container movieclip.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines