AS3 TypeError:错误#1009:无法访问空对象引用的属性或方法

时间:2013-12-03 23:43:21

标签: actionscript-3

我(以及在此发帖提问的所有其他人)对AS3来说相当新。所以我疯狂地试图找出造成这个错误的原因。我有一个“自动点唱机”播放器,可以在点击按钮时相应地加载歌曲。但这是我的错误:

  

TypeError:错误#1009:无法访问null的属性或方法   对象参考。在lesson09_V2_S5_fla :: MainTimeline / frame1()

这是我的代码:

import fl.events.SliderEvent;

var snd:Sound;
var channel:SoundChannel;
var trans:SoundTransform;

//create variables to store values for the current song and it's volume and pan        settings.
var currSong:String;
var currVol:Number = .5;
var currPan:Number = 0;

// Array of all the songs in the current playlist.
var songList:Array=new Array("Nothing On You.mp3","Grenade.mp3","Ride.mp3","Pretty       Girl Rock.mp3","Tick Tock.mp3","Dynamite.mp3");

// don't need to see the volume and pan controls until a song is playing
panSlide.visible=false;
volSlide.visible=false;

   //Listeners for the onstage song buttons
song1.addEventListener(MouseEvent.CLICK, chooseSong);
song2.addEventListener(MouseEvent.CLICK, chooseSong);
song3.addEventListener(MouseEvent.CLICK, chooseSong);
song4.addEventListener(MouseEvent.CLICK, chooseSong);
song5.addEventListener(MouseEvent.CLICK, chooseSong);
song6.addEventListener(MouseEvent.CLICK, chooseSong);

//listeners for the volume and pan sliders 
panSlide.addEventListener(SliderEvent.CHANGE, panChange);
volSlide.addEventListener(SliderEvent.CHANGE, volumeChange);


//sets the text field of all of the song buttons to display the names of the songs in      the songList array
for (var i = 0; i < songList.length; i++) {
    var str:String = songList[i] as String;
str = str.replace(".mp3","");
var clip = this["song" + (i + 1)].title;
clip.text = str;
  }

//switch statement to set the current song based on which song button was clicked.

function chooseSong(e:MouseEvent):void {
switch (e.currentTarget.name) {
    case "song1":
        currSong = "../MP3s/"+songList[0] as String;
        break;

    case "song2":
        currSong = "../MP3s/"+songList[1] as String;
        break;

    case "song3":
        currSong = "../MP3s/"+songList[2] as String;
        break;

    case "song4":
        currSong = "../MP3s/"+songList[3] as String;
        break;

    case "song5":
        currSong = "../MP3s/"+songList[4] as String;
        break;

    case "song6":
        currSong = "../MP3s/"+songList[5] as String;
        break;

if (snd != null) {
    channel.stop();
}
snd = new Sound();
snd.load(new URLRequest(currSong));
snd.addEventListener(IOErrorEvent.IO_ERROR, onError);

function onError(e:IOErrorEvent):void {
// Do nothing
}
}
}
channel = new SoundChannel  ;
trans = new SoundTransform(currVol,currPan);
channel = snd.play();
channel.soundTransform = trans;
panSlide.visible = true;
volSlide.visible = true;
//currVolume and pan values are used here for display in the text fields next to    sliders

volLabel.text = "Current Volume " + int(currVol * 100);
panLabel.text = "Current Pan " + int(currPan * 100);

//listens for arrival of ID3 tags
snd.addEventListener(Event.ID3, id3Handler);
 //triggered when id3 tags are available
 //sets info text field to display current song information from id3 tags.
 function id3Handler(event:Event):void {
var id3:ID3Info = snd.id3;
if (id3.songName != null) {
    songTitle.text = id3.songName + "\n";
    info.text="Artist: \n"+id3.artist+"\n \n";
    info.appendText("Album: \n" + id3.album);
    info.appendText("\n\n" + "Available at: \n" + "passionrecords \n.com");
}
}


var format:TextFormat = new TextFormat();
format.font = "Arial Black";
format.color = 0xFFFF00;
format.size = 14;
format.url = "http://www.passionrecords.com/";

info.defaultTextFormat = format;
// uses volume slider value to control volume
function volumeChange(e:SliderEvent):void {
currVol = e.target.value;
volLabel.text = "Current Volume: " + int(currVol*100);
trans.volume = currVol;
channel.soundTransform = trans;
 }


 function panChange(e:SliderEvent):void {
currPan = e.target.value;
panLabel.text = "Current Pan " + int(currPan*100);
trans.pan = e.target.value;
channel.soundTransform = trans;
 }

1 个答案:

答案 0 :(得分:0)

这是正确的..

第73行的错误是:snd = new Sound();