在函数中将实例名称作为字符串传递
问题描述:
我正在创建一个Adobe Air Desktop项目,该项目在MainTimeline(RadioSel,CarMC1,CarMC2,CarMC3等)中有许多影片剪辑。 当你点击任何的CarMC
它显示RadioSel
(另一影片剪辑)在函数中将实例名称作为字符串传递
function showRadio(event: MouseEvent) {
RadioSel.visible = true;
RadioSel.instance = event.currentTarget.name;
trace (RadioSel.instance);
}
的CarMC
是具有多帧的影片剪辑。每个显示不同的形状,取决于RadioSel
的选择。 RadioSel
是一个具有多个单选按钮的影片剪辑,每个单选按钮都将CarMC
更改为不同的形状,并将名为instance
的变量作为字符串携带单击的CarMC
实例。
我在RadioSel
(在radiobuttongroup发生变化时调用)中创建了一个函数,它将点击的CarMC
更改为指定的帧并隐藏了RadioSel
。
function chooseCar(CarInstance: String, frame: Number) {
this["Object(root)."+CarInstance].gotoAndStop(frame);
this.visible = false;
//trace(event.target)
}
当我改变RadioSel
选择,我把这...
chooseCar(instance, frameNo)
...其中instance
是CarMC
的名称,frameNo
是由单选按钮定义了一些点击,但是,每次调用该函数时都会出现错误。我相信错误在这个部分:
this["Object(root)."+CarInstance].gotoAndStop(frame);
我该如何解决它?
答
您没有传递实例(movieclip),而是实例名称(字符串)。你应该只通过车上实例(动画片段),使事情变得更容易 - 那么你不关心你的车,以及如何访问它们:
function showRadio(event: MouseEvent)
{
RadioSel.visible = true;
// I assume the currentTarget is your car you've clicked on
// pass the movieclip instance to the radiosel and not just the name
RadioSel.instance = event.currentTarget as MovieClip;
trace (RadioSel.instance.name);
}
在你RadioSel:
public var instance:MovieClip;
// no need to pass the instance here as it is saved in the instance property anyway
function chooseCar(frame: Number)
{
instance.gotoAndStop(frame);
this.visible = false;
}
请包括您收到的错误。如果您切换“允许调试”并以*调试模式*(https://helpx.adobe.com/animate/using/debugging-actionscript-3-0.html)进行编译,您可以确切知道发生了什么行。 – Atriace