I have a Class called RenderingImage which is a bitmap like so:
[Embed(source="images/thumb.png")]
var RenderingImage:Class;
What I am trying to do is change the bitmap image in a loop.
for (var i:String in homes)
{
var renderingImg:Bitmap = new RenderingImage();
renderingImg.y = 50 + spaceY;
renderingImg.x = 0 + spaceX;
renderingImg.width = 320;
renderingImg.height = 185;
home.addChild(renderingImg);
}
Is there anyway to change the source that I put when defining the RenderingImage Class inside this loop?
You may be better off not embedding your image assets in your project. Regardless of how you store the images, you simply need to point to the new image when looping.
var images:Array = [
"https://cdn1.iconfinder.com/data/icons/freeline/32/home_house_real_estate-64.png",
"https://cdn2.iconfinder.com/data/icons/cute-tech-icon-set-1/512/Home-64.png",
"https://cdn3.iconfinder.com/data/icons/watchify-v1-0-80px/80/home-80px-64.png"
]
for (var i:int = 0; i < images.length; i++) {
var house:Loader = new Loader();
house.load(new URLRequest(images[i]));
addChild(house)
house.x = 65*i;
}
Alternatively, if you're heartset on only using explicit Bitmap objects, you could reference the loaded BitmapData after loading.
import flash.events.Event;
import flash.display.Loader;
import flash.display.Bitmap;
var images:Array = [
"https://cdn1.iconfinder.com/data/icons/freeline/32/home_house_real_estate-64.png",
"https://cdn2.iconfinder.com/data/icons/cute-tech-icon-set-1/512/Home-64.png",
"https://cdn3.iconfinder.com/data/icons/watchify-v1-0-80px/80/home-80px-64.png"
]
var bitmaps:Array = [];
var loadedCount:int = 0;
for (var i:int = 0; i < images.length; i++) {
var loader:Loader = new Loader();
loader.load(new URLRequest(images[i]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}
function loaded(e:Event):void {
bitmaps.push(e.currentTarget.loader.content);
loadedCount++;
if (loadedCount == images.length) {
for (var i:int = 0; i < images.length; i++) {
var houseBmp:Bitmap = new Bitmap();
houseBmp.bitmapData = bitmaps[i].bitmapData;
houseBmp.x = 65*i;
addChild(houseBmp);
}
}
}
Related
I have a drag and drop project where everytime I click on a movieclip a clone is made that can be dragged. I would like to know how to make a button that can reset/remove the clones when the button is pressed.
This is what I got so far:
import flash.display.MovieClip;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
addChild(latestClone);
latestClone.startDrag();
latestClone.addEventListener(MouseEvent.MOUSE_DOWN, latestClone.startDrag);
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if(latestClone != null){
latestClone.stopDrag();
}
}
Add all of your clones in an array, and loop over the array and do a removeChild on each item in the array. So:
var items:Array = new Array();
....
addChild(latestClone);
items.push(latestClone);
....
var resetButton:SimpleButton = new SimpleButton();
//set your button properties here
resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);
function onResetClicked(e:MouseEvent):void
{
reset();
}
function reset():void
{
for (var i:uint = 0; i < items.length; i ++)
{
removeChild(items[i]);
items[i] = null;
}
items = new Array();
}
Hope this helps.
just declare container variable like so:
var clone_container:Sprite = new Sprite();
put all clones inside,than u can clear it up very easely:
while(clone_container.numChildren > 0){
clone_container.removeChildAt(0);
}
that's all..
I found the solution to my question by testing several possibilities with the code that was posted + searching other questions.
This is the code that worked:
import flash.display.MovieClip;
import flash.events.MouseEvent;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
addChild(latestClone);
latestClone.startDrag();
latestClone.addEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
}
function onClonedPlusPressed(event:MouseEvent):void{
latestClone = MovieClip(event.currentTarget);
latestClone.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if(latestClone != null){
latestClone.stopDrag();
items.push(latestClone);
}
}
resetButton.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
}
var items:Array = new Array();
resetButton.addEventListener(MouseEvent.CLICK, onResetClicked);
addChild(resetButton);
function onResetClicked(e:MouseEvent):void
{
reset();
}
function reset():void
{
for (var i:uint = 0; i < items.length; i ++)
{ items[i].removeEventListener(MouseEvent.MOUSE_DOWN,onClonedPlusPressed);
if(items[i].parent) items[i].parent.removeChild(items[i]);
items[i] = null;
}
items = new Array()
}
you most add an array and push object into this,
your solution:
import flash.display.MovieClip;
import flash.events.Event;
var latestClone:MovieClip;
plus.addEventListener(MouseEvent.MOUSE_DOWN, onPlusPressed);
var plusArray:Array = new Array();
resetbtn.addEventListener(MouseEvent.CLICK,resetFunc);
function resetFunc(e:Event)
{
for (var i=0; i<plusArray.length; i++)
{
removeChild(plusArray[i]);
}
plusArray = new Array()
}
function onPlusPressed(event:MouseEvent):void
{
latestClone = new Plus();
latestClone.x = event.stageX;
latestClone.y = event.stageY;
plusArray.push(latestClone);
addChild(plusArray[plusArray.length-1]);
plusArray[plusArray.length - 1].startDrag();
plusArray[plusArray.length - 1].addEventListener(MouseEvent.MOUSE_DOWN, plusArray[plusArray.length-1].startDrag);
}
stage.addEventListener(MouseEvent.MOUSE_UP, onStageReleased);
function onStageReleased(event:MouseEvent):void
{
if (latestClone != null)
{
latestClone.stopDrag();
}
}
Good luck
What I am trying to do is add say 20 jpgs labelled 1-20 individually inside movieClips [ sReel ] , then add the movieClips to movieClip [ aReel ] and duplicate movieClip [ aReel ] into movieClip [ mReel ] so I can have a horizontal scrolling movieClip.
I can do all manually no problem but doing it automatically is not so easy :(
Just need how to stack the mocieClips correctly as know how to do rest oc code Thanks..
var movie_Number:Array = new Array();
var movie_Title: Array = new Array();
var movie_Director: Array = new Array();
var movie_Star: Array = new Array();
var movie_Duration: Array = new Array();
var movie_Genre: Array = new Array();
var movie_Year: Array = new Array();
var movie_Rating: Array = new Array();
var movie_Comment: Array = new Array();
var mReel:MovieClip = new MovieClip;
var aReel:MovieClip = new MovieClip;
var bReel:MovieClip = new MovieClip;
var sReel:MovieClip = new MovieClip;
var ldr:Loader;
newReelMask.cacheAsBitmap = true;
mReel.cacheAsBitmap = true;
mReel.mask = newReelMask;
var xmlLoader:URLLoader = new URLLoader(), xmlData:XML;
var movieNum:Number;
var xReel = 0;
mReel.addEventListener(MouseEvent.MOUSE_OVER, onStop);
mReel.addEventListener(MouseEvent.MOUSE_OUT, onStart);
var myTimer:Timer = new Timer(1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
init();
makeSmallReel();
aReel.x = 0;
aReel.y = 0;
mReel.addChild(aReel);
addChild(mReel);
function init():void
{
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("movieInfo.xml"));
}
function LoadXML(e:Event):void
{
xmlData = new XML(e.target.data);
var movieTitle:XMLList = xmlData..title;
var movieDirector:XMLList = xmlData..director;
var movieStar:XMLList = xmlData..star;
var movieDuration:XMLList = xmlData..duration;
var movieGenre:XMLList = xmlData..genre;
var movieYear:XMLList = xmlData..year;
var movieRating:XMLList = xmlData..rating;
var movieComment:XMLList = xmlData..comment;
var len: int = xmlData.movie.length();
var i:int = 0;
for (i; i < len; i++)
{
movie_Number.push(i);
movie_Title.push(movieTitle[i]);
movie_Director.push(movieDirector[i]);
movie_Star.push(movieStar[i]);
movie_Duration.push(movieDuration[i]);
movie_Genre.push(movieGenre[i]);
movie_Year.push(movieYear[i]);
movie_Rating.push(movieRating[i]);
movie_Comment.push(movieComment[i]);
}
}
function makeSmallReel():void
{
var i:int = 1;
for (i; i < 11; i++)
{
ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest("images/thumb/"+i+".jpg"));
}
}
function ldr_complete(e:Event):void
{
sReel.x = xReel;
sReel.addChild(ldr);
xReel = xReel + e.target.width;
aReel.addChild(sReel);
}
function timerListener(e:TimerEvent):void
{
if (mReel.x <= -(aReel.width))
{
mReel.x = 0;
}
mReel.x -= 1;
}
function onStop(e:MouseEvent):void
{
myTimer.stop();
}
function onStart(e:MouseEvent):void
{
myTimer.start();
}
You 're adding always the same loader(ldr is a class-var) to your container.
At loading you have to create a ldr vor each picture. And add this to your container in your complete-handler. (it's e.target in your complete-handler)
BTW: You don't need a MovieCLip. A Sprite is enought, 'cause semms, you never need the timeline of a movieclip, which is the difference between MovieClip and Sprite ( and the overhead).
I've created an Array with the names of my image-Classes in the library.
var myArr:Array = new Array("myPic1","myPic2","myPic3");
Normally if I would like to create an object of my class I would
var libraryImage:Bitmap;
libraryImage = new Bitmap(new myPic1(0,0));
But how can I iterate through the array and create my images dynamically?
for(var i:uint = 0; i<myArr.length; i++){
var libraryImage:Bitmap;
libraryImage = new Bitmap(new myArr[i](0,0));
}
doesn't works
Try this:
var myArr:Array = new Array(new myPic1(0,0), new myPic2(0,0), new myPic3(0,0));
for(var i:uint = 0; i<myArr.length; i++){
var libraryImage:Bitmap;
libraryImage = new Bitmap(myArr[i]);
}
You can do something like this
for(var i:uint = 0; i<myArr.length; i++){
var myClass:Class = getDefinitionByName(myArr[i]) as Class;
var libraryImage:Bitmap;
libraryImage = new Bitmap(new myClass(0,0));
}
In my library I have a bunch of classes named tip1, tip2, tip3, tip4...and so on. Is it possible to create one instance of each on the stage using a for loop? I tried this but it didn't seem to work.
var tips:int = 12;
for(var i:int = 1; i<=tips; i++){
var tipName:String = "tip"+i
var tip:MovieClip = new tipName();
tip.name = "tip" + i
tip.x = stage.width;
tip.y = 0;
addChild(tip);
}
Any help would be appreciated. Thanks!
You were missing the "getDefinitionByName" part.
// Up top
import flash.utils.getDefinitionByName;
// Down below
var tips:int = 12;
for (var i:int = 1; i < tips; ++i ) {
var myClass:Class = getDefinitionByName('tip' + i) as Class;
var tip:Object = new myClass();
tip.name = "tip" + i;
....
}
Instead of
var tip:MovieClip = new tipName();
Try (written from memory)
var clazz:Class = getDefinitionByName(tipName) as Class;
var tip:MovieClip = new clazz();
Also, you generally want to use stage.stageWidth instead of stage.width, since the latter will return the stage bounding box width (which might not be the same as the area the swf file covers).
Hello I'm trying to do this (in as2 this worked but not in as3) I looked on google for 3 hours, but still don't found a solution (thans for your help) :
import flash.display.MovieClip;
var mcContainer:MovieClip = new MovieClip();
var mcImage0:MovieClip = new MovieClip();
var mcImage1:MovieClip = new MovieClip();
var mcImage2:MovieClip = new MovieClip();
var mcImage3:MovieClip = new MovieClip();
mcImage0.name = "Boy";
mcImage1.name = "Girl";
mcImage2.name = "Woman";
mcImage3.name = "Man";
var ArrayNamesOfMC:Array = new Array();
var i:int = 4;
while(i--) {
ArrayNamesOfMC.push(["mcImage"+i].name);
}
This donsn't work :
ArrayNamesOfMC.push(["mcImage"+i].name);
This is the simple answer to your question:
var mcImage0:MovieClip = new MovieClip();
var mcImage1:MovieClip = new MovieClip();
var mcImage2:MovieClip = new MovieClip();
var mcImage3:MovieClip = new MovieClip();
mcImage0.name = "Boy";
mcImage1.name = "Girl";
mcImage2.name = "Woman";
mcImage3.name = "Man";
var ArrayNamesOfMC:Array = new Array();
var i:int = 3;
while (i >= 0)
{
ArrayNamesOfMC.push(MovieClip(this["mcImage" + i]).name);
i--;
}// end while
The following may not be relevant in your case as I'm not quite sure what the purpose of your application is, but this is probably a better approach:
var sprites:Vector.<Sprite> = new Vector.<Sprite>();
var names:Vector.<String> = new <String>["Boy", "Girl", "Woman", "Man"];
for (var i:uint = 0; i < names.length; i++)
{
var sprite:Sprite = new Sprite();
sprite.name = names[i];
sprites.push(sprite);
}// end for
Disregard this if it is not applicable in your case.
this should do the trick:
var _movieClip:MovieClip = ("mcImage" + i) as MovieClip;
ArrayNamesOfMC.push(_movieClip.name);
Taurayi's answer is an interesting technique that I didn't know about.
Personally I would recommend restructuring your code to put all the movieclips in an array, like so:
var mcImages:Array = new Array();
for (var i = 0; i < 4; i++) {
mcImages.push(new MovieClip);
}
mcImages[0].name = "Boy";
trace(mcImages[0].name);
Incidentally, your while loop was constructed incorrectly. You need a condition in the parentheses and then do the decrement inside the loop. But with all your movieclips in an array then you can use this much simpler approach to loop through all of them:
for each (var mc in mcImages) {
trace(mc.name);
}