adding Bitmaps to various frames in movieClip in as3 - actionscript-3

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)

Related

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.

Loading images does not load them into memory

I am loading a batch of 150 HD images into my app - it is basically a 3D view of an object. Once I load the image files using Loader instances I store the loaders' first child's bitmapdata in a Vector. When all of the loaded, I want to begin to "rotate" the object = meaning I am simply swapping the images. I take the Vector where I have the bitmapdatas and draw them onto a canvas bitmapdata one after the other. No science there, it all works as intended.
The problem is that once all the images are loaded and stored in a vector and BEFORE they are drawn to the canvas, they are not in the memory. That means that the first rotation of my 3D object (-> all 150 images drawn) is really slow. After the first rotation there is no problem and all is fluid. My question is: is there a way to force the images to get loaded into the memory without drawing them onto the stage? I expected that they would simply get loaded to memory once they are loaded to the app (Wrong!).
I tried to use addChild() instead of drawing them to a canvas bitmap, same result. I don't think the code is necessary but just in case:
private var _loaders:Vector.<Loader>;
private static const NAME:String = "img_00";
private static const MIN:uint = 0;
private static const MAX:uint = 150;
private var _loaded:uint = 0;
private var _currentFrameIndex:uint = 0;
private var _canvas:Bitmap;
private var _bitmaps:Vector.<BitmapData>;
private var _destPoint:Point;
public function loadImages():void {
var s:String;
for(var i:int=MIN; i<=MAX; i++) {
if(i < 10) s = "00" + i;
else if(i < 100) s = "0" + i;
else s = i.toString();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadHandler);
loader.load(new URLRequest("images/JPEG/"+ NAME + s + ".jpg"));
_loaders.push(loader);
}
}
private function loadHandler(e:Event):void {
_loaded++;
if(_loaded > (MAX - MIN)) {
_bitmaps = new Vector.<BitmapData>(_loaders.length);
for(var i:int=0; i<_loaders.length; i++) {
var loader:Loader = _loaders[i];
_bitmaps[i] = Bitmap(loader.getChildAt(0)).bitmapData;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadHandler);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loadHandler);
loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loadHandler);
}
setFrame(0);
dispatchEvent(new Event(LOAD_COMPLETE));
}
}
public function setFrame(frame:uint):void {
if(frame >= 0 && frame < _bitmaps.length) {
_currentFrameIndex = frame;
var bmpData:BitmapData = _bitmaps[_currentFrameIndex];
_canvas.bitmapData.copyPixels(bmpData, bmpData.rect, _destPoint);
}
}
"Not in the memory" means that the images are loaded, but not yet decoded, and this decode is done on the fly, and this takes the time you observe as slowness. You can attempt to "virtually" rotate the image by having a bitmap that's not yet added to stage to be the reference to each of the bitmapDatas of your vector. Make a progress bar that shows how much of the vector has already been decoded, and once this happens, display the bitmap and give the user smooth rotation.
addEventListener(Event.ENTER_FRAME,prerender);
var b:Bitmap=new Bitmap();
/* optional
b.x=stage.stageWidth;
b.y=stage.stageHeight;
addChild(b);
*/
var vi:int=0;
var sh:Shape=new Shape();
sh.graphics.lineStyle(4,0,1); // a simple progress bar
sh.graphics.moveTo(0,0);
sh.graphics.lineTo(100,0);
sh.scaleX=0;
sh.x=stage.stageWidth/2-50; // centered by X
sh.y=stage.stageHeight/2;
addChild(sh);
function prerender(e:Event):void {
if (vi==_bitmaps.length) {
// finished prerender
removeEventListener(Event.ENTER_FRAME, prerender);
removeChild(sh);
// removeChild(b); if optional enabled
setFrame(0);
return;
}
b.bitmapData=_bitmaps[vi];
vi++;
}
Also, it's always better to assign the bitmapData property to a Bitmap object if you don't plan to have that bitmapdata changed. So, instead of your _canvas.bitmapData.copyPixels(bmpData, bmpData.rect, _destPoint); you just do _canvas.bitmapData = bmpData; and it'll work.
UPDATE: Your issue might as well nail to the last point, that is assigning instead of copying. If your destPoint is something else than (0,0), you just make another Bitmap object on top of your _canvas with desired offset, and assign bitmapdatas in there. I have remembered that when I first made multiple animated objects based on a single Vector.<BitmapData> like yours, and tried doing copyPixels(), my animations were jittering and not displaying proper frames, but once I did _bitmap.bitmapData=_bitmaps[currentFrame] everything went as smooth as it should be.

ActionScript: How to start something that has a method: .stop( );

Alright, so I'm making this little game in ActionScript using Flash Builder. The duck asset has an animation. I used duck.stop(); to stop the Movie Clip so the animation doesn't play.
However, when I click on the duck, I need to figure out a way to once against start the Movie Clip. Does anyone know a way I could go about doing this?
private function makeDucks(amount:uint):void
{
for(var j:int = 0; j < amount; j++){
var duck:Duck = new Duck();
addChild(duck);
duck.x = j * (duck.width + duck.width / 3);
// .stop stops the MovieClip
duck.stop();
}
}
duck.addEventListener(MouseEvent.CLICK, onDuckClicked);
private function onDuckClicked(e:MouseEvent):void
{
var duck:MovieClip = e.target as MovieClip;
if(duck)
duck.play();
}

AS3 can't find the correct way to gotoAndStop a movieclip instance in as3

I have a movieclip "achtergrond" from my library which I put on stage with a function like this:
function set_game ()
{
oefNr = 4;
var bg:achtergrond = new achtergrond();
bg.x = 0;
bg.y = 0;
bg.name = "bg";
bg.gotoAndStop ("uit");
addChild (bg);
set_next ();
}
The movieclip contains 2 frames "aan" and "uit" and it starts on the frame "uit".
Further in my game I want to set the frame to "aan" while a sound is playing, like this:
function playSnd ():void
{
getChildByName("bg").gotoAndStop("aan");
snd = new Sound(new URLRequest("phonetic_" + curArr[curSnd] + ".mp3"));
cnl = snd.play();
cnl.addEventListener (Event.SOUND_COMPLETE, completeSnd);
}
But for the life of me I can't find the correct way to do this. Flash keeps going on about displayObjects and other things, and I have no clue why I can't address my movieclip. Actually, I have a clue, but no more than that. I don't understand this part of Flash very well yet.
The answer is that if I try to access my clip like this: this["bg"] or getChildByName("bg") I am referring to the DisplayObject.
This does not have all the methods of a MovieClip, like the gotoAndStop I need in this case.
I declared a new variable:
var movie:MovieClip;
Then I cast my DisplayObject as a MovieClip and put it into the var movie:
function set_game ()
{
oefNr = 4;
var bg:achtergrond = new achtergrond();
bg.x = 0;
bg.y = 0;
bg.name = "bg";
bg.gotoAndStop ("uit");
addChild (bg);
movie = this.getChildByName("bg") as MovieClip;
set_next ();
}
Now I can use MovieClip-specific methods like gotoAndStop:
function playSnd ():void
{
movie.gotoAndStop("aan");
snd = new Sound(new URLRequest("phonetic_" + curArr[curSnd] + ".mp3"));
cnl = snd.play();
cnl.addEventListener (Event.SOUND_COMPLETE, completeSnd);
}
Answer inspired by this answer
bg.name = "bg";
actually many times this code doesn't work properly. So, instead of setting name, you should get real instance name of your "achtergrond" object and than you have to use this instance name.
Another Solution:
I think achtergrond is single object so why do you use it with "getChildByName"? Use it like bg.gotoAndStop();.

HitTest for objects not yet on Stage

I need to add a MovieClip to stage, the limitation being that it should only be added to an empty area on the stage. The stage itself either contains complex shapes or is manipulable by the user i.e. he can drag/move objects to change the empty area. The hitTest and hitTestObject methods need DisplayObject already available on the stage. What is the right way to go - the only solution I can imagine is having added my object on the stage and then repeatedly doing hit tests?
[Imagine it to something like adding sprites in a video game - they must spawn in empty regions; if they pop out from inside of each other, then it'll look really odd.]
Well, when you create a new class, just turn it off with a variable and set the visibility to false, then loop until there is no hitTest.
A silly example:
public class someClass extends Sprite
{
private var objectsOnStage:Array;
public function someClass(objectsArray:Array) {
objectsOnStage = objectsArray;
visible = false;
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event){
removeEventListener(Event.ADDED_TO_STAGE, init);
addEventListener(Event.ENTER_FRAME, SEARCH);
}
private function SEARCH(e:Event) {
var doesHit:Boolean = false;
x = Math.round(Math.random() * (550 - 0)) + 0;
y = Math.round(Math.random() * (400 - 0)) + 0;
for (var i:int = 0; i < objectsOnStage; i++) {
if (doesHit) break;
if (this.hitTestObject(objectsOnStage[i])) {
doesHit = true;
}
}
if (doesHit) return;
placedInit();
}
private function placedInit() {
visible = true;
removeEventListener(Event.ENTER_FRAME, SEARCH);
//now init the stuff you want.
}
}
You just check if bounding boxes of both clips overlaps. Like this:
import flash.geom.Rectangle;
import flash.display.MovieClip;
// create simple movie clips that has a rectangle shape inside
var sym1 : MovieClip = new Sym1();
var sym2 : MovieClip = new Sym2();
// get a rectanle of both clipt
var boundingBox1 : Rectangle = sym1.getBounds(this);
var boundingBox2 : Rectangle = sym2.getBounds(this);
// check if bounding boxes of both movie clips overlaps
// so it works like hitTestObject() method
trace( boundingBox1.intersects( boundingBox2) )
I know this post is super old, but in case it helps anybody --
If you need to do a hit test on a movieclip that isn't on the stage. A workaround is to rasterize it to a bitmap first.
var bitmapData:BitmapData = new BitmapData(mc.width, mc.height, true, 0x0000000);
bitmapData.draw(mc);
if (bitmapData.getPixel32(x, y) > 0) {
// Hit true.
}