all groups > flash actionscript > october 2006 >
You're in the

flash actionscript

group:

Problem with getting the values out of class


Problem with getting the values out of class mloncaric
10/26/2006 8:00:10 PM
flash actionscript:
HI

The problem is that I cannot get the value from class and then used it in
textfield.

I'm getting different values from XML file and one of them is name of current
song.

It works fine when I trace it inside of for loop which is inside xml.onLoad
function
but when I want to trace it outside of that xml.onLoad function it traces me
undefined and the same result is in any other function...

I don't know where is the problem. I'm sure it's my fault because this is my
first class and I don't know much of it right now.

If you have any idea what am I doing wrong please tell me.

Thanks in advance

This is the current code of that class:

import com.mosesSupposes.fuse.*;
//
class com.mloncaric.soundPlayer {
//
var xml:XML;
var urls:Array;
var band_txt:Array;
var song_txt:Array;
var album_txt:Array;
var _sound:Sound;
var _index:Number = 0;
var pos:Number;
var vol:Number;
var _loaded:Number;
//
var isPlaying:Boolean = false;
//
function soundPlayer() {
ZigoEngine.register(Fuse, PennerEasing);
trace(_index);
}
//
function loadXML(targetXML:String) {
var root:Object = this;
xml = new XML();
urls = new Array();
band_txt = new Array();
song_txt = new Array();
album_txt = new Array();
_sound = new Sound();
pos = new Number();
vol = new Number();
//_loaded = new Number();
//
xml.ignoreWhite = true;
//
xml.onLoad = function(success:Boolean) {
if (success) {
var temp:Array = this.firstChild.childNodes;
//
for (var i:Number = 0; i<temp.length; i++) {
root.band_txt.push(temp[i].attributes._band_txt);
root.song_txt.push(temp[i].attributes._song_txt);
root.album_txt.push(temp[i].attributes._album_txt);
root.urls.push(temp[i].attributes._src);
//trace(root.song_txt[i]);//undefined
}
}
};
//
xml.load(targetXML);
}
//
function playSong(mc1:MovieClip, mc2:MovieClip) {
if (isPlaying == false) {
_sound.start(0, 100);
_sound.loadSound(urls[_index], true);
isPlaying = true;
playpause(mc1, mc2, isPlaying);
} else {
_sound.start(0, 100);
_sound.loadSound(urls[_index], true);
isPlaying = false;
playpause(mc1, mc2, isPlaying);
}
}
function pauseSong(mc1:MovieClip, mc2:MovieClip) {
if (isPlaying == true) {
pos = _sound.position/1000;
_sound.stop();
isPlaying = false;
} else {
_sound.start(pos, 100);
isPlaying = true;
}
}
function stopSong(mc1:MovieClip, mc2:MovieClip) {
pos = 0;
_sound.stop();
if (isPlaying) {
isPlaying = false;
playpause(mc1, mc2, isPlaying);
} else {
playpause(mc1, mc2, isPlaying);
}
}
function nextSong() {
if (_index<urls.length-1) {
_index++;
pos = 0;
//
if (isPlaying == true) {
_sound.start(pos, 100);
_sound.loadSound(urls[_index], true);
}
} else {
_index = 0;
//
if (isPlaying == true) {
_sound.start(pos, 100);
_sound.loadSound(urls[_index], true);
}
}
}
function prevSong() {
if (_index>0) {
_index--;
pos = 0;
//
if (isPlaying == true) {
_sound.start(pos, 100);
_sound.loadSound(urls[_index], true);
}
} else {
_index = urls.length-1;
//
if (isPlaying == true) {
_sound.start(pos, 100);
_sound.loadSound(urls[_index], true);
}
}
}
function soundVolume() {
_sound.setVolume(vol);
}
function playpause(mc1:MovieClip, mc2:MovieClip, xy:Boolean) {
if (xy == true) {
mc1._visible = false;
mc2._visible = true;
} else {
mc2._visible = false;
mc1._visible = true;
}
}
function RollOver(target_mc:MovieClip) {
_animation(target_mc, 100);
}
function RollOut(target_mc:MovieClip) {
_animation(target_mc, 50);
}
function _animation(target_mc:MovieClip, alpha:Number) {
var f:Fuse = new Fuse();
//
f.push({target:target_mc, _alpha:alpha, seconds:0.5,
ease:"easeOutQuad"});
f.start();
}
function progressStatus(pBar:MovieClip, scrub_mc:MovieClip,
posBar:MovieClip, ozadje_mc:MovieClip) {
_loaded = _sound.getBytesLoaded()/_sound.getBytesTotal();
pBar._width = _loaded*ozadje_mc._width;
scrub_mc._x = (_sound.position/_sound.duration)*pBar._width;
posBar._width = scrub_mc._x;
//trace(song_txt[_index]);//undefined
}
}
Re: Problem with getting the values out of class mloncaric
10/30/2006 12:00:00 AM
Re: Problem with getting the values out of class LuigiL
10/30/2006 2:24:02 PM
Re: Problem with getting the values out of class mloncaric
10/30/2006 3:50:56 PM
Thanks for reply!

Here is XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<soundPlayer>
<song
_band_txt="Janis Joplin"
_song_txt="Piece of My Heart"
_album_txt="Album"
_src="Janis_Joplin_-_Piece_of_My_Heart.mp3"
/>
<song
_band_txt="Billy Idol"
_song_txt="Sweet Sixteen"
_album_txt="Album"
_src="Billy Idol - Sweet Sixteen.mp3"
/>
</soundPlayer>
Re: Problem with getting the values out of class mloncaric
10/30/2006 3:56:07 PM
Sample code for using it:

import com.mloncaric.*;
//
var sound:soundPlayer = new soundPlayer();
//
sound.loadXML("soundPlayer.xml");
//
play_btn.onRelease = function() {
sound.playSong();
};
Re: Problem with getting the values out of class LuigiL
10/30/2006 5:12:16 PM
Put a trace on _index in the progressStatus() function:
function progressStatus(pBar:MovieClip, scrub_mc:MovieClip, posBar:MovieClip,
ozadje_mc:MovieClip) {
_loaded = _sound.getBytesLoaded()/_sound.getBytesTotal();
pBar._width = _loaded*ozadje_mc._width;
scrub_mc._x = (_sound.position/_sound.duration)*pBar._width;
posBar._width = scrub_mc._x;
trace(_index);
}
What's the result?
Re: Problem with getting the values out of class mloncaric
10/30/2006 5:24:53 PM
I get undefined... I've tried that before and many other variables...but all
what I get is undefined, exept in playSong function loadSound works fine...
This is my first class so it's big possibility that the problem is in bad
formed class, and because of that I don't know much about scopes and similar
things...

Thanks for reply

mloncaric
Re: Problem with getting the values out of class LuigiL
10/30/2006 6:07:29 PM
Well, I can't run the complete code (just the xml part) but it looks like a
scope problem. I can't see from what scope the function progressStatus is
running. Try using the local reference to the class (similar to the usage in
loadXML) --- var root=this; and try calling class members using that reference.
Otherwise you can zip ALL files and post a link to the zip so I can run the
code here.
Re: Problem with getting the values out of class mloncaric
10/31/2006 12:00:00 AM
Thanks for helping me!

Here is link to files(without of mp3s for keeping the small size of rar file)
Re: Problem with getting the values out of class LuigiL
10/31/2006 12:42:33 PM
Re: Problem with getting the values out of class mloncaric
10/31/2006 1:17:43 PM
OK, here is zip file:
Re: Problem with getting the values out of class LuigiL
10/31/2006 1:59:18 PM
See attached code - lines 24/25 and 167/168



import com.mosesSupposes.fuse.*;
import mx.utils.Delegate;
//
class soundPlayer {
private static var MILISECONDS:Number = 1000;
//
private var xml:XML;
private var urls:Array;
private var band_txt:Array;
private var song_txt:Array;
private var album_txt:Array;
private var _sound:Sound;
private var _index:Number = 0;
private var pos:Number;
private var vol:Number;
private var soundPosition, soundDuration:Number;
private var soundLoaded, soundTotal:Number;
private var _loaded:Number;
private var progressStatusInterval:Number;
private var updateInterval:Number;
//
private var isPlaying:Boolean = false;
//
//set a static reference to this object
private static var thisObj:soundPlayer;

public function soundPlayer() {
thisObj=this;
ZigoEngine.register(Fuse, PennerEasing);
//
_sound = new Sound();
}
//
public function loadXML(targetXML:String) {
xml = new XML();
xml.ignoreWhite = true;
xml.onLoad = Delegate.create(this, onLoadXML);
xml.load(targetXML);
}
private function onLoadXML(success:Boolean) {
urls = new Array();
band_txt = new Array();
song_txt = new Array();
album_txt = new Array();
//
if (success) {
var temp:Array = xml.firstChild.childNodes;
//
for (var i:Number = 0; i<temp.length; i++) {
band_txt.push(temp[i].attributes._band_txt);
song_txt.push(temp[i].attributes._song_txt);
album_txt.push(temp[i].attributes._album_txt);
urls.push(temp[i].attributes._src);
}
}
}
//
public function playSong(mc1:MovieClip, mc2:MovieClip) {
if (isPlaying == false) {
startSound();
isPlaying = true;
playpause(mc1, mc2, isPlaying);
progressStatusInterval = setInterval(progressStatus, 100);
} else {
startSound();
isPlaying = false;
playpause(mc1, mc2, isPlaying);
progressStatusInterval = setInterval(progressStatus, 100);
}
}
public function pauseSong(mc1:MovieClip, mc2:MovieClip) {
//
if (isPlaying == true) {
pos = _sound.position/MILISECONDS;
_sound.stop();
isPlaying = false;
clearInterval(progressStatusInterval);
} else {
_sound.start(pos, 100);
isPlaying = true;
progressStatusInterval = setInterval(progressStatus, 100);
}
}
public function stopSong(mc1:MovieClip, mc2:MovieClip) {
//
pos = 0;
_sound.stop();
if (isPlaying) {
isPlaying = false;
playpause(mc1, mc2, isPlaying);
clearInterval(progressStatusInterval);
} else {
playpause(mc1, mc2, isPlaying);
}
}
public function nextSong() {
if (_index<urls.length-1) {
_index++;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
} else {
_index = 0;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
}
}
public function prevSong() {
if (_index>0) {
_index--;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
} else {
_index = urls.length-1;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
}
}
public function soundVolume() {
//
_sound.setVolume(vol);
}
public function playpause(mc1:MovieClip, mc2:MovieClip, xy:Boolean) {
if (xy == true) {
mc1._visible = false;
mc2._visible = true;
} else {
mc2._visible = false;
mc1._visible = true;
}
}
public function RollOver(target_mc:MovieClip) {
_animation(target_mc, 100);
}
public function RollOut(target_mc:MovieClip) {
_animation(target_mc, 50);
}
public function get getTime():String {
//
var timeCurrent:Number = Math.round(soundPosition);
var timeTotal:Number = Math.round(soundDuration);
var minutesCurrent:Number = Math.round(timeCurrent/60);
var minutesTotal:Number = Math.round(timeTotal/60);
var secondsCurrent:Number = Math.round(timeCurrent%60);
var secondsTotal:Number = Math.round(timeTotal%60);
var ct:String = ((minutesCurrent<10) ? "0" :
"")+minutesCurrent+":"+((secondsCurrent<10) ? "0" : "")+secondsCurrent;
var tt:String = ((minutesTotal<10) ? "0" :
"")+minutesTotal+":"+((secondsTotal<10) ? "0" : "")+secondsTotal;
//
return ct+"/"+tt;
}
public function progressStatus(pBar:MovieClip, scrub_mc:MovieClip,
posBar:MovieClip, ozadje_mc:MovieClip) {
_loaded = soundLoaded/soundTotal;
pBar._width = _loaded*ozadje_mc._width;
scrub_mc._x = (soundPosition/soundDuration)*pBar._width;
posBar._width = scrub_mc._x;
//use the static reference to access class members
trace(thisObj._index);
}
private function _animation(target_mc:MovieClip, alpha:Number) {
var f:Fuse = new Fuse();
//
f.push({target:target_mc, _alpha:alpha, seconds:0.5, ease:"easeOutQuad"});
f.start();
}
private function startSound():Void {
_sound.start(pos, 100);
_sound.loadSound(urls[_index], true);
updateInterval = setInterval(Delegate.create(this, update(_sound)), 100);
}
public function update(__sound:Sound) {
soundPosition = __sound.position/MILISECONDS;
soundDuration = __sound.duration/MILISECONDS;
soundLoaded = __sound.getBytesLoaded();
soundTotal = __sound.getBytesTotal();
trace(soundLoaded);
}
}
Re: Problem with getting the values out of class mloncaric
10/31/2006 2:19:47 PM
Thanks for all your help!

This is my updated code:
In main movie:
[num]
import com.mloncaric.*;
//
var player:soundPlayer = new soundPlayer();
var updateInterval:Number = new Number();
//
//
player.loadXML("soundPlayer.xml");
player.playpause(play_btn, pause_btn);
//
play_btn.onRollOver = function() {
player.RollOver(play_btn);
};
play_btn.onRollOut = function() {
player.RollOut(play_btn);
};
play_btn.onRelease = function() {
player.playSong(play_btn, pause_btn);
updateInterval = setInterval(updateFunction, 100);
};
//
pause_btn.onRollOver = function() {
player.RollOver(pause_btn);
};
pause_btn.onRollOut = function() {
player.RollOut(pause_btn);
};
pause_btn.onRelease = function() {
player.pauseSong(play_btn, pause_btn);
};
//
stop_btn.onRollOver = function() {
player.RollOver(stop_btn);
};
stop_btn.onRollOut = function() {
player.RollOut(stop_btn);
};
stop_btn.onRelease = function() {
player.stopSong(play_btn, pause_btn);
clearInterval(updateInterval);
};
//
next_btn.onRollOver = function() {
player.RollOver(next_btn);
};
next_btn.onRollOut = function() {
player.RollOut(next_btn);
};
next_btn.onRelease = function() {
player.nextSong();
};
//
prev_btn.onRollOver = function() {
player.RollOver(prev_btn);
};
prev_btn.onRollOut = function() {
player.RollOut(prev_btn);
};
prev_btn.onRelease = function() {
player.prevSong();
};
function updateFunction() {
player.update();
status_mc.timer.text = player.getTime;
}
[/num]

Class code:
[num]
import com.mosesSupposes.fuse.*;
import mx.utils.Delegate;
//
class com.mloncaric.soundPlayer {
private static var MILISECONDS:Number = 1000;
//
private var xml:XML;
private var urls:Array;
private var band_txt:Array;
private var song_txt:Array;
private var album_txt:Array;
private var sound:Sound;
private var _index:Number = 0;
private var pos:Number;
private var vol:Number;
private var soundPosition, soundDuration:Number;
private var soundLoaded, soundTotal:Number;
private var _loaded:Number;
//
private var isPlaying:Boolean = false;
//
public function soundPlayer() {
ZigoEngine.register(Fuse, PennerEasing);
//
sound = new Sound();
}
//
public function loadXML(targetXML:String) {
xml = new XML();
xml.ignoreWhite = true;
xml.onLoad = Delegate.create(this, onLoadXML);
xml.load(targetXML);
}
private function onLoadXML(success:Boolean) {
urls = new Array();
band_txt = new Array();
song_txt = new Array();
album_txt = new Array();
//
if (success) {
var temp:Array = xml.firstChild.childNodes;
//
for (var i:Number = 0; i<temp.length; i++) {
band_txt.push(temp[i].attributes._band_txt);
song_txt.push(temp[i].attributes._song_txt);
album_txt.push(temp[i].attributes._album_txt);
urls.push(temp[i].attributes._src);
}
}
}
//
public function playSong(mc1:MovieClip, mc2:MovieClip) {
if (isPlaying == false) {
startSound();
isPlaying = true;
playpause(mc1, mc2, isPlaying);
} else {
startSound();
isPlaying = false;
playpause(mc1, mc2, isPlaying);
}
}
public function pauseSong(mc1:MovieClip, mc2:MovieClip) {
//
if (isPlaying == true) {
pos = sound.position/MILISECONDS;
sound.stop();
isPlaying = false;
} else {
sound.start(pos, 100);
isPlaying = true;
}
}
public function stopSong(mc1:MovieClip, mc2:MovieClip) {
//
pos = 0;
sound.stop();
if (isPlaying) {
isPlaying = false;
playpause(mc1, mc2, isPlaying);
} else {
playpause(mc1, mc2, isPlaying);
}
}
public function nextSong() {
if (_index<urls.length-1) {
_index++;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
} else {
_index = 0;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
}
}
public function prevSong() {
if (_index>0) {
_index--;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
} else {
_index = urls.length-1;
pos = 0;
//
if (isPlaying == true) {
startSound();
}
}
}
public function soundVolume() {
//
sound.setVolume(vol);
}
public function playpause(mc1:MovieClip, mc2:MovieClip, xy:Boolean) {
if (xy == true) {
mc1._visible = false;
mc2._visible = true;
} else {
mc2._visible = false;
mc1._visible = true;
}
}
public function RollOver(target_mc:MovieClip) {
_animation(target_mc, 100);
}
public function RollOut(target_mc:MovieClip) {
_animation(target_mc, 50);
}
public function get getTime():String {
//
var timeCurrent:Number = Math.round(soundPosition);
var timeTotal:Number = Math.round(soundDuration);
var minutesCurrent:Number = Math.round(timeCurrent/60);
var minutesTotal:Number = Math.round(timeTotal/60);
var secondsCurrent:Number = Math.round(timeCurrent%60);
var secondsTotal:Number = Math.round(timeTotal%60);
var ct:String = ((minutesCurrent<10) ? "0" :
"")+minutesCurrent+":"+((secondsCurrent<10) ? "0" : "")+secondsCurrent;
var tt:String = ((minutesTotal<10) ? "0" :
"")+minutesTotal+":"+((secondsTotal<10) ? "0" : "")+secondsTotal;
//
return ct+"/"+tt;
}
public function progressStatus(pBar:MovieClip, scrub_mc:MovieClip,
posBar:MovieClip, ozadje_mc:MovieClip) {
//
_loaded = soundLoaded/soundTotal;
pBar._width = _loaded*ozadje_mc._width;
scrub_mc._x = (soundPosition/soundDuration)*pBar._width;
posBar._width = scrub_mc._x;
}
private function _animation(target_mc:MovieClip, alpha:Number) {
var f:Fuse = new Fuse();
//
f.push({target:target_mc, _alpha:alpha, seconds:0.5, ease:"easeOutQuad"});
f.start();
}
private function startSound():Void {
sound.start(pos, 100);
sound.loadSound(urls[_index], true);
}
public function update() {
soundPosition = sound.position/MILISECONDS;
soundDuration = sound.duration/MILISECONDS;
soundLoaded = sound.getBytesLoaded();
soundTotal = sound.getBytesTotal();
}
}
[/num]

XML file is the same...

This code will be updated soon, but for now it works fine:D

Thank you very much man for all your time you wasted for me, and help.

mloncaric
AddThis Social Bookmark Button