开发者

Help with simple AS3 code class

Here is my class file:

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:M开发者_StackOverflowouseEvent):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();
   }
  }
 }
}

I keep getting this error when I click a button: Error #1009: Cannot access a property or method of a null object reference.

Why am I getting this error? How can I fix it?


Use Event.ADDED_TO_STAGE instead of Event.ENTER_FRAME. Because to using Event.ENTER_FRAME event, object must be added to stage.


I would start by running your code in the debugger. Do you know what line it is failing at? That should help you figure out what is null and work back from there.


Turn on the debugger: File > Publish Settings > Flash > Permit Debugging


A couple things I noticed.

The listener for preview() will get called every frame (regardless of whether you call stop()). This means that every frame you are calling stage.addEventListener(Event.ENTER_FRAME, actions1); which will only add the listener the first time but should still be avoided as a best practice.

As for the error you are getting, the first thing to try is calling trace(l1); in your constructor. If that outputs out something like "[Object Movieclip]" when you run it, then you are good. If instead it traces nothing and gives an error, you need to make sure you have added a button to the stage and gave it an instance name of l1. If it does trace what we expected then your code should probably look more like this:

    public class main extends MovieClip
    {
        public function main()
        {
            stop();
            l1.addEventListener(MouseEvent.CLICK, goto2);
        }
        public function preview(e:Event):void
        {
            gotoAndStop(2);
        }
    }

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜