So I'm trying to use this barebones script to get and play music. I modified it to read from a different folder (Radio), and that seems to work fine. I get an error though when trying to load the damn thing =p
Thoughts on what it might be?
Script:
class Jukebox
attr_reader :list
def initialize
@songs = Dir.entries("Audio/BGM/")
temp = @songs.clone
@list = []
for i in 0..(temp.size)
next if temp[i] == "." or temp[i] == ".." or temp[i] == nil
@list.push(temp[i])
end
for i in 0..(@list.size)
@list[i].gsub!(/\.[^\.]*\Z/) {|s| "" }
end
end
def find(song)
@list.each.do{|i| return i.index if i == song}
end
def play(song)
index = find(song)
Audio.bgm_play(@songs[index + 2], 100, 100)
end
def stop
Audio.bgm_stop
end
end
Screenshot of error:
http://www.tiikoni.com/tis/view/?id=1164d7f
Someone else is welcome to correct me if I'm wrong on this, but I believe that on the line
@list.each.do(|i| return i.index if i == song)
the parentheses should be curly braces {}.
Here's the corrected line:
@list.each.do{|i| return i.index if i == song}
That fixed one problem, now I have this. I've narrowed it down to it happening when I initialize it..
http://www.tiikoni.com/tis/view/?id=5020c9a
I'll upload a working copy of it if needed..
In the "intitialize" method, instead of creating copies and enumerating multiple times, just create the array of song names in one command.
@songs = Dir.glob("Audio/BGM/*.{ogg,mp3,wma,mid,wav}")
Not only is it more efficient, but also allows you to determine which formats are accepted, so if someone drops some file in another format in there, it won't crash the game, but simply be ignored.