Drahcir
Drahcir

Reputation: 11972

as3, instantiate object on stage with parameters

If I have an object which extends MovieClip, for example let's say it's some kind of custom built text field class (InputField).

The InputField constructor has 2 parameters(placeholderText:String = null, displayAsPassword:Boolean = false).

If I drag the movie clip onto the stage, it will be constructed, but will not have the parameters set. Is there any way to set them at construct time?

To be clear, I want to be able to just drag this movie clip onto the stage, not create it and do addChild() from the code behind.

Upvotes: 0

Views: 413

Answers (2)

Mark Knol
Mark Knol

Reputation: 10143

I don't know if this is a good idea but you could create a class that extends your InputField. You can alter the constructor parameters inside the super function.

 public function CustomInputField() {
   super("hello", true); 
 }

However, I would go for a public function init(), with the same parameters as you now use in the constructor.

Btw are you reinventing the wheel? Inputfield are created a thousand times. Take a look at the temple library, which has pretty good extendable components and form classes.

Upvotes: 1

Simon McArdle
Simon McArdle

Reputation: 893

You can initialise the variables inside the symbol itself, they would then be set as soon as it was placed onto the stage. You can also still access these variables from your .as files.

EDIT: To extend on the comments, when you declare the two variables in the class you have set up just set them to whatever value you would like by default, for example:

class MyInputField
{
  // Will always set following variables to default values, 
  // whether using addChild or dragging symbol onto stage.
  var placeholderText:String = "Enter text here..";
  var displayAsPassword:Boolean = false;

  // If needed, store text field via constructor
  var tf:TextField;

  // Constructor
  function MyInputField()
  {
  }
}

Upvotes: 1

Related Questions