0

I have a function in my flash AS3 file. I'd like to pass a parameter when calling this function and have that parameter become the name of a new FLV playback component I'm declaring;

function newVideo(myVideoName){
var [myVideoName]:FLVPlayback = new FLVPlayback();
}

-- edit - I now have the following code. How would I add the FLV playback component to the stage?

> function newVideo(myVideoName:String):FLVPlayback {
>    
>     var flvP:FLVPlayback = new FLVPlayback();
>     flvP.name = myVideoName;
      flvP.source = "argentinaTrailer.f4v"; 
>     return flvP;
   }
> 
>  var myPlayer:FLVPlayback = newVideo('player1');

1 Answer 1

1

What you are doing here is not allowed. The notation for creating and accessing objects using a string reference (this['someObject']) does not permit you to cast a type. Furthermore, objects created in this fashion cannot then be directly accessed using that name without using the identifier notation:

this['foo']:Object = {}; // fails, 1078: Label must be a simple identifier.
this['foo']        = {}; // works

and

this['foo'] = {};
trace(this.foo);    // fails, 1120: Access of undefined property foo.
trace(this['foo']); // works, [object Object]

So, to make your function work it should be written:

function newVideo(myVideoName:String):void
{
    this[myVideoName] = new FLVPlayback();
}

but you will only then be able to access the player by using that same string reference, like this:

this[myVideoName].play();

Furthermore, none of the above is giving the instance of the FLVPlayback component a name. What it is doing is defining the name of a reference to your FLVPlayback. If you are intending to create an FLVPlayback that has an instance name of myVideoName then you should create a function that looks like this:

function newVideo(myVideoName:String):FLVPlayback
{
    var flvP:FLVPlayback = new FLVPlayback();
    flvP.name = myVideoName;
    return flvP;
}

What this does is creates a new instance of the FLVPlayback component, assigns it an instance name, and returns a reference to it. You would use it like this:

var myPlayer:FLVPlayback = newVideo('player1');
addChild(myPlayer);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for that - it makes sense. However, I've previously been adding the FLVPlayback component to the stage using addChild. Could you explain how I would incorporate this into your function as it doesn't seem to be working?
I'll be happy to edit the answer with more info, but could you explain what you mean by 'not working'?
Hi Shane, I've edited my question with the code I now have thanks to your answer. I've set a source parameter - how would I now add the FLV playback component to the stage?
@user469453 - I've updated my answer, if that's not working for you I'll need some details of the type of error you're getting.
Hi Shane - thats really helped. Thanks a lot - this will save me a lot of time and lines of code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.