View Full Version : Update variable value
Lee Davies
01-13-2006, 08:01 PM
I'm making a jukebox and I'd like it to show how many seconds have passed in the song. I know how to do this;
track1.position/1000;
However, I'd like the seconds counter to be on the jukebox itself.
I've thrown the above code into a variable;
secs = track1.position/1000;
... and asked a dynamic text box to echo that.
It works, but it doesn't update itself, it only stays at 0.
Considering I have many frames, I can't ask it to go back and re-run the entire frame.
Any suggestions please?
sophistikat
01-13-2006, 08:05 PM
you can use onEnterFrame
this.onEnterFrame = function () {
var secs = track1.position/1000;
trace(secs);
}
Lee Davies
01-14-2006, 12:38 AM
That doesn't work when I put it in to a dynamic text box...
Flash Gordon
01-14-2006, 01:12 AM
That doesn't work when I put it in to a dynamic text box...
then you better show the code you're using.
Lee Davies
01-14-2006, 11:44 PM
Sure, here it is in all it's glory - even if it is a tad long...
Apologies about the comments too, I'm making it for a friend of mine and he needs to know the stuff:
// Code for timer
onEnterFrame = function ()
{
var secs1 = Math.floor(track1.position/1000);
trace(secs);
}
// Define track names here
trackName1 = "Morrowind Theme";
trackName2 = "Morrowind Battle";
trackName3 = "TESIV: Oblivion";
// Define ActionScript code for tracks
// Each new track needs a new line with
// a new number (e.g. "track4 = new Sound();")
track1 = new Sound();
track2 = new Sound();
track3 = new Sound();
// Attach the tracks. Again, each new track
// needs a new line
track1.attachSound("tune_1");
track2.attachSound("tune_2");
track3.attachSound("tune_3");
// Pause button code
btn_pause.onRelease = function ()
{
track1.stop();
}
// Play button code
btn_play.onRelease = function ()
{
// Has to come before "play" button because of the way the
// variable "pos"(position) works
var pos:Number = track1.position/1000;
stopallsounds();
trackName = trackName1;
// Just your basic IF statement
// If no seconds of track1 has played
// then start track1 from the start
// Otherwise if seconds have played
// (meaning track has been paused)
// start from when paused (from
// variable "pos")
if(pos < 1)
{
track1.start(0,0);
}
else if(pos > 1)
{
track1.start(pos,0);
}
}
// Stop button code
btn_stop.onRelease = function ()
{
// Give the text box empty content
// as no track is playing
trackName = "";
track1.stop("tune_1");
pos = 0;
}
// Previous button code
btn_prev.onRelease = function ()
{
stopallsounds();
track3.start(0,0);
trackName = trackName3;
gotoAndPlay(3);
}
// Next button code
btn_next.onRelease = function ()
{
stopallsounds();
track2.start(0,0);
trackName = trackName2;
gotoAndStop(2);
}
// Pretty obvious what this does;
stop();
Whilst I'm here, the STOP button isn't working either. It's acting like a pause button :confused:
sophistikat
01-16-2006, 07:14 PM
That doesn't work when I put it in to a dynamic text box...
Whatchu Talking 'Bout Willis
u may want to double check your syntax because the trace and your variable don't match
// Code for timer
onEnterFrame = function () {
var secs1 = Math.floor(track1.position/1000);
trace(secs);
}
most importantly your running the loop prior to declaring who track1 is and prior to playing your audio file. you need to reconsider when this loop should begin excuting. how about once you hit play?
Lee Davies
01-16-2006, 10:40 PM
It's okay - thanks for your help!
But I figured out a much much easier way.
To have the variables in a movieClip and have a blank frame so it just loops back!
galak
01-16-2006, 11:08 PM
you're tracing secs, whereas you variables name is sec1...
flashead
01-17-2006, 12:24 AM
It's okay - thanks for your help!
But I figured out a much much easier way.
To have the variables in a movieClip and have a blank frame so it just loops back!
ACK! Where'd you come up with that idea?! I mean frame loops? Seriously? We've come so far since Flash 4!
OK, enough razzing... help time... SERIOUS help time...
create a function, lets call it updateTime. put your code inside that.
function updateTime()
{
var secs1 = Math.floor(track1.position/1000);
trace(secs1);
thePathToYourDynamicTextBox.text = secs1;
}
then inside your play button start an onEnterFrame that will call your updateTime function. And in your stop button delete that thing so its not running all the time.
//Something to keep your scopes understandable
var main = this;
// Define track names
heretrackName1 = "Morrowind Theme";
trackName2 = "Morrowind Battle";
trackName3 = "TESIV: Oblivion";
// Define ActionScript code for tracks
// Each new track needs a new line with
// a new number (e.g. "track4 = new Sound();")
track1 = new Sound();
track2 = new Sound();
track3 = new Sound();
// Attach the tracks. Again, each new track
// needs a new line
track1.attachSound("tune_1");
track2.attachSound("tune_2");
track3.attachSound("tune_3");
// Pause button code
btn_pause.onRelease = function ()
{
track1.stop();
delete main.onEnterFrame;
}
// Play button code
btn_play.onRelease = function ()
{
// Has to come before "play" button because of the way the
// variable "pos"(position) works
var pos:Number = track1.position/1000;
stopallsounds();
trackName = trackName1;
// Just your basic IF statement
// If no seconds of track1 has played
// then start track1 from the start
// Otherwise if seconds have played
// (meaning track has been paused)
// start from when paused (from // variable "pos")
if(pos < 1)
{
track1.start(0,0);
}
else if(pos > 1)
{
track1.start(pos,0);
}
main.onEnterFrame = updateTime;
}
// Stop button code
btn_stop.onRelease = function ()
{ // Give the text box empty content
// as no track is playing
trackName = "";
track1.stop("tune_1");
pos = 0;
delete main.onEnterFrame;
}
And if you're anything like me, you'll probably just ignore most/all of this cause now you have something that apparently works. But at least I can feel better knowing I've tried to make a difference.
Lee Davies
01-17-2006, 08:27 PM
The thing I love about programming, especially in Flash, is that there are so many different ways of doing the same thing.
I personally like to keep it simple (even if the definition is relative), but I know other people like to go hardcore and make it really complex!
Thanks for trying though - but as you say, it works! And as I say, it works easily :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.