In the flash AS3.0 programming practice, we often find the following error on the output panel when compiling:
Typeerror: error # 1009: the property or method referenced by an empty object cannot be accessed
Here is a blog post from CSDN:
AS3 can’t access the properties or methods of empty object reference. AS3 Flash CS4’s built-in compiler is too general. It only reports errors without exposing error lines. The most depressing thing is that “can’t access the properties or methods of empty object reference” only points out error files without reporting error lines.
The cause of this error is access, setting the properties of the empty object or calling the methods of the empty object.
The first point may be:
For example, var obj:Object;
obj.aaa()
When this line of error code is called by fla in as file, an error will be reported.
The second point may be:
The most extensive is the stage object. If a compiled SWF file is imported into fla as a component, when the SWF has the code of the stage object,
will import the stage as its stage, but it can not be acquired, so it will cause stage=null called in SWF, and the compiler will report “unable to access the attribute or method of empty object reference”.
For example, stage. Displaystate = stagedisplaystate. Full_ SCREEN
Will cause compilation errors. This error also occurs in the as document class. If a is the document class of a fla and another B class is instantiated in a, an error will be reported if there is a stage object or other empty object in B class. Please check it carefully.
Solution: add the object name. Addeventlistener (event. Added)_ TO_ Stage, the name of the processing function), and then write the stage related operations in the processing function. Note that it can only be written in this function or the function called by this function. Also import flash.events.event
The third point may be:
When loading a large file, if you do not listen for the complete event, you hasten to assign various parameters to the loader of the file. If you do not load it completely, you will not be able to find the object.
Solution: loader. Addeventlistener (event. Complete, setting) setting is the function executed after loading
And remember import flash. Events. Event
The fourth point: there is no addchild in the container
A file T1. As
————————————————-
package{
import flash.display.Sprite
public class t1 extends Sprite
{
function t1()
{
var circle = new Sprite();
circle.graphics.beginFill(0x000000, 1)
circle.graphics.drawCircle(0, 0, 15);
addChild(circle);
circle.graphics.endFill();
}
}
}
——————————————————-
Another file t0. As
——————————————–
package {
import flash.display.Sprite;
public class t0 extends Sprite {
function t0() {
var obj=new t1();
//addchild (obj)// missing addchild
trace(this);
}
}
}
Create a new fla, set the document class to T1, find that you can draw a black circle, but if you set the document class to T0, it will report that you cannot access the empty object.
Because each as file can be regarded as an independent display object container, the addchild in the. As file object actually adds various display objects to this container.
Only as files set as document classes by fla will be added to stage. The others are containers filled with display objects. When we need to display them on the stage, we must first instantiate them in the document class as file code, and then addchild();
Here, through watching a tutorial on the Internet, I try to make a flash that simulates the touch screen of a mobile phone to drag and unlock
The final effect picture is as follows:
01
02
03
stage UI:
the stage timeline structure is as follows:
First layer: gray background and mobile phone
The second layer: the first frame contains
The second frame stores a clip animation with logo from top to bottom and alpha from 0 to 1
Layer 3: ActionScript
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.display.MovieClip;
stop();// Stop at the first frame
var startx: number = dragmc. X// Store the X coordinate of dragmc on stage in a variable
var starty: number = dragmc. Y// Store the X coordinate of dragmc on stage in a variable
var Myr ect:Rectangle = new Rectangle(startX,startY,slideMc.width – dragMc.width,0); // Drag limit rectangle of dragmc
var RT: MovieClip = root as MovieClip
dragMc.buttonMode = true;
dragMc.addEventListener(MouseEvent.MOUSE_ DOWN,ondown);
stage.addEventListener(MouseEvent.MOUSE_ UP,onup);// Add mouse release listening to stage instead of dragmc. Readers can try to add situations to dragmc
functionondown(e:MouseEvent):void
{
dragMc.startDrag(true,myRect);
dragMc.addEventListener(Event.ENTER_ FRAME,onframe);
}
function onup (E: mouseevent): void
{
if (this. Currentframe = = this. Totalframes)// check whether the stage frame is in the second frame
{
return
}
else
{
dragMc.removeEventListener(Event.ENTER_ FRAME,onframe);
dragMc.stopDrag();
}
At the beginning of the function, I wrote it like this
dragMc.removeEventListener(Event.ENTER_ FRAME,onframe);
dragMc.stopDrag();
there is no if — else statement. Why does it report the error of null object?According to this example, it is possible to release the mouse in the second frame. If there is no object dragmc on the stage in the second frame, write dragmc. Removeeventlistener (event. Enter) directly_ FRAME,onframe); If you do, you will report the above error**********************/
}
functiononframe(e:Event):void
{
if (dragMc.x >= slideMc.x + slideMc.width/2 – dragMc.width * 2/3)
{
dragMc.removeEventListener(Event.ENTER_ FRAME,onframe);
gotoAndStop(2);
When I started to write code, I wrote gotoandstop (2) in the statement dragmc. Removeeventlistener (event. Enter)_ FRAME,onframe);
in front of, an empty object error is reported. That’s the problem. If the stage frame is stopped in the second frame first, and then the dragmc delete listen is executed, this error will occur. The reason is that there is no dragmc object in the second frame********************/
}
}