How to pass arguments to stage instances in ActionScript 3?
I have an instance on my stage that I dragged from my library at design time. This instance links to a custom class who's constructor takes an argument.
package
{
import flash.display.MovieClip;
import flash.media.Sound;
public class PianoKey extends MovieClip
{
var note:Sound;
public function PianoKey(note:Sound)
{
this.note = note;
}
}
}
Obviously, trying to run the the code as is yields the following argument count error:
ArgumentError: Error #1063: Argument count mismatch on PianoKey(). Expected 1, got 0.
Is there any way to se开发者_JAVA技巧t arguments on an instance that has been manually dragged to the stage?
This may help you. Just little changes are required in Custom Class
package
{
import flash.display.MovieClip;
import flash.media.Sound;
public class PianoKey extends MovieClip
{
var note:Sound;
public function PianoKey(note:Sound=null)
{
if(note!=null)
{
this.note = note;
}
}
}
}
I think the only way to do this is by making a PianoKey
component. This will have component properties that can be set. They're a real pain to set up though.
Why not use a setter instead?
package { import flash.display.MovieClip; import flash.media.Sound; public class PianoKey extends MovieClip { var _note:Sound; public function PianoKey() { } public function set note(value:Sound) { this._note = value; } } }
精彩评论