Actionscript 3 AS3-如何在类中获取当前场景名称
我在玩flash,我为菜单、按钮等创建了多个场景。当试图为一个场景中的按钮(而不是其他场景中的按钮)添加事件处理程序时,编译器抱怨说它不能引用不存在的对象 我认为解决办法很简单。。。获取场景名称,将其与if语句匹配,并通过if语句加载事件处理程序 然而,在网上挖了太长时间之后,我似乎找不到一个合适的方法。有人知道路吗 我尝试使用以下方法:Actionscript 3 AS3-如何在类中获取当前场景名称,actionscript-3,flash,Actionscript 3,Flash,我在玩flash,我为菜单、按钮等创建了多个场景。当试图为一个场景中的按钮(而不是其他场景中的按钮)添加事件处理程序时,编译器抱怨说它不能引用不存在的对象 我认为解决办法很简单。。。获取场景名称,将其与if语句匹配,并通过if语句加载事件处理程序 然而,在网上挖了太长时间之后,我似乎找不到一个合适的方法。有人知道路吗 我尝试使用以下方法: var scene:Scene = myflvandclassname.currentScene; var sName:String = MovieClip.
var scene:Scene = myflvandclassname.currentScene;
var sName:String = MovieClip.currentScene.name;
Both lead to an error "Access of possibly undefined property Scene through a reference with static type Class".
省略MovieClip和场景作为组织项目的源代码,并在时间轴上编写代码。用作入口点。这将有助于您掌握主要概念
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
public class StackOverflow extends Sprite {
public function StackOverflow() {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
setup();
}
private function setup():void {
const padding:int = 20;
//Initiate your UI components and place them in the display list
var menu:MyMenu = new MyMenu();
var buttons:MyFooterButtons = new MyFooterButtons();
var etc:AnotherComponent = new AnotherComponent();
addChild(menu);
addChild(buttons);
addChild(etc);
menu.x = menu.y = padding;
//Place them and initialise with concrete information
}
}
}
例如,MyMenu、MyFooterButtons和库中的另一个组件可以是MovieClip/Sprite,您在其中完成了放置、样式设置等所有工作我也遇到了同样的问题。我想从外部类中检查当前场景名称,并根据名称(游戏级别的名称)传递一些属性中的值…所以我所做的和它所起的作用就是这样
//main in 1st frame of the fla
stop();
var myCheckSceneClass: CheckSceneClass = new CheckSceneClass();
myCheckSceneClass.myCurrentScene = currentScene;
myCheckSceneClass.checkScene();
//CheckSceneClass
package {
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Scene;
public class CheckSceneClass extends flash.display.MovieClip {
public var myCurrentScene : Scene;
public function CheckSceneClass () {
}
public function checkScene() {
switch (myCurrentScene.name) {
case "Scene 1":
trace("It is the 1st Scene");
break;
default:
trace("Error with Scene Name");
break;
}
}
}
}
你试了什么?请包括代码、错误等。对于大多数情况,我一直在尝试将场景名称指定给字符串变量。错误主要是“通过静态类型类的引用访问可能未定义的属性场景”。我尝试过的东西=var sName:Scene=myflvandclassname.currentScene;,var sName:String=MovieClip.currentsecene.name以及类似的内容。请将其编辑到问题中,以便其他人更容易访问