Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

For loop throws "term undefined" error?

Explorer ,
May 31, 2013 May 31, 2013

I'm trying to add a playlist to my music player, which loads by xml. I've got the text box set up, and data is added into it after the XML is done loading.

I get an error on line 32, I'll point it out later, about something being not defind.

Here is my code:

var my_songs:XMLList;

var my_total:Number;

var my_sound:Sound;

var my_channel:SoundChannel;

var current_song:Number = 0;

var song_position:Number;

var song_paused:Boolean;

var myXMLLoader:URLLoader = new URLLoader();

var songNames:Array = new Array();

songData.text = "Playlist:\n";

myXMLLoader.load(new URLRequest("playlist.xml"));

myXMLLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {

          var myXML:XML = new XML(e.target.data);

          my_songs = myXML.SONG;

          my_total = my_songs.length();

          //playSong(0);

          myXMLLoader.removeEventListener(Event.COMPLETE, processXML);

          myXMLLoader = null;

          setUpPlaylist();

}

function setUpPlaylist():void

{

          for (var i:int = 0; i <= my_total;i++){

                    songNames.push(String(i)+ ". "+ my_songs.@TITLE);

                    //songData.appendText("A\n");

                    songData.appendText(String(i+1)+ ". " + my_songs.@TITLE + "\n");

          }

}

function playSong(mySong:Number):void {

          var myTitle = my_songs[mySong].@TITLE;

          var myArtist = my_songs[mySong].@ARTIST;

          var myURL = my_songs[mySong].@URL;

          title_txt.text = myTitle;

          artist_txt.text = myArtist;

          if (my_channel) {

                    my_channel.stop();

                    my_channel.removeEventListener(Event.SOUND_COMPLETE, onNext);

          }

          my_sound = new Sound();

          my_sound.load(new URLRequest(myURL));

          my_channel = my_sound.play();

          my_channel.addEventListener(Event.SOUND_COMPLETE, onNext);

}

next_btn.addEventListener(MouseEvent.CLICK, onNext);

function onNext(e:Event):void {

          current_song++;

          if (current_song>=my_total) {

                    current_song=0;

          }

          playSong(current_song);

}

prev_btn.addEventListener(MouseEvent.CLICK, onPrev);

function onPrev(e:MouseEvent):void {

          current_song--;

          if (current_song<0) {

                    current_song = my_total-1;

          }

          playSong(current_song);

}

pause_btn.addEventListener(MouseEvent.CLICK, onPause);

function onPause(e:MouseEvent):void {

          if (my_channel) {

                    song_position = my_channel.position;

                    my_channel.stop();

                    song_paused=true;

          }

}

play_btn.addEventListener(MouseEvent.CLICK, onPlay);

function onPlay(e:MouseEvent):void {

          if (song_paused) {

                    my_channel = my_sound.play(song_position);

                    song_paused=false;

          } else if (!my_channel) {

                    playSong(current_song);

          }

}

This is line 32:

songNames.push(String(i)+ ". "+ my_songs.@TITLE);

Edit:

I was also wondering if It was possible to make a list of movie clips (or buttons) that could be controlled by a scroll bar.

The movie clips would have to be made through as3, with text on them (again added by as3) and then controlled through more code.

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 02, 2013 Jun 02, 2013

What is feeding the textbox?  As I mentioned, I cannot tell if the update function you showed is related to the problem since I cannot see what executes it, but if you happen to be dynamically reading in the text from an external resource, you might be updating the scrollbar before the new text gets written to the textbox.

Translate
LEGEND ,
Jun 01, 2013 Jun 01, 2013

You should show the complete error messages whenever you get them.

It appears your loop might be going one over the number of elements in the xml list....

for (var i:int = 0; i <= my_total;i++){

Since my_total = the length of the list, that is one greater than the last index of the list which is going to be my_total-1

Try:

for (var i:int = 0; i < my_total; i++){

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 01, 2013 Jun 01, 2013

Thanks that fixed it. Do you know why my uiScrollbar isn't working? It works fine when I run it in flash, but anywhere else it doesn't seem to actually move.

I have this imported at the top:

import fl.controls.UIScrollBar;

The scrollbar is actually on the stage, with an instance name of "scrollBar"

Here's the actual code for it:

//var scrollBar:UIScrollBar = new UIScrollBar();//Old method how I loaded it via library.

scrollBar.scrollTarget = songData;

scrollBar.height = songData.height;

scrollBar.move(songData.x + songData.width, songData.y);

addChild(scrollBar);

function updateScrollBar():void{

scrollBar.update();

if (scrollBar.enabled == false) {

scrollBar.alpha = 0;

} else {

scrollBar.alpha = 100;

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 01, 2013 Jun 01, 2013

I cannot tell you why it doesn't work anywhere else, but then I don't know what you mean by anywhere else either.

Is there a reason you set the scrollbar to be the same height as the object it is controlling... that kinda defeats the purpose of having the scrollbar.

Regarding the updateScrollbar function, I don't know if that is related to the problem or not since there is nothing I can see that prompts execution of it.  The alpha property ranges from 0 to 1 in AS3, not 0 to 100, so you can fix that but it is not likely anything related to the problem you say you have.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2013 Jun 02, 2013

I got the alpha from a snippet, so I didn't really second guess that.

What I mean by everywhere else: If I publish the html document and run that, the scrollbar won't be able to move. If I just run the plain .swf anywhere, it doesn't work. If I upload it to my webhost, the scrollbar doesn't work.

It's set to the same size as the object because the object is a text box. There's more text then the box itself can hold. So it scrolls by moving the text off the screen, and new text on it.

The textbox holds about 10 lines of text, I've got about 60 being fed into it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2013 Jun 02, 2013

What is feeding the textbox?  As I mentioned, I cannot tell if the update function you showed is related to the problem since I cannot see what executes it, but if you happen to be dynamically reading in the text from an external resource, you might be updating the scrollbar before the new text gets written to the textbox.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 02, 2013 Jun 02, 2013
LATEST

The forloop that you helped me fix above feeds the text box. It gets the data from a XML list of 60+ songs.

I guess I'll have to try and call the scrollbar after the music list is made, see if that fixes anything.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines