2016-01-05 121 views
-2

我只是一个初学者,我真的很感激一些帮助。 这是我的代码:Actionscript错误#1009无法访问空对象引用的属性或方法

import flash.events.MouseEvent; 
import flash.display.MovieClip; 

var currentButton:MovieClip 
button1.addEventListener(MouseEvent.CLICK, mouseClick); 
button2.addEventListener(MouseEvent.CLICK, mouseClick); 
button3.addEventListener(MouseEvent.CLICK, mouseClick); 
button4.addEventListener(MouseEvent.CLICK, mouseClick); 
function mouseClick(event:MouseEvent):void { 
    currentButton.alpha = 1; 
    currentButton.mouseEnabled = true; 
    currentButton = event.target as MovieClip; 
    trace("CLICK"); 
    currentButton.alpha = 0.7; 
    currentButton.mouseEnabled = false; 
} 

但是当我点击一个按钮,我得到这个错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Untitled_fla::MainTimeline/mouseClick()

回答

0

看来,你的代码有一些问题与您currentButton对象。

我不知道添加下面一行在你的鼠标点击功能的开头应解决您的问题:

function mouseClick(event:MouseEvent):void { 
    var currentButton:MovieClip = event.currentTarget; // Giving proper type to an object is better practise, but since your button objects appear unknown, I have assumed them as MovieClip 
    currentButton.alpha = 1; 
... 
0

我没有多少东西添加到@ kiran`s答案,但我会试着解释为什么你有这个错误。

如果您调试代码,you'ill发现错误是由该行发射:

currentButton.alpha = 1; 

因为只创建了currentButton对象(var currentButton:MovieClip),但你还没有初始化,所以它仍然是null

而要避免这种情况,你刚才使用它,在开始之前初始化对象(您只能被使用的功能,而不是全球创建一个),例如:

var currentButton:MovieClip = new MovieClip(); 

或(内部功能)

var currentButton:MovieClip = event.target as MovieClip; 

或为当前的代码,你刚刚反转的你mouseClick()功能的指令的顺序:

function mouseClick(event:MouseEvent):void 
{ 
    currentButton = event.target as MovieClip; 

    // you can then use "currentButton" 

} 

希望能有所帮助。

相关问题