Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-15-2012, 09:25 PM   #1
chrisopia
bleurg;
 
Join Date: Nov 2005
Posts: 344
Default Movieclip Classes to add movieclip to the stage

If I want to add a movieClip to the stage, do I really have to create a class for it?
It's the only method I've got to actually add a movieclip to the stage, but I'm so new to classes I can't get any functionality out of it.

Take for instance- Manually- you place a movieclip on the stage, click on it's properties and give it an instance name... lets say "box_mc"... then throughout the rest of the script you refer to "box_mc" to do anything with it. e.g. box_mc.x = 100;

Using classes doesn't give you an option to give an instance name to each added movieClip. I've got as far as ".name" but it doesn't do anything in the same away as an instance name does?

ActionScript Code:
var B:MovieClip = new Box();     B.name = "box_mc";     stage.addChild(B);

I want "box_mc" to become the instance name - so I can say "box_mc.x = 100;" outside of the function or anywhere in the script and refer to that one instance. But the .name property does no such thing. It want's me to use "B.x" But B is the movieClip name - not it's instance.

Eventually "box_mc" will be replaced with a string variable - filled in through a function:

ActionScript Code:
function Box(box:string){ var B:MovieClip = new Box();     B.name = box;     stage.addChild(B); } Box("box_mc"); Box("circle_mc"); Box("triangle_mc"); box_mc.x = 100; circle_mc.x = 200; triangle_mc.x = 300;

But it won't recognise box_mc, or circle_mc or triangle_mc... only B!
Instead you get error: 1120: Access of undefined property.


Just out of interest - heres my class:
ActionScript Code:
package com.BoxMaker {     import flash.display.MovieClip;     public class person extends MovieClip     {         public function Box()         {         }     } }
chrisopia is offline   Reply With Quote
Old 10-15-2012, 10:16 PM   #2
iamgotenks
Super Saiyan
 
iamgotenks's Avatar
 
Join Date: Nov 2011
Location: New Zealand
Posts: 345
Default

Well, if they are just MovieClips, then you don't have to make a class. Basically, the instance name is the same as the variable name you give it, for example, if I have a movieclip in my library called Box, then in the properties, I would have "export for action script" ticked, and then it should auto-fill in the class box with "Box", and the base class will be "flash.display.MovieClip". So, you wont have to make a class for your Box as flash will do it automatically.

Now, when you create a MovieClip and add it to the stage, you access it from the variable you created - not the name you give it.
ActionScript Code:
var myBox:MovieClip = new Box(); stage.addChild(myBox); //we have control over our box: myBox.x = 100; myBox.y = 150; myBox.alpha = 0.5; //50% transparency

Now you can do this with any MovieClip, and have many more than 1;
ActionScript Code:
var myBox:MovieClip = new Box(); var myCircle:MovieClip = new Circle(); stage.addChild(myBox); stage.addChild(myCircle); myBox.x = 100; myCircle.x = 150; myBox.y = 100; myCircle.y = 300;

The part that might get you is if you have something on the stage already, and you've given it an Instance Name inside the flash design area. Flash will (by default) automatically create your variable and name it. So, if I have a box on the stage and I've called it "myBox", then I would just do it similar to before:
ActionScript Code:
var myBox:MovieClip; //no need for new Box() because it's already created, we're just telling flash what it is so we can access it easily. myBox.x = 100; myBox.y = 15;

If you have a MovieClip inside a MovieClip, then it comes down to relationships. Lets say we have created a triangle movieclip called "myTriangle" inside a movieclip called "myCircle" and then put that into our "myBox", which is already on the stage.

ActionScript Code:
var myBox:MovieClip; //so flash knows myBox is a movieclip //because they're inside one another, if I move myBox, then all of it's children will move too (myCircle/myTriangle). myBox.x = 100; myBox.y = 100; //now we want to move the circle and triangle. Because they're children of myBox, they take the position of myBox and then move in relation to that point. myBox.myCircle.myTriangle.x = 50; //myBox.x (100) + myCircle.x (0) + myTriangle.x (50) = 150; myBox.myCircle.myTriangle.y = 50; //and we can move the circle too, which because triangle is a child of myCircle, then it moves in relation to that too myBox.myCircle.x = 75; myBox.myCircle.y = 250; //so the position on stage for "myTriangle" is now, myBox.x (100) + myCircle.x (75) + myTriangle.x (50) = 225

When you start having a lot of movieclips (lets say 50 myCircles's) then you just need to keep track of the variables which is another lesson for another day
iamgotenks is offline   Reply With Quote
Old 10-16-2012, 08:18 PM   #3
chrisopia
bleurg;
 
Join Date: Nov 2005
Posts: 344
Default

Thanks you thank you thank you iamgotenks! Thats certainly an easier way to add movieclips to the stage.

And in preliminary tests they work... the issue I'm facing is placing this inside a custom function and naming the MovieClip through a variable...

e.g
ActionScript Code:
AddBox("myBox"); function AddBox(TheName:String){     [TheName]:MovieClip = new Person();     stage.addChild(this[TheName]); NextFunction(); } NextFunction(){   myBox.x = 100; }

None of that works. I need to somehow convert the String into the name of the movieclip I'm adding to the stage!
chrisopia is offline   Reply With Quote
Old 10-16-2012, 08:43 PM   #4
iamgotenks
Super Saiyan
 
iamgotenks's Avatar
 
Join Date: Nov 2011
Location: New Zealand
Posts: 345
Default

I'm just having a little trouble seeing why you would be doing it like this. You don't have to name your movieclips at all. If it's so you can find specific movieclips after you've added it, then there is a different way to do that. If it's just so you can have a generic function set up your movieclip with generic details, then that also can be done without the need for naming them.
iamgotenks is offline   Reply With Quote
Old 10-16-2012, 08:51 PM   #5
iamgotenks
Super Saiyan
 
iamgotenks's Avatar
 
Join Date: Nov 2011
Location: New Zealand
Posts: 345
Default

I suppose, regardless of your use, you can do it this way:

ActionScript Code:
var myBoxes:Dictionary = new Dictionary(); createBox("myBox"); moveBox("myBox", 10); createBox("myCircle"); moveBox("myCircle",50); createBox("myTriangle"); moveBox("myTriangle", 150); function createBox(boxName:String):void {      var tempMovieClip:MovieClip = new Person();      tempMovieClip.name = boxName; //no need to do this            tempMovieClip.x = 50;      tempMovieClip.y = 150;      addChild(tempMovieClip);      myBoxes[boxName] = tempMovieClip; //store the movieclip reference here } function moveBox(boxName:String, amount:int):void {      var tempMovieClip:MovieClip = myBoxes[boxName];      if (tempMovieClip != null)      {             tempMovieClip.x += amount;      } }
iamgotenks is offline   Reply With Quote
Old 10-16-2012, 09:11 PM   #6
chrisopia
bleurg;
 
Join Date: Nov 2005
Posts: 344
Default

You are a genius!

i've never heard of the dictionary class before, but looking it up it's like the inequivalent of how arrays use numbers as index keys.

The only reason this is such a pain is because later there is a function that joins a line between 2 movieclips on the stage. the first movieclip was placed on the stage by the very same function that places the second one.

In a nut shell, here's what I have:
ActionScript Code:
AddPeople("GrandFather","GrandMother",["Wife","Aunt"]); AddPeople("Husband","Wife",["ChildA","ChildB"]); function AddPeople(H:String, W:String, C:Array){  //places moviceClips on the stage  DrawLines(H,W,C); } function DrawLines(H:MovieClip,W:MovieClip,K:MovieClip){  // list of graphic functions. it will need to recognise that "Wife" was placed on // stage by the first time the function ran - but as a child of GrandFather  // and GrandMother - but the second time "Wife" is now the ... wife! }

... in case you haven't guess it yet - it's a family tree maker... But this is just one project that would need this type of association!

Thank you once again for your help! Thank you!!
chrisopia 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 09:52 PM.

///
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.