PDA

View Full Version : help creating a reusuable function


fallenturtle
01-14-2011, 09:26 PM
I'm trying to create a function to streamline the process of doing this (which works):
var myCCmc_cc:myCCmc;
myCCmc_cc = new myCCmc();
ccHolder.addChild(myCCmc_cc);
myCCmc_cc.x = 370;
myCCmc_cc.y = 450;

myCCmc represents the names of a MovieClips in my movie library. Basically its a bunch of movieclips for onscreen captions that are suppose to appear at specific times in the movie. While I could add the above code to every keyframe to trigger the new movieclip to appear I thought it would be a good exercise in ActionScript 3 to create a function so that instead of having to replace "mcCCmc" multiple times with the appropriate movieclip name I could instead do something more streamlined like this:
showCC(mcCCmc,370,450);

The function I created looks like this:
function showCC(ccName:Object, ccX:Number = 0, ccY:Number = 0):void {
var ccName_cc:ccName;
ccName_cc = new ccName();
ccHolder.addChild(ccName_cc);
ccName_cc.x = ccX;
ccName_cc.y = ccY;
}

Unfortunately, I'm getting this error: 1046: Type was not found or was not a compile-time constant: ccName.

Where I'm confused is that I thought ccName was declared by the ccName:Object portion of my code, but I guess I need to import some classes or create a custom one? Any help would be appreciated.

Thanks.

the binary
01-14-2011, 09:38 PM
since you're creating a new instance of the "ccName" the variable should
be typed the same..

i guess it should be like this..


function showCC(target:MovieClip, ccX:Number = 0, ccY:Number = 0):void {
var ccName_cc: myCCmc = new ccName();
ccName_cc.x = ccX;
ccName_cc.y = ccY;
target.addChild(ccName_cc);
}

//
showCC( ccHolder, 370,450 );

fallenturtle
01-14-2011, 10:01 PM
Sorry, I'm not sure I understand or maybe I wasn't clear.

ccHolder is a previously created sprite that I'm using as a container for the captioning. It remains on stage throughout the whole movie. I did this to make it easier to remove old captions by just targeting ccHolder's children. mcCCmc is a placeholder for this example that represents the name of the various caption movieclips, so this value will change depending on what the current caption is suppose to be.

Basically my idea was that the first argument would replace ccName within the execution of the code.

the binary
01-16-2011, 01:28 PM
/**
* @ccName The name of the Class linked with the library symbol
* @ccX The x position
* @ccY The y position
*/
function showCC (ccName:String, ccX:Number = 0, ccY:Number = 0):void {

var instance: MovieClip = getDefinitionByName( ccName ) as MovieClip;

instance.x = ccX;
instance.y = ccY;

ccHolder.addChild( instance );
}


showCC( 'mcCCmc' );


is this one better.. ?

fallenturtle
01-16-2011, 09:57 PM
/**
* @ccName The name of the Class linked with the library symbol
* @ccX The x position
* @ccY The y position
*/
function showCC (ccName:String, ccX:Number = 0, ccY:Number = 0):void {

var instance: MovieClip = getDefinitionByName( ccName ) as MovieClip;

instance.x = ccX;
instance.y = ccY;

ccHolder.addChild( instance );
}


showCC( 'mcCCmc' );


is this one better.. ?

I'm getting some runtime errors now:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Speak_fla::MainTimeline/showCC()[Speak_fla.MainTimeline::frame5:36]
at Speak_fla::MainTimeline/frame5()[Speak_fla.MainTimeline::frame5:43]

For reference frame 5 is the frame the actionscript is on. Line 36 is: instance.x = ccX;
Line 43 is:
showCC('wellspeakup',0,0);

wellspeakup is the name of one of my moviescript's classes

I've never used getDefinitionByName. I take it this takes the place of new?

Thanks again for your help with this. It's great there are people like you willing to help us novices. ;)