Okay here’s my situation:
I’ve got code which is creating sound objects. The code is inside a method of an object which is then registered to a MovieClip in the library and later instantiated with attachMovie(); This code is totally standard, and looks something like this:
this.theSound = new Sound(this);
this.theSound.attachSound(soundName);
Now my basic problem is hat this code stops working shortly after the app starts and through testing I’ve discovered the precise line of code that is the culprit. Elsewhere in the script therere is another object that contains an array (created in its constructor with the line this.theSounds = new Array();). As soon as any instance of this object actually populates its theSounds array, all future Sound objects created fail (they show up as undefined in the debugger). For those who don’t like to read prose:
theNewObject = function () {
this.theSounds = newArray();
//sound objects will still work
this.theSounds[0] = "puppies";
//all future sound objects will be broken
}
WHAT THE #*&!?
for inquiring minds, here’s the full code:
/********************************
* *
* MySound object definition *
* and object registration *
* *
********************************/
MySound = function() {
//Constructor is in construct() function
}
MySound.prototype = new MovieClip();
MySound.prototype.construct = function(soundName, start, end, y, rgb) {
this.startFrame = start;
this.endFrame = end;
this._x = start + _level0.theTimeline._x;
this._y = y + _level0.theTimeline._y + 1;
this._height = 29;
this._width = (end-start);
this.theColor = new Color(this);
this.theColor.setRGB(rgb);
this.theSound = new Sound(this);
this.theSound.attachSound(soundName);
trace(this.theSound.duration);
}
MySound.prototype.start = function() {
this.theSound.start(0, 999);
}
MySound.prototype.stop = function() {
this.theSound.stop()
}
MySound.prototype.remove = function() {
this.removeMovieClip();
}
MySound.prototype.onFrame = function() {
this.startFrame--;
this.endFrame--;
this._x = this.startFrame + _level0.theTimeline._x;
};
Object.registerClass("Bar", MySound);
/********************************
* *
* Channel object definition *
* *
********************************/
Channel = function(ypos) {
this.ypos = ypos;
this.theSounds = new Array();
//If I add a line this.theSounds[0] = "puppies"; then all future Sound objects will be broken.
}
//Channel.prototype.theSounds = new Array();
Channel.prototype.addSound = function(soundName, start, color, duration) {
after = 0;
end = Math.floor(start+(duration * (12/1000))); //IN FRAMES
arraylen = this.theSounds.length; // shortcut
if (arraylen > 0) {
trace("Array is populated");
if (this.theSounds[arraylen - 1].endFrame <= start) {
trace("New sound starts after last element.");
name = _level0.theTimeline.getNewName();
this.theSounds[arraylen] = attachMovie("Bar", "sound" + name, name);
this.theSounds[arraylen].construct(soundName, start, end, this.ypos, color);
return name;
}
for (i=arraylen - 2; i>=0; i--) {
if (this.theSounds*.endFrame <= start) {
trace("New sound starts after " + i + "th element.");
if (this.theSounds[i+1].startFrame>=end) {
name = _level0.theTimeline.getNewName();
this.theSounds.splice(i+1, 0, attachMovie("Bar", "sound" + name, name));
this.theSounds[i+1].construct(soundName, start, end, this.ypos, color);
return name;
} else {
return false;
}
}
}
if (this.theSounds[0].startFrame>=end) {
trace("New sound starts before first element");
name = _level0.theTimeline.getNewName();
this.theSounds.splice(0, 0, attachMovie("Bar", "sound" + name, name));
this.theSounds[0].construct(soundName, start, end, this.ypos, color);
return name;
} else {
return false;
}
} else {
trace("Array is empty")
name = _level0.theTimeline.getNewName();
//Right here is where the sound gets nuked
this.theSounds[0] = attachMovie("Bar", "sound" + name, name);
this.theSounds[0].construct(soundName, start, end, this.ypos, color);
return name;
}
}
Channel.prototype.onFrame = function() {
for (sound in this.theSounds) {
this.theSounds[sound].onFrame();
if (this.theSounds[sound].endFrame==0) {
this.theSounds[sound].stop();
} else if (this.theSounds[sound].startFrame == 0) {
this.theSounds[sound].start();
} else if (this.theSounds[sound].endFrame < -100) {
this.theSounds[sound].remove();
this.theSounds.splice(sound, 1);
//this.theSounds.pop();
}
}
}
/********************************
* *
* Timeline class definition *
* and registration to *
* Timeline MovieClip object *
* *
********************************/
function Timeline() {
for (i=0; i<4; i++) {
this.channels* = new Channel((i*30));
}
}
Timeline.prototype = new MovieClip();
Timeline.prototype.origen = 150;
Timeline.prototype.name = 0;
Timeline.prototype.channels = new Array();
Timeline.prototype.getNewName = function () {
this.name++;
return(this.name);
}
Timeline.prototype.drop = function(x, y, soundName, color, duration) {
channel = 0;
if (0<=x && x<=360) {
if (0<=y && y<=30) {
channel = 0;
} else if (30<=y && y<=60) {
channel = 1;
} else if (60<=y && y<=90) {
channel = 2;
} else if (90<=y && y<=120) {
channel = 3;
} else {
return false;
}
} else {
return false;
}
trace ("Channel is " + channel);
start = x;
result = this.channels[channel].addSound(soundName ,start, color, duration);
trace("Result is " + result);
}
Timeline.prototype.onEnterFrame = function() {
for (channel in this.channels) {
this.channels[channel].onFrame();
}
}
Timeline.prototype.onMouseUp = function() {
this.drop(this._xmouse, this._ymouse, "weddell 9", 0x336633, 4519);
}
Object.registerClass("Timeline", Timeline);
Thank you very much!!