-
How to add random movie clip from library to stage dynamically?
Hi,
How to add random movie clip to stage from many different movie clips in library?
If I wanted to add one movie clip to stage I would do liko so:
Code:
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
function onMove(e:MouseEvent):void {
var mc:MovieClip = new Ball();
mc.x = mouseX;
mc.y = mouseY;
addChild(mc);
}
Code above works perfect but I tried following but with no success, no errors, but nothing happens - no mc's are added to stage. I have 6 movie clips in library and they all are linked, exported for actionscript. On MouseOver I want to add random movie clip from those six movie clips to the stage. Am I going in wrong direction? Any thoughts how to achieve this?
Code:
var myArray:Array = [mc1, mc2, mc3, mc4, mc5, mc6];
stage.addEventListener(MouseEvent.MOUSE_OVER, onLoop);
function onLoop(e:MouseEvent):void {
for (var i:int = 0; i< myArray.length; i++) {
var randomMc:Number = Math.floor(Math.random()*i);
var mc:MovieClip = new myArray[randomMc];
addChild(mc);
mc.x = mouseX;
mc.y = mouseY;
}
}
Thanks
-
lemon juice hurts your eyes
The loop you use will generate 6 objects on the stage, and you only want to add 1 right?
Try this:
PHP Code:
var theClip:Object; stage.addEventListener(MouseEvent.MOUSE_OVER, onLoop);
function onLoop(e:MouseEvent):void { var random:uint = Math.round(Math.random() * 6); var randomClass:Class = Class(getDefinitionByName("mc" + random)); if(mainClass) // if it exists, so no errors { theClip = new randomClass(); addChild(theClip as DisplayObject); } }
EDIT: Oh, and be sure your library items extend to mc1, mc2, mc3, mc4, mc5 and mc6, because it will be looking for those exact names
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
Hi, your code returned error:
1120: Access of undefined property mainClass.
I took away if statement and then got no errors but it didn't work, then I replaced MouseEvent.MOUSE_OVER with MOUSE_MOVE and then your code worked, the only thing I could not figure out how to assign x and y properties of mouse to the movie clips:
Code:
var theClip:Object;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onLoop);
function onLoop(e:MouseEvent):void
{
var random:uint = Math.round(Math.random() * 6);
var randomClass:Class = Class(getDefinitionByName("Mc" + random));
theClip = new randomClass();
addChild(theClip as DisplayObject);
}
Meanwhile I fixed my code and now it works:
Code:
var myArray:Array = [Mc,Mc1,Mc2,Mc3,Mc4,Mc5];
stage.addEventListener(MouseEvent.MOUSE_MOVE, onLoop);
function onLoop(e:MouseEvent):void {
var randomMc:Number = Math.floor(Math.random()*myArray.length);
var mc:MovieClip = new myArray[randomMc];
addChild(mc);
mc.x = mouseX;
mc.y = mouseY;
}
Thank you!
-
lemon juice hurts your eyes
Yeah sorry, that should have been if(randomClass)
So it was probably because of the naming it didn't work, right? Because I see you've capitalized the m's in your Array.
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
I was testing this approach for my future project. I will create particle effect: on MOUSE_MOVE I want to add random latin alphabet letters (26 alltogether), so now I must figure out the way how to do this without creating by hand 26 movie clips and then link them from library....
Any thoughts on that?
Cheers
-
lemon juice hurts your eyes
Sure, just type in the alphabet in an Array and use the same method to make an instance of a Textfield, which you then put inside of a MovieClip (can be done in code) or an instance of a MovieClip in your library containing a dynamix textfield
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
Thanks dude, will try right away
P.S
Yeah sorry, that should have been if(randomClass)
So it was probably because of the naming it didn't work, right? Because I see you've capitalized the m's in your Array.
I just tried again your code again with if(randomClass) + MOUSE_MOVE instead of MOUSE_OVER and it works, also gave x and y properties to MC's, works perfect!
Code:
var theClip:Object;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onLoop);
function onLoop(e:MouseEvent):void
{
var random:uint = Math.round(Math.random() * 6);
var randomClass:Class = Class(getDefinitionByName("Mc" + random));
if(randomClass) // if it exists, so no errors
{
theClip = new randomClass();
addChild(theClip as DisplayObject);
theClip.x = mouseX;
theClip.y = mouseY;
}
}
thnks again!
-
lemon juice hurts your eyes
Also, see if adding e.updateAfterEvent(); at the end of the onLoop() function (just beneath the if()) gives you a more fluid particle effect
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
This worked perfectly for what I was looking for. Thanks to everyone!
I have one additional question regarding this method. I am working with creating movie clips on the stage. With every mouse click event, a new movie clip is created in a static position. But, as it is currently coded, a new movie clip is loaded directly on top of the previously loaded movie clip. So, with every click, I keep adding more and more on top of each other.
Now, I know I should probably use the removeChild(); command, but I'm not sure if I should be removing the array or theClip, as I have never worked with this "addChild as DisplayObject" method before.
So, what would I remove and where would I put that code?
My code is essentially exactly the same, but here it is, just so you get a better idea of what's happening:
Actionscript Code:
var front:Object; stage.addEventListener(MouseEvent.MOUSE_DOWN, onLoop);
function onLoop(e:MouseEvent):void { var random:uint = Math.round(Math.random() * 2) + 1; var randomClass:Class = Class(getDefinitionByName("fmc" + random)); if(randomClass) { front = new randomClass(); addChild(front as DisplayObject); front.x = 0; front.y = 0; } }
-
Hi all!
I am new to programming.
I want to ask?
How to add 26 movie clip from library to stage when press different key on the keyboard?
In other way how to place letters-movie clips from library on the stage when press associated key on keyboard?
Thank you
-
You are a king!!!
Thank you for introducing the "Class" type and the use of "getDefinitionByName".
I was looking for this solution and it's was not easy to find!
Originally Posted by florianvanthuyn
The loop you use will generate 6 objects on the stage, and you only want to add 1 right?
Try this:
PHP Code:
var theClip:Object; stage.addEventListener(MouseEvent.MOUSE_OVER, onLoop);
function onLoop(e:MouseEvent):void { var random:uint = Math.round(Math.random() * 6); var randomClass:Class = Class(getDefinitionByName("mc" + random)); if(mainClass) // if it exists, so no errors { theClip = new randomClass(); addChild(theClip as DisplayObject); } }
EDIT: Oh, and be sure your library items extend to mc1, mc2, mc3, mc4, mc5 and mc6, because it will be looking for those exact names
Last edited by avicbk; 06-20-2015 at 11:57 PM.
Reason: accidentally posted with SHIFT+ENTER before finish writing...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|