Welcome, Guest. Please login or register.
Did you miss your activation email?
31 Jul 2010, 11:47:57 UTC
Forum home
+  flexdeveloper.eu Forum
|-+  Flex and ActionScript 3.0
| |-+  ActionScript 3.0 (Moderators: JMWhittaker, Jan K, thewarpedcoder, James)
| | |-+  add /remove image control to canvas
« previous next »
Pages: [1] Print
Author Topic: add /remove image control to canvas (Read 1670 times)
arjunender
Newbie FD
*
Posts: 13


« on: 19 Feb 2010, 13:10:11 UTC »

Hi,

i am able to add image control to canvas but how to remove it again

here is the my code for adding and removing

Code:
private function addchild(val:int):void{
image = new Image();
image.id = "pointer_img" + val
image.maintainAspectRatio = true;
image.width = 25
image.height = 25
image.x = 100
image.y = 50
image.load("skin/a.jpg")
    canvascontainer.addChild(image);
}

private removechild(val:int):void{
canvascontainer.removeChild( "pointer_img" + val)
}
this is the error displaying:

Quote
1067 : implicit coercion of a value of type string to an unrelated type flash.display:displayobject.

any suggestions

thanks
arjun
« Last Edit: 19 Feb 2010, 15:26:38 UTC by flexy » Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #1 on: 19 Feb 2010, 15:29:30 UTC »

This line won't work:
Code:
canvascontainer.removeChild( "pointer_img" + val)
You can't reference an image using its ID.

Instead you should use:
Code:
canvascontainer.removeChild( image )

If you are creating multiple images, push them to an Array, then you can hold a reference to each image in the Array for when you need to remove them.
Logged

arjunender
Newbie FD
*
Posts: 13


« Reply #2 on: 22 Feb 2010, 09:50:50 UTC »

thanks for reponding

I tried but failed

for adding in array:
pointerimg.push(cbtcontainer.addChild(image)) : working

for removing the child
cbtcontainer.removeChild(pointerimg); : not working

Quote
Error #2025: The supplied DisplayObject must be a child of the caller

thanks
arjun
« Last Edit: 22 Feb 2010, 11:34:33 UTC by flexy » Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #3 on: 22 Feb 2010, 11:33:14 UTC »

Nope, you're doing that wrong.

Create 10 Image objects and add them as a child of cbtcontainer.
Code:
var pointerimg:Array = [];

for( var i:int; i<10; ++i )
{
     var image:Image = new Image();
     cbtcontainer.addChild( image );
     pointerimg.push( image );
}

Remove the same 10 Image instances from cbtcontainer
Code:
for( var i:int; i<10; ++i )
{
      if( cbtcontainer.contains( pointerimg[ i ] ) )
      {
           cbtcontainer.removeChild( pointerimg[ i ] );
      }
}

Note that these images still exist in the pointerimg Array, so if you want to add them back to the display list, you need only iterate through the Array again calling the addChild() method.
Logged

fxdwebmaster
Newbie FD
*
Posts: 14


« Reply #4 on: 24 Feb 2010, 07:02:29 UTC »

Try this....


Code:
private function addchild(val:int):void{
image = new Image();
image.name = "pointer_img" + val;
image.maintainAspectRatio = true;
image.width = 25
image.height = 25
image.x = 100
image.y = 50
image.load("skin/a.jpg")
    canvascontainer.addChild(image);
}

private removechild(val:int):void{
var disp:DisplayObject=canvascontainer.getChildByName( "pointer_img" + val);
canvascontainer.removeChild(disp)
}

Hi,

i am able to add image control to canvas but how to remove it again

here is the my code for adding and removing

Code:
private function addchild(val:int):void{
image = new Image();
image.id = "pointer_img" + val
image.maintainAspectRatio = true;
image.width = 25
image.height = 25
image.x = 100
image.y = 50
image.load("skin/a.jpg")
    canvascontainer.addChild(image);
}

private removechild(val:int):void{
canvascontainer.removeChild( "pointer_img" + val)
}
this is the error displaying:

Quote
1067 : implicit coercion of a value of type string to an unrelated type flash.display:displayobject.

any suggestions

thanks
arjun
« Last Edit: 24 Feb 2010, 09:56:19 UTC by flexy » Logged
arjunender
Newbie FD
*
Posts: 13


« Reply #5 on: 02 Mar 2010, 12:31:45 UTC »

hi,

i tried still not able to remove image

it is entering into the if statement but not removing the image and no error also


Code:
if( cbtcontainer.contains( pointerimg[ i ] ) )
      {
Alert.show(i)
           cbtcontainer.removeChild( pointerimg[ i ] );
      }

Thanks
« Last Edit: 02 Mar 2010, 13:50:40 UTC by flexy » Logged
Pages: [1] Print
« previous next »
Share this on: Twitter Twitter Del.icio.us del.icio.us Digg Digg
Jump to:

©2006-2010 Flexdeveloper.eu/Jodie O'Rourke. All rights reserved.
Adobe®, Adobe® Flash™, Adobe® AIR™ and Adobe® Flex™ are registered trademarks of Adobe Systems Incorporated in the United States and other countries. All rights reserved.

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC