Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-29-2009, 08:15 PM   #1
Striker9099
Senior Member
 
Join Date: Aug 2009
Location: Lebanon
Posts: 403
Default Adding image to stage dynamically

Hello there. I have an image (.jpg file) in the library and I want to instantiate it dynamically through timeline code and then add it to stage. But I do not want to drag it and drop it from the library to stage, I want to keep it in the library and add it to stage through code only... how can I do that?
Striker9099 is offline   Reply With Quote
Old 08-29-2009, 08:29 PM   #2
roadkillguy
Registered User
 
Join Date: Apr 2008
Location: UT
Posts: 33
Default

Right click it in the library and select linkage. Toggle "Export for Actionscript" and type whatever you want to call it in the Class Text Box. You then add it to the stage as such:

If you typed "myPicture" in the text box,
ActionScript Code:
addChild(new myPicture());
roadkillguy is offline   Reply With Quote
Old 08-29-2009, 08:50 PM   #3
Striker9099
Senior Member
 
Join Date: Aug 2009
Location: Lebanon
Posts: 403
Default

mmm I tried that... didn't work for some reason
Striker9099 is offline   Reply With Quote
Old 08-29-2009, 09:00 PM   #4
Striker9099
Senior Member
 
Join Date: Aug 2009
Location: Lebanon
Posts: 403
Default

I'm getting two errors:

1136: Incorrect number of arguments. Expected 2.
1067: Implicit coercion of a value of type Image1 to an unrelated type flash.displayisplayObject.
Striker9099 is offline   Reply With Quote
Old 08-29-2009, 10:05 PM   #5
kingundkong
kingundkong
 
kingundkong's Avatar
 
Join Date: Jul 2009
Posts: 320
Default

you are trying to put a bitmap on stage, not a movieClip, try this:
ActionScript Code:
var myBitmap:Bitmap = new Bitmap(new(getDefinitionByName("myPicture")) (0,0) ); addChild(myBitmap);
__________________
kingundkong is offline   Reply With Quote
Old 08-29-2009, 10:38 PM   #6
UncleNinja
I love noogies.
 
UncleNinja's Avatar
 
Join Date: Mar 2009
Location: Georgia, USA
Posts: 786
Default

Also, you can put a UILoader in there. Drag the UILoader component from the Components panel to the Library panel.
ActionScript Code:
import fl.containers.UILoader; var myUILoader:UILoader=new UILoader(); myUILoader.width=stage.stageWidth; myUILoader.height=stage.stageHeight; myUILoader.load(new URLRequest("http://uncleninja.hostzi.com/images/newAvatarLarge.png")); addChild(myUILoader);
What this code will do is fill the stage up will a ginormous UILoader which will load my avatar.
Just so you know, my avatar is a PNG, but that UILoader could load a JPEG just as easy.
__________________
-UncleNinja on Twitter

ActionScript Hints - a revolutionary wiki dedicated to helping Flash developers

Last edited by UncleNinja; 08-29-2009 at 10:43 PM.
UncleNinja is offline   Reply With Quote
Old 08-29-2009, 11:16 PM   #7
Striker9099
Senior Member
 
Join Date: Aug 2009
Location: Lebanon
Posts: 403
Default

Quote:
Originally Posted by kingundkong View Post
you are trying to put a bitmap on stage, not a movieClip, try this:
ActionScript Code:
var myBitmap:Bitmap = new Bitmap(new(getDefinitionByName("myPicture")) (0,0) ); addChild(myBitmap);
This actually worked! but adding the image to the top of the display list is covering all the contents, how do I add it right at the bottom? I tried using addChildAt() but I do not know what index number I should use... so any ideas?
Striker9099 is offline   Reply With Quote
Old 08-29-2009, 11:55 PM   #8
Striker9099
Senior Member
 
Join Date: Aug 2009
Location: Lebanon
Posts: 403
Default

oh nevermind, I checked out Learning Actionscript 3.0 ebook and figured it out. Using addChildAt(myObject, 0) always adds the new object to the bottom of the display list
Striker9099 is offline   Reply With Quote
Old 08-30-2009, 12:49 AM   #9
Striker9099
Senior Member
 
Join Date: Aug 2009
Location: Lebanon
Posts: 403
Default

Ok here's my code so far:

Code:
function swapImages(incomingImage:Sprite, currentImage:Sprite):void {
    TweenMax.to(incomingImage, 2, { alpha:1 } );
    TweenMax.to(currentImage, 2, { alpha:0, onCompleteListener:tweenFinished } );
}

function tweenFinished(e:Event):void {
    removeChild(Sprite(e.target));
	currentImage = incomingImage;
}

var imageArray:Array = [
     {thumb:bottom_mc.thumb1, image:image1},
     {thumb:bottom_mc.thumb2, image:image2},
     {thumb:bottom_mc.thumb3, image:image3},
     {thumb:bottom_mc.thumb4, image:image4},
     {thumb:bottom_mc.thumb5, image:image5}
];

var currentImage:Sprite = image1;
var incomingImage:Sprite;

for(var i = 0; i < imageArray.length; i++){
     imageArray[i].thumb.name = "thumb_" + i;
     imageArray[i].thumb.addEventListener(MouseEvent.CLICK, onThumbClick);
}
 
function onThumbClick(e:MouseEvent):void{
     incomingImage = imageArray[e.currentTarget.name.split("_")[1]].image;
     // add it to display list behind current image
     addChildAt(incomingImage, getChildIndex(currentImage));
     swapImages(incomingImage, currentImage);
}
I keep getting this error:
1067: Implicit coercion of a value of type Class to an unrelated type flash.display:Sprite.
Source: var currentImage:Sprite = image1;

How can I solve this?
Striker9099 is offline   Reply With Quote
Old 08-30-2009, 01:07 AM   #10
UncleNinja
I love noogies.
 
UncleNinja's Avatar
 
Join Date: Mar 2009
Location: Georgia, USA
Posts: 786
Default

What is image1?
Is it an MC exported for actionscript?
__________________
-UncleNinja on Twitter

ActionScript Hints - a revolutionary wiki dedicated to helping Flash developers
UncleNinja is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:40 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.