PDA

View Full Version : Duplicating Empty Movie Clips?!


gogojuice
06-14-2004, 10:51 AM
This might seam like a strange thing to do, but I'm just trying to get my head around createEmptyMovieClip, createMovieClip, createTextField and ActionScript in general.

What I want to do is this:-

Create An Empty Movie Clip
Create A Text Field in the New Movie Clip
Set Font and Style for newly created Movie Clip

Duplicate Movie Clip with Formated Text Field
Set Text in Duplicated Movie Clip

So far I've got this...

// First Create an Empty Movie Clip called Menu Item
_root.createEmptyMovieClip("menuItem", 1);

with (this.menuItem) {
// In the Empty Movie Clip create a text box
this.createTextField("txtBox", 0, 50, 50, 450, 100);

// Format the Text Box
txtFmt = new TextFormat();
// My Font is specified by the Font in the Library
txtFmt.font = "myFont";
txtFmt.size = 28;
txtFmt.color = "0x999999";
// now we apply the format...
txtBox.setNewTextFormat(txtFmt);

// Not sure yet what these values mean, got them from a tutorial, I'll look them up later
txtBox._quality("BEST");
txtBox.multiline = false;
txtBox.selectable = false;
txtBox.wordWrap = true;
txtBox.embedFonts = true;

// Don't really want this here, as the inital menuItem won't be used
txtBox.text = "Dummy Text";
}

// Duplicate Movie Clip
duplicateMovieClip( menuItem, "snaps", 2);

// Set text in Mov
_root.snaps.txtBox.text = "snaps";

From looking at this problem, I'm thinking that duplicateMovieClip is creating a copy of the Empty Movie Clip. Is this right? Logically I would expect to be able to create an Empty Movie Clip, add to the Movie Clip and duplicate the now modified movie clip.

Any help would be greatly appeciated

Tink
06-15-2004, 10:21 AM
the problem is this line// In the Empty Movie Clip create a text box
this.createTextField("txtBox", 0, 50, 50, 450, 100);using 'this' will create the textField inside the clip where the code resides (i believe its on your _root), therefore you are creating a textBox on the _root and not inside your new MovieClip.

try using// First Create an Empty Movie Clip called Menu Item
_root.createEmptyMovieClip("menuItem", 1);

//you don't need the 'with' statement
//with (this.menuItem) {
// In the Empty Movie Clip create a text box
menuItem.createTextField("txtBox", 0, 50, 50, 450, 100);

//then later in the code
menuItem.txtBox.setNewTextFormat(txtFmt);

// Duplicate Movie Clip
_root.duplicateMovieClip("menuItem", "snaps", 2);i hope its of some help

gogojuice
06-15-2004, 10:49 AM
Thanks Tink, that's exactly the line that I'm having trouble with. Since I wrote the original post, I've not got this code (fla attached also).

function createMenuItem( menuItem_mc,menuItemTxt, depth, x, y ) {

// First Create an Empty Movie Clip called Menu Item
_root.createEmptyMovieClip(menuItem_mc, depth);


// In the Empty Movie Clip create a text box
_root.menuItem_mc.createTextField("txtBox", depth, x, y, 450, 100);

// Format the Text Box
txtFmt = new TextFormat();
// My Font is specified by the Font in the Library
txtFmt.font = "myFont";
txtFmt.size = 28;
txtFmt.color = "0x999999";
// now we apply the format...
txtBox.setNewTextFormat(txtFmt);

// Not sure yet what these values mean, got them from a tutorial, I'll look them up later
txtBox._quality("BEST");
txtBox.multiline = false;
txtBox.selectable = false;
txtBox.wordWrap = true;
txtBox.embedFonts = true;

// Don't really want this here, as the inital menuItem won't be used
txtBox.text = menuItemTxt;

}

createMenuItem( "snaps_mc","snaps", 1, 10, 10 );
createMenuItem( "comingup_mc","Coming Up", 2, 30, 30 );
createMenuItem( "devil_mc","The Devil Made me do it", 3, 50, 50 );

I'm having trouble referencing the newly created Movie Clip. Doesn't seam to matter what I put before the createTextField box I only get three empty movie clips. No text box as a child of any of them.

Also I've taken out the "with ( mc )" part, as I realized that wasn't logically doing what I was expecting it to do.

So how do I reference the newly created Movie Clip?

Tink
06-15-2004, 11:02 AM
with the new code you are not trying to duplicate any movieClips, but you are running a function 3 times and passing different arguements each time so that a new clip is created each time, not duplicated.

the problem you still have is that you are not targeting the TextField correct each time. the TextField is inside your clip, so you must target the clip and then the TextField. also i don't believe you can target a clip in the way you are tryingfunction createMenuItem( menuItem_mc,menuItemTxt, depth, x, y ) {

// First Create an Empty Movie Clip called Menu Item
_root.createEmptyMovieClip(menuItem_mc, depth);

//this is changed as i don't believe you can target the clip the way you were before
//as flash will need to evaluate the string to find the targeted clip
// In the Empty Movie Clip create a text box
_root[menuItem_mc].createTextField("txtBox", depth, x, y, 450, 100);

// Format the Text Box
txtFmt = new TextFormat();
// My Font is specified by the Font in the Library
txtFmt.font = "myFont";
txtFmt.size = 28;
txtFmt.color = "0x999999";
//here again you need to target the clip, as the TextField is inside the clip
// now we apply the format...
_root[menuItem_mc].txtBox.setNewTextFormat(txtFmt);

// Not sure yet what these values mean, got them from a tutorial, I'll look them up later
_root[menuItem_mc].txtBox._quality("BEST");
_root[menuItem_mc].txtBox.multiline = false;
_root[menuItem_mc].txtBox.selectable = false;
_root[menuItem_mc].txtBox.wordWrap = true;
_root[menuItem_mc].txtBox.embedFonts = true;

//again you need to target the clip
// Don't really want this here, as the inital menuItem won't be used
_root[menuItem_mc].txtBox.text = menuItemTxt;

}

gogojuice
06-15-2004, 11:18 AM
Thanks. I've been playing with Flash for ages, and being a programmer, I get really frustrated with the interface. It never seams to do what I want it to do??!!

I knew I wasn't referencing the text filed properly, but I just couldn't figure out how to do it. ActionScript has changed it's syntax so many times over the release, that it's really confusing.

Again thanks. I think I need to now create a new post on how to use onEnterFrame properly. I'll have a play first and see if I can work it out.

Tink
06-15-2004, 11:28 AM
not being a programmer, i love the flash interface

onEnterFrame will basically run the code that is within the function on every frame. you can also specify the clip that the onEnterFrame is being run on//to run an onEnterFrame on the _root

var count = 0;

_root.onEnterFrame = function () {
trace (count);
if (count == 30) {
delete this.onEnterFrame;
}
count++;
}

//to run it on a clip with the instance name 'dog' that is inside a clip with the instance name 'pet', which is on the _root

var count = 0;

_root.pet.dog.onEnterFrame = function () {
trace (_root.count);
if (_root.count == 30) {
delete this.onEnterFrame;
}
_root.count++;
}

gogojuice
06-15-2004, 12:23 PM
Still having trouble passing referencing movieClip. I'm now moving on. Create the movieclips and they are on the stage. Now I want to do things to the txt when the mouse rolls over the them. I can do this if I write everything out long hand. When I create a function to do the same thing, nothing happens. I think it's because I'm not referencing the MovieClips properly again. Or it maybe how I'm referencing the function??


function createMenuItem( menuItem_mc,menuItemTxt, depth, x, y ) {

// First Create an Empty Movie Clip called Menu Item
_root.createEmptyMovieClip(menuItem_mc, depth);

// In the Empty Movie Clip create a text box
_root[menuItem_mc].createTextField("txtBox", depth, x, y, 450, 100);

_root[menuItem_mc].txtBox.text = menuItemTxt;


// Not sure yet what these values mean, got them from a tutorial, I'll look them up later
txtBox._quality("BEST");
txtBox.multiline = false;
txtBox.selectable = false;
txtBox.wordWrap = true;
txtBox.embedFonts = true;


// Format the Text Box
txtFmt = new TextFormat();
// My Font is specified by the Font in the Library
txtFmt.font = "myFont";
txtFmt.size = 45;
txtFmt.color = "0x999999";
// now we apply the format...
txtBox.setTextFormat(txtFmt);

}


// Functions to Move the Text, I want to use these function
function moveTxt ( mc ) {
this[mc]._x += 1;
trace( 'doing stuff' );
}

function stopTxt ( mc ) {
delete this[mc].onEnterFrame;
}


// Create the text Movie Clips
createMenuItem( "snaps_mc","snaps", 1, 10, 10 );
createMenuItem( "comingup_mc","Coming Up", 2, 30, 30 );
createMenuItem( "devil_mc","The Devil Made me do it", 3, 50, 50 );


// This way works, but I want to use the two functions above Instead of having to write this every time
_root.comingup_mc.onRollOver = function () {
_root.comingup_mc.onEnterFrame = function () {
_root.comingup_mc._x += 1;
}
trace ('comingup');
}

_root.comingup_mc.onRollOut = function () {
delete _root.comingup_mc.onEnterFrame;
}


// Using the Fuctions it works. This way it doesn't? HOW DO I REFERENCE THE MOVIE CLIP?
_root.snaps_mc.onRollOver = moveTxt ( _root[snaps_mc] );

_root.snaps_mc.onRollOut = stopTxt ( _root[snaps_mc] );

Tink
06-15-2004, 12:41 PM
function moveTxt ( mc ) {
//you need to run an enterFrame on the clip so that it does it every frame
//if not it will only run once
mc.onEnterFrame = function () {
this._x += 1;
trace(this + " is doing stuff);
}
}

function stopTxt ( mc ) {
delete mc.onEnterFrame;
trace(this + " has stopped doing stuff);
}


_root.snaps_mc.onRollOver = function () {
trace (this);
// you could target '_root.snaps_mc' here, but 'this' makes it more re-usable
moveTxt (this);
}

_root.snaps_mc.onRollOut = function () {
trace (this);
stopTxt ( this );
}that should work

gogojuice
06-16-2004, 08:24 AM
Yea that worked Tink. Now though I've got more questions. I shall create a new post though as it is a different subject