AS3把功能等等(){左右code产生于code线的错误我没有 [英] AS3 Putting function blah(){ around code generates errors on lines of code I dont have
问题描述
我已经得到了code,它在做什么,我希望有一个块 - 它产生MC的网格
当我把类似的功能等等()周围,它开始产生错误指示code线我没有如
类型错误:错误#1010:A项是不确定的,并没有房源 - 在flightCellMaker_fla :: MainTimeline / myXMLtrace()[flightCellMaker_fla.MainTimeline ::帧1:87]
在flightCellMaker_fla :: MainTimeline / processFPBxml()[flightCellMaker_fla.MainTimeline ::帧1:52]
在flash.events::EventDispatcher/dispatchEventFunction()
在flash.events::EventDispatcher/dispatchEvent()
在flash.net::URLLoader/onComplete()
当我把函数出来,但它确实是我想要做的事。怎么了呢?
VAR testXML:XML;
VAR myFPBxml:XML;
//初始化一个的URLLoader来从XML文件中的XML数据
VAR myFPBLoader:的URLLoader =新的URLLoader();
myFPBLoader.load(新的URLRequest(flightPlannerBoard.xml));
//检查XML数据满载
myFPBLoader.addEventListener(引发Event.COMPLETE,processFPBxml);
//一旦飞行板规划数据加载时,将其保存到一个变量
功能processFPBxml(五:事件):无效{
myFPBxml = XML(e.target.data);
myXMLtrace();
}
//抓住完成了XML数据负载,使其可在别处
功能myXMLtrace(){
testXML = XML(myFPBxml);
}
跟踪(***********************+ testXML *。);这将引发(不在myXMLtrace寿)错误
好了,所以这里的code,将它自己的正常运行而不是在一个函数的其余部分:
//创建并把所有的飞行单元规划和拖放
//设置2个循环:J代表列和i行
对于(VAR记者:数量= 0; J<的rowNum; J ++){
对于(VAR我:数量= 0; I< 9;我++){
//创建flightCell的板栅副本
VAR my_mc中=新flightCell();
my_mc.name =MC+ I + J;
的addChild(my_mc中);
所有子对象//设置事件监听器
my_mc.myDragShape.addEventListener(的MouseEvent.MOUSE_OVER,fl_MouseOverHandler);
my_mc.myDragShape.addEventListener(的MouseEvent.MOUSE_OUT,fl_MouseOutHandler);
my_mc.myDragShape.addEventListener(的MouseEvent.MOUSE_DOWN,fl_MouseDownHandler);
my_mc.myDragShape.addEventListener(侦听MouseEvent.MOUSE_UP,fl_MouseUpHandler);
对象(this).my_mc.yellowHiLite.visible = FALSE;
// cellPos [J] = myXML.cellPosX [J]。
//trace(stage.myXML.*);
//trace(my_mc.name);
my_mc.x =(100 +列表ColWidth);
my_mc.y = myRowHeight;
列表ColWidth =列表ColWidth + 155;
//trace(myXML.*);
cellArray [arrayCount] = [my_mc.x,my_mc.y];
跟踪(CellArrayCount =+ cellArray [arrayCount]);
arrayCount = arrayCount + 1;
}
myRowHeight = myRowHeight + 105;
列表ColWidth = 50;
}
您的跟踪,因为它是在XML加载之前执行抛出一个错误。
ActionScript是asyncronous,这意味着,在加载了XML,程序执行进行下去,而且看起来是这样的:
- 声明
testXML:XML
和myFPBxml:XML
- 创建加载器和启动加载
- 添加监听器加载完成
- 跟踪
testXML
- 执行
processFPBxml
当XML被加载
如果您移动跟踪到 myXMLTrace
函数,那么它会正常工作。
新加入了code,问题是这一行:
对象(本).my_mc.yellowHiLite.visible = FALSE;
有没有必要使用这
关键字都在这里。这将在里面工作或功能外:
my_mc.yellowHiLite.visible = FALSE;
您code函数里面失败的原因是因为内部和功能外这
将对象的范围,此code所在。但是,当你把code到一个函数,参考创建 - my_mc中
- 在局部范围的功能,而不是父对象,所以 this.my_mc
是不确定的,因为这
不是函数范围。
I've got a block of code that's doing what I want - it generates a grid of MC's.
As soon as I put something like function blah() around it, it starts generating errors indicating lines of code I don't have e.g.
TypeError: Error #1010: A term is undefined and has no properties.
at flightCellMaker_fla::MainTimeline/myXMLtrace() [flightCellMaker_fla.MainTimeline::frame1:87]
at flightCellMaker_fla::MainTimeline/processFPBxml() [flightCellMaker_fla.MainTimeline::frame1:52]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
When I take the function out, it does what I want it to do. What's up with that?
var testXML:XML;
var myFPBxml:XML;
// Initialise a URLLoader to get XML data from XML file
var myFPBLoader:URLLoader = new URLLoader();
myFPBLoader.load(new URLRequest("flightPlannerBoard.xml"));
// Check XML data fully loaded
myFPBLoader.addEventListener(Event.COMPLETE, processFPBxml);
// Once the flight board planning data is loaded, save it to a variable
function processFPBxml(e:Event):void {
myFPBxml = XML(e.target.data);
myXMLtrace();
}
// Grab the XML data load completed and make it available elsewhere
function myXMLtrace(){
testXML = XML(myFPBxml);
}
trace("***********************" + testXML.*); This throws an error (not within myXMLtrace tho)
OK, so here's the rest of the code that will run correctly on it's own but not in a function:
// Create and place all the flight cells for planning and drag and drop
// Setup 2 loops: j for columns and i for Rows
for (var j:Number =0; j < rowNum; j++){
for (var i:Number =0; i<9; i++){
// Create copies of flightCell for board grid
var my_mc = new flightCell();
my_mc.name = "mc"+i+j;
addChild(my_mc);
// Set event Listeners on all Child objects
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler);
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_OUT, fl_MouseOutHandler);
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_DOWN, fl_MouseDownHandler);
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_UP, fl_MouseUpHandler);
Object(this).my_mc.yellowHiLite.visible = false;
//cellPos[j] = myXML.cellPosX[j];
//trace(stage.myXML.*);
//trace(my_mc.name);
my_mc.x = (100 + colWidth);
my_mc.y = myRowHeight;
colWidth = colWidth + 155;
//trace(myXML.*);
cellArray[arrayCount] = [my_mc.x, my_mc.y];
trace("CellArrayCount = " + cellArray[arrayCount]);
arrayCount = arrayCount + 1;
}
myRowHeight = myRowHeight + 105;
colWidth = 50;
}
Your trace is throwing an error because it is being executed before the XML is loaded.
ActionScript is asyncronous, which means that while the XML is being loaded, the program execution carries on going, and looks something like this:
- Declare
testXML:XML
andmyFPBxml:XML
- Create loader and start loading
- Add a listener for load completion
- Trace
testXML
- Execute
processFPBxml
when the XML is loaded
If you move your trace into the myXMLTrace
function then it will work correctly.
With the newly added code, the problem is with this line:
Object(this).my_mc.yellowHiLite.visible = false;
There is no need to use the this
keyword at all here. This will work inside or outside a function:
my_mc.yellowHiLite.visible = false;
The reason your code fails inside a function is because inside and outside the function this
will be the scope of the object in which this code resides. However, when you put the code into a function, the reference you create - my_mc
- is scoped locally to the function, not to the parent object, and so this.my_mc
is undefined, because this
is not the function scope.
这篇关于AS3把功能等等(){左右code产生于code线的错误我没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!