user385762
user385762

Reputation: 591

Change bitmap image in flash

I hope someone can help me here. I've been tasked to write a chat engine in actionscript3 (AIR) and every chat message has an image of the poster associated with it. So I have 5 chat messages on the screen at any one time but it will be obvious that the images associated with message e.g. #1 changes all the time because the last person posting is different.

What I believe is wrong is to use the .addChild method every time I need to replace the image because it will just keep creating more stuff on the stage. What I tried to do is the code below but it refuses to update the image. The way I thought it should work:

Line 1 -- creates a movie class image which is added to "this.ChatLine"

When the next image comes in Line 2 -- is suppose to overwrite "ImageBitMap" with the new image data but that bit doesn't work.

1 -- this.ChatLine:bmChatImage = new mbChatImage();

2 -- this.ChatLine.LoadChatImage("NewImage.png");

package com.mypackage {

import  flash.display.MovieClip;
import  flash.events.*;
import  flash.display.Loader;
import  flash.display.Bitmap;
import  flash.display.BitmapData;   
import  flash.net.URLRequest;   
import  flash.display.BitmapData;
import  flash.geom.Rectangle;
import  flash.geom.Point;


public class bmChatImage extends MovieClip {

    public var  ChatImage               =   new Loader();
    private var ImageBitMap:Bitmap          =   null;
    private var AddedBitMap:Boolean         =   false;
    private var     ChatImageBMD:BitmapData         =   null;
    private var Parent                  =   null;
    private var ImageURL:String             =   "";

    public function bmChatImage() {

        ChatImage.load(new URLRequest("http://mydomain/default.png"));
        ChatImage.contentLoaderInfo.addEventListener(Event.COMPLETE,        DoneLoadImage);         
        ChatImage.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

    }

    public  function    LoadChatImage(pImageURL:String,pChatSequence:int) {

        ImageURL        =   pImageURL;
        ChatSequence        =   pChatSequence;
        ChatImage.load(new URLRequest(ImageURL));
        ChatImage.contentLoaderInfo.addEventListener(Event.COMPLETE, DoneLoadImage);            
        ChatImage.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

    }

    protected function ioErrorHandler(e:IOErrorEvent):void{
            trace("ERROR",e.text);          
    }

    private function    DoneLoadImage(ThisObject:Event)
    {
        ChatImageBMD            =   Bitmap(ChatImage.content).bitmapData.clone();
        ImageBitMap         =   new Bitmap(ChatImageBMD);

        if (AddedBitMap == false) {
            AddedBitMap     =   true;
            this.addChild(ImageBitMap);
        }
    }
}   
}

Upvotes: 0

Views: 1752

Answers (1)

Kodiak
Kodiak

Reputation: 5978

The problem is that you wrote

ImageBitMap         =   new Bitmap(ChatImageBMD);

And you only add ImageBitMap to the display list the first time. So after that, you are only changing the reference "ImageBitMap", not the displayed object. You should write something like that:

ChatImageBMD = Bitmap(ChatImage.content).bitmapData.clone();
if (!ImageBitMap)
{
    ImageBitMap = new Bitmap(ChatImageBMD);
}
else
{
    ImageBitMap.bitmapData = ChatImageBMD;
}

tips : You can avoid using your boolean AddedBitMap by checking ImageBitMap.parent.

And by the way, the first letter of your variables should be in lower case to make the difference with types and classes ;)

Upvotes: 1

Related Questions