如何在 AS3 中创建可重用的切换按钮?
我正在尝试使下面的代码可重用。我的 Flash 项目中需要多个切换按钮。现在,下面的代码可以在一个按钮上运行。如果我继续创建更多按钮,并遵循下面的格式,我将需要为每个按钮创建单独的功能。
我想将可重用代码放在单独的 ActionScript 文件中,而不是放在 FLA 文件中。我试图将 rolloverToggle、rolloverToggle 和toggleClick 放入我正在制作的类中。
// ///////////////////////////////////////////////////////////////////////
// ------- Need to make this code reusable -------
// ///////////////////////////////////////////////////////////////////////
// code on Frame 1
toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);
toggleButton.buttonState = "off";
// function rolloverToggle
function rolloverToggle(event:MouseEvent) {
toggleButton.gotoAndStop(toggleButton.buttonState+" over");
}
// function rolloutToggle
function rolloutToggle(event:MouseEvent) {
toggleButton.gotoAndStop(toggleButton.buttonState);
}
// function toggleClick
function toggleClick(event:MouseEvent) {
if (toggleButton.buttonState == "on") {
toggleButton.buttonState = "off";
toggleButton.gotoAndStop(1);
} else {
toggleButton.buttonState = "on";
}
}
I'm trying to make the code below reusable. I need multiple toggle buttons in my flash project. Right now the code below works on one button. If I continue and create more buttons, and follow the format below, I would need to create separate functions for each button.
I would like to put the reusable code in a separate ActionScript file and not in the FLA file. I am trying to put the rolloverToggle, rolloverToggle, and toggleClick in a class that I'm making.
// ///////////////////////////////////////////////////////////////////////
// ------- Need to make this code reusable -------
// ///////////////////////////////////////////////////////////////////////
// code on Frame 1
toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);
toggleButton.buttonState = "off";
// function rolloverToggle
function rolloverToggle(event:MouseEvent) {
toggleButton.gotoAndStop(toggleButton.buttonState+" over");
}
// function rolloutToggle
function rolloutToggle(event:MouseEvent) {
toggleButton.gotoAndStop(toggleButton.buttonState);
}
// function toggleClick
function toggleClick(event:MouseEvent) {
if (toggleButton.buttonState == "on") {
toggleButton.buttonState = "off";
toggleButton.gotoAndStop(1);
} else {
toggleButton.buttonState = "on";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很简单。创建一个新的通用按钮类,并在其中添加所有事件侦听器。对于要创建的每个新按钮,只需扩展通用按钮并在事件侦听器中填写所需的代码。:
现在只需扩展该类并添加您的功能即可。
This is quite Simple. Create a new generic button class, and add all of your event listeners within. For each new button you want to create, just extend your generic button and fill in the required code within the event listeners.:
Now just extend that class and add your functionality.