0

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.

Drahcir
  • 11,772
  • 24
  • 86
  • 128
  • You are getting confused with the difference between construct(Author-time) and run time. Code in the standard way doesn't run during author time(dragging an item from library to stage). I would suggest you read up on how to create a component. – The_asMan Sep 26 '12 at 19:27
  • More specifically look into the author time component framework – The_asMan Sep 26 '12 at 19:32
  • @The_asMan: Thanks, but that's not the problem. I'm good with AS3, but not so good with the flash pro interface, I was just wondering if there was a way to set the construction variables somehow in the interface. The 2 answers below are correct - No way to do it. – Drahcir Sep 27 '12 at 09:38

2 Answers2

1

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()
  {
  }
}
Simon McArdle
  • 893
  • 6
  • 21
  • Thanks, could explain a little more please. What do you mean by "inside the symbol"? – Drahcir Sep 26 '12 at 11:49
  • A text field dragged to stage is accessible as an object. That text field is the "symbol". So, go to its properties, and set those variables. – Vesper Sep 26 '12 at 13:09
  • @Vesper: Yes, that's what I've been doing. I wanted to know if I could somehow set the construct parameters. – Drahcir Sep 26 '12 at 13:53
  • Apologies for taking so long to reply, busy day! If you have a symbol you're dragging from your 'Library' onto the stage in Flash the best way to assign these variables (placeholderText, displayAsPassword) would be to write a small class to go along side the symbol. I can provide more details if this is what you meant, I may not have fully understood before. – Simon McArdle Sep 26 '12 at 14:44
  • @Sim: No problem, I have a busy day my self. It's OK, I know what you mean and I think this was what I'd already done. I created my class and have the properties set in here, I was just wondering if there was a better way of doing it. – Drahcir Sep 26 '12 at 15:10
  • Handling variables (and all code for that matter) via .as files is the best way to do it in the long run, it would be quicker to add the variables to the time line and handle them that way but you lose a lot of control over your code by separating it like that. You can set the variables in the class when you declare them, see my answer for an example. – Simon McArdle Sep 26 '12 at 16:07
1

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.

Mark Knol
  • 9,663
  • 3
  • 29
  • 44
  • +1 - Thanks for pointing me to temple lib, my input field class was only short (60 lines) but you're right, maybe re-inventing the wheel. – Drahcir Sep 27 '12 at 09:50