这是我的班级文件:
package com
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.events.Event;
import fl.accessibility.CheckBoxAccImpl;
import flash.display.MovieClip;
import fl.controls.CheckBox;
public class main extends MovieClip
{
public function main()
{
stop();
trace("b4 fate")
stage.addEventListener(Event.ENTER_FRAME, preview)
}
public function preview(e:Event):void
{
if (currentFrame == 1)
{
stage.addEventListener(Event.ENTER_FRAME, actions1);
}
else
{
stage.addEventListener(Event.ADDED_TO_STAGE, actionsother);
}
}
public function actions1(e:Event):void
{
trace("b4 event listeners")
l1.addEventListener(MouseEvent.CLICK, goto2);
l2.addEventListener(MouseEvent.CLICK, goto3);
l3.addEventListener(MouseEvent.CLICK, goto4);
l4.addEventListener(MouseEvent.CLICK, goto5);
l5.addEventListener(MouseEvent.CLICK, goto6);
l6.addEventListener(MouseEvent.CLICK, goto7);
l7.addEventListener(MouseEvent.CLICK, goto8);
l8.addEventListener(MouseEvent.CLICK, goto9);
l9.addEventListener(MouseEvent.CLICK, goto10);
l10.addEventListener(MouseEvent.CLICK, goto11);
l11.addEventListener(MouseEvent.CLICK, goto12);
l12.addEventListener(MouseEvent.CLICK, goto13);
l13.addEventListener(MouseEvent.CLICK, goto14);
l14.addEventListener(MouseEvent.CLICK, goto15);
l15.addEventListener(MouseEvent.CLICK, goto16);
l16.addEventListener(MouseEvent.CLICK, goto17);
l17.addEventListener(MouseEvent.CLICK, goto18);
l18.addEventListener(MouseEvent.CLICK, goto19);
c1.addEventListener(MouseEvent.CLICK, goto2);
c2.addEventListener(MouseEvent.CLICK, goto3);
c3.addEventListener(MouseEvent.CLICK, goto4);
c4.addEventListener(MouseEvent.CLICK, goto5);
c5.addEventListener(MouseEvent.CLICK, goto6);
c6.addEventListener(MouseEvent.CLICK, goto7);
c7.addEventListener(MouseEvent.CLICK, goto8);
c8.addEventListener(MouseEvent.CLICK, goto9);
c9.addEventListener(MouseEvent.CLICK, goto10);
c10.addEventListener(MouseEvent.CLICK, goto11);
c11.addEventListener(MouseEvent.CLICK, goto12);
c12.addEventListener(MouseEvent.CLICK, goto13);
c13.addEventListener(MouseEvent.CLICK, goto14);
c14.addEventListener(MouseEvent.CLICK, goto15);
c15.addEventListener(MouseEvent.CLICK, goto16);
c16.addEventListener(MouseEvent.CLICK, goto17);
c17.addEventListener(MouseEvent.CLICK, goto18);
c18.addEventListener(MouseEvent.CLICK, goto19);
trace("after rl")
function goto2(e:MouseEvent):void
{
trace("after rl2")
gotoAndStop(2);
}
function goto3(e:MouseEvent):void
{
gotoAndStop(3);
}
function goto4(e:MouseEvent):void
{
gotoAndStop(4);
}
function goto5(e:MouseEvent):void
{
gotoAndStop(5);
}
function goto6(e:MouseEvent):void
{
gotoAndStop(6);
}
function goto7(e:MouseEvent):void
{
gotoAndStop(7);
}
function goto8(e:MouseEvent):void
{
gotoAndStop(8);
}
function goto9(e:MouseEvent):void
{
gotoAndStop(9);
}
function goto10(e:MouseEvent):void
{
gotoAndStop(10);
}
function goto11(e:MouseEvent):void
{
gotoAndStop(11);
}
function goto12(e:MouseEvent):void
{
gotoAndStop(12);
}
function goto13(e:MouseEvent):void
{
gotoAndStop(13);
}
function goto14(e:MouseEvent):void
{
gotoAndStop(14);
}
function goto15(e:MouseEvent):void
{
gotoAndStop(15);
}
function goto16(e:MouseEvent):void
{
gotoAndStop(16);
}
function goto17(e:MouseEvent):void
{
gotoAndStop(17);
}
function goto18(e:MouseEvent):void
{
gotoAndStop(18);
}
function goto19(e:MouseEvent):void
{
gotoAndStop(19);
}
/*
var myfile:File = File.applicationStorageDirectory.resolvePath("mytext.txt");
var filestream:FileStream = new FileStream();
filestream.open(myfile, FileMode.WRITE);
filestream.writeUTFBytes("hello data");
filestream.close();
var myfile2:File = File.applicationStorageDirectory.resolvePath("mytext.txt");
var filestream2:FileStream = new FileStream();
filestream2.open(myfile2, FileMode.READ);
textbx.text = filestream2.readUTFBytes(filestream2.bytesAvailable);
filestream2.close();
*/
/*var loadname;
var loadnamevalue;
var testnow;
for (var lo:int = 1; lo <= 18; lo++)
{
loadname = "cb"+lo;
var testiffile:File = File.applicationStorageDirectory.resolvePath("cb1.txt");
if (!testiffile.exists)
{
break;
}
var myfile2:File = File.applicationStorageDirectory.resolvePath(loadname+".txt");
var filestream2:FileStream = new FileStream();
filestream2.open(myfile2, FileMode.READ);
loadnamevalue = filestream2.readUTFBytes(filestream2.bytesAvailable);
filestream2.close();
if (loadnamevalue == "true")
{
this["cb"+lo].selected = true;
}
else
{
this["cb"+lo].selected = false;
}
}
var current;
for (var inc = 1; inc <= 18; inc++)
{
current = "cb"+inc;
this["cb"+inc].addEventListener(Event.CHANGE, storedata);
}
var storage:Array;
var writevalue;
var nowname;
function storedata(e:Event):void
{
for (var ninc:int = 1; ninc <=18; ninc++)
{
nowname = "cb"+ninc;
if (this["cb"+ninc].selected == true)
{
writevalue = "true";
}
else
{
writevalue = "false";
}
var myfile:File = File.applicationStorageDirectory.resolvePath(nowname+".txt");
var filestream:FileStream = new FileStream();
filestream.open(myfile, FileMode.WRITE);
filestream.writeUTFBytes(writevalue);
filestream.close();
}
}*/
var loadname;
var loadnamevalue;
var varname;
var testnow;
for (var lo:int = 1; lo <= 18; lo++)
{
loadname = "cb" + lo;
var testiffile:File = File.applicationStorageDirectory.resolvePath(loadname + ".txt");
if (testiffile.exists)
{
var myfile2:File = File.applicationStorageDirectory.resolvePath(loadname + ".txt");
var filestream2:FileStream = new FileStream();
filestream2.open(myfile2, FileMode.READ);
loadnamevalue = filestream2.readUTFBytes(filestream2.bytesAvailable);
filestream2.close();
if (loadnamevalue == "true")
{
this["l" + lo].visible = false;
}
else
{
this["l" + lo].visible = true;
}
}
}
}
public function actionsother(e:Event):void
{
trace("gotthere")
backbtn.addEventListener(MouseEvent.CLICK, goback);
function goback(e:MouseEvent):void
{
gotoAndStop(1);
}
var checkbox:CheckBox = new CheckBox()
addChild(checkbox)
checkbox.x=100;
checkbox.y=100;
//trace("start");
var loadname;
var loadnamevalue;
var testnow;
trace("1")
loadname = "cb"+(currentFrame-1);
var testiffile2:File = File.applicationStorageDirectory.resolvePath(loadname + ".txt");
if (testiffile2.exists)
{
var myfile2:File = File.applicationStorageDirectory.resolvePath(loadname + ".txt");
var filestream2:FileStream = new FileStream();
filestream2.open(myfile2, FileMode.READ);
loadnamevalue = filestream2.readUTFBytes(filestream2.bytesAvailable);
filestream2.close();
if (loadnamevalue == "true")
{
checkbox.selected = true;
}
else
{
checkbox.selected = false;
}
}
else
{
//trace("doesnt exist");
}
checkbox.addEventListener(Event.CHANGE, storedata);
var storage:Array;
var writevalue;
var nowname;
function storedata(e:Event):void
{
nowname = loadname;
trace(nowname);
if (checkbox.selected == true)
{
writevalue = "true";
}
else
{
writevalue = "false";
}
var myfile:File = File.applicationStorageDirectory.resolvePath(loadname + ".txt");
var filestream:FileStream = new FileStream();
filestream.open(myfile, FileMode.WRITE);
filestream.writeUTFBytes(writevalue);
filestream.close();
}
}
}
}
单击按钮时,我一直收到此错误:Error #1009: Cannot access a property or method of a null object reference.
为什么我收到此错误?我该如何解决?
答案 0 :(得分:1)
使用Event.ADDED_TO_STAGE
代替Event.ENTER_FRAME
。因为要使用Event.ENTER_FRAME
事件,必须将对象添加到舞台。
答案 1 :(得分:0)
我首先在调试器中运行您的代码。你知道它失败了吗?这应该可以帮助你找出什么是null并从那里开始工作。
答案 2 :(得分:0)
我发现了几件事。
每个帧都会调用preview()的监听器(无论你是否调用stop())。这意味着你正在调用stage.addEventListener(Event.ENTER_FRAME,actions1)的每一帧;这只会在第一时间添加听众,但仍应避免作为最佳做法。
至于你得到的错误,首先要尝试的是调用trace(l1);在你的构造函数中。如果在运行它时输出类似“[对象动画片段]”的内容,那么你就是好的。如果相反它没有任何痕迹并且给出错误,则需要确保已向舞台添加了一个按钮并为其指定了实例名称l1。如果它确实跟踪了我们的预期,那么您的代码应该看起来更像这样:
public class main extends MovieClip { public function main() { stop(); l1.addEventListener(MouseEvent.CLICK, goto2); } public function preview(e:Event):void { gotoAndStop(2); } }
希望这有帮助!
答案 3 :(得分:0)
启用调试器:文件&gt;发布设置&gt; Flash&gt;允许调试