Copy link to clipboard
Copied
I've tried so many things, but now I fold again. I'm trying to remove "holder" before I create it again.
function duplicateMov(clone:MovieClip, obj:MovieClip) {
var bmd:BitmapData=new BitmapData(clone.width * SCALE, clone.height * SCALE, true, 0x00000000);
var scaleMatrix:Matrix = new Matrix();
scaleMatrix.scale(SCALE, SCALE);
bmd.draw(clone, scaleMatrix);
if (obj.holder != null && contains(obj.holder))
{
obj.removeChild(holder);
}
var holder:Bitmap = new Bitmap();
holder.bitmapData = bmd
obj.addChild(holder);
}
Copy link to clipboard
Copied
What values do you get when you trace what you are testing in that conditional...
trace(obj.holder, contains(obj.holder));
if (obj.holder != null && contains(obj.holder))
{
You are likely going to have a problem declaring the holder object after you try referencing it.
Copy link to clipboard
Copied
"Parameter child must be non null."
But isn't that the point of the line?
if (obj.holder != null && contains(obj.holder)) --> if (it) is not = to null and contains (it) then i can remove (it)
Everything besides the lines for removing the child works. The problem is that the obj:Movieclip keeps adding children instead of replacing the same one.
Copy link to clipboard
Copied
You haven't really said what specifically is going wrong, so we can just ask questions.
Did you populate the obj.holder variable with your holder local variable when you called addChild with the local holder? If not, the next time you call removeChild on obj.holder, obj.holder will contain whatever was in it before (just removing the object doesn't reassign the variable).
Note that I don't see where you did that. I also don't think that it could be anywhere else in the code (since the local holder only is in scope within duplicateMov). The only other place that you could be doing that is if you're watching for ADDED or ADDED_TO_STAGE on obj and you populate holder if you see a bitmap come on board.
Also note that if you add holder to obj then contains(obj.holder) will be false, because obj is what contains holder, not this.
Copy link to clipboard
Copied
What is going wrong is that I'm pretty new to the whole coding thing.
I'm trying to update a clone of some movieclip to some other movieclips. This works. The problem is that duplicateMov functions keeps adding movieclips to the stage instead of replacing them. I need to -remove and add "holder" - every time, except the first time I add it.
This is on stage:
function toggleLayerMenu(event:MouseEvent):void{
if(layerMenu.visible == true){
layerMenu.visible = false;
}else{
layerMenu.visible = true;
layerMenu.duplicateMov(LHolder.L1Holder, Box(layerMenu.getChildByName("layer1")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L2Holder, Box(layerMenu.getChildByName("layer2")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L3Holder, Box(layerMenu.getChildByName("layer3")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L4Holder, Box(layerMenu.getChildByName("layer4")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L5Holder, Box(layerMenu.getChildByName("layer5")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L6Holder, Box(layerMenu.getChildByName("layer6")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L7Holder, Box(layerMenu.getChildByName("layer7")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L8Holder, Box(layerMenu.getChildByName("layer8")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L9Holder, Box(layerMenu.getChildByName("layer9")).getChildByName("L0Dup"));
layerMenu.duplicateMov(LHolder.L10Holder,Box(layerMenu.getChildByName("layer10")).getChildByName("L0Dup"));
Copy link to clipboard
Copied
keeps adding movieclips to the stage
edit: keep adding new instances of "holder" to the movieclip.
Copy link to clipboard
Copied
Try something like
var holder:Bitmap = new Bitmap();
holder.bitmapData = bmd
obj.holder = holder;
in the Class you're using for obj:
protected var _holder:Bitmap;
public function get holder():Bitmap {
return _holder;
}
public function set holder(value:Bitmap):void {
if (value !== _holder) {
if (contains(_holder) {
removeChild(_holder);
}
_holder = value;
if (_holder) {
addChild(_holder);
}
}
}
This puts the responsibility for managing the children of obj firmly with obj. And since obj necessarily has the most knowledge about its own children, this is exactly who should have that responsibility.
Copy link to clipboard
Copied
I'll try and see what I can do. Thanks.
Copy link to clipboard
Copied
>> if (obj.holder != null && contains(obj.holder))
You can't do both tests at one like this. What if obj.holder is null - then contains will fail. You need to split it up:
if(obj.holder != null){
if(contains(obj.holder)){
Copy link to clipboard
Copied
If obj.holder is null, the contains condition won't even run. So it's entirely legitimate (if logically flawed, because holder would be contained by obj, not this) to use the syntax the OP proposed.
Copy link to clipboard
Copied
When dealing with bitmaps it is more efficient to reuse them - not create new instances. In your case just replacing BitmapData instance in the same Bitmap instance may be sufficient.
var holder:Bitmap = new Bitmap(null, "auto", true);
function duplicateMov(clone:MovieClip, obj:MovieClip):void
{
var bmd:BitmapData = new BitmapData(clone.width * SCALE, clone.height * SCALE, true, 0x00000000);
var scaleMatrix:Matrix = new Matrix();
scaleMatrix.scale(SCALE, SCALE);
bmd.draw(clone, scaleMatrix);
holder.bitmapData = bmd
obj.addChild(holder);
}
Copy link to clipboard
Copied
If you're going to that route, you should keep a BitmapData made from each MC you might want to clone, then reuse that.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now