PDA

View Full Version : Determine time before listbox click


Dave At ADC
08-05-2005, 06:07 PM
I have a listbox that has several items that will load a path for a movie. If you click an item, the movie plays. I want this to detect the time before a user clicks on an item in the list box. If 10 seconds pass by and the user doesn't select an item, I want it to play my movieclip, PleaseSelectAVideo...

I assume I would start the timer on my stage level... from there it would detect some click on the list... maybe a selection? I dunno. I'm new to this so I'm not sure what events go on a listbox component (Since today is my first time trying to play with one.) If anyone knows how to do this, please lemme know.

Thanks

pdarley
08-05-2005, 06:32 PM
I would use a setInterval. For instance;

On the timeline put:
_global.timer = setInterval(callListBox,10000);
function callListBox(){
clearInterval(_global.timer);
//enter in statement to call the list box and play it
}


Inside the list box on your buttons put:
clearInterval(_global.timer);

I used global because I do not know how you have your pathing setup, but this shoudl do the trick ideally.

Dave At ADC
08-05-2005, 07:01 PM
How do I add that code to a specific button within my listbox? Whenever I try to click on it, it selects the listbox instance... Any ideas?

pdarley
08-05-2005, 07:16 PM
Honestly I never ever....ever ever....use any of the MX components. Hopefully someone else can chime in.

Sorry man

Paerez
08-05-2005, 07:41 PM
For lists, if a user selects an item, it fires the "change" event. So use this:

// Name of your list on the stage
var myList:List;
var selectListener:Object = new Object();
selectListener.change = function(event) {
// event.target is the same as myList
// Clear interval
// Play the movie
}
myList.addEventListener("change",selectListener);

Good Luck

Dave At ADC
08-05-2005, 08:03 PM
// event.target is the same as myList

Eh, what? Sorry, my noobishness is beating me down on this.

Paerez
08-05-2005, 08:25 PM
no problem. When events are fired, they run the respective function, in this case:
selectListener.change = function(event) {
is run. It passes evemt so that the coder can reference the item that fired the event. So event is the change firing, and the target of the event would be your list. Flash translates this to event.target, which in your case is your list.

Are you using the flash media component? Also, how are you storing the data of the movie in the list? The more code you give, the more I can touch up.

Good Luck!

p.s. I won't do your whole thing though. I do this in my free time (in this case, free time = work mwahah)

Dave At ADC
08-08-2005, 01:38 PM
Well, we needed more code so lemme give you some. This is what I have on my listbox 'mylist'


on (change) {
var m = this;
var n = this._parent.VideoPlay;
var p = true;
var playin = 0;
n.setMedia(m.selectedItem.data);


if (p == true) { n.play();
_root.Playing.gotoAndPlay("Start");
p = false;
}
else if (p == false) { n.stop();
_root.Playing.gotoAndPlay("Stopped");
p = true;
}
}

My only problem with the previous bit of code is the "Stopped Sequence" wont play. It just restarts my video and plays the "Start" frames again. But, I have my listbox customized and all pretty. Woot. I just need to figure out this label problem. Also, if the list is 'change' and a video is currently playing, how do I get that clip to stop and play my 'Stopped' frames?

Eh... what else. Oh yea, the timer.

PleaseSelectAVideo.gotoAndStop("Blank");
Playing.gotoAndStop("Blank");
Select.label("Please Select a Video");
var Timer = _root.getTimer();

onEnterFrame = function () {
if (_root.getTimer() > 300) {
onEnterFrame = null;
trace(getTimer);
PleaseSelectAVideo.gotoAndPlay("Start");
} else if ( Timer < 300 ) {
Timer = Timer + 1;
trace(Timer);
}
}

Someone said that this would work too on a different forum... however, it didn't. So, I'm trying to work your method right now... which, looks like the following.

PleaseSelectAVideo.gotoAndStop("Blank");
Playing.gotoAndStop("Blank");

_global.timer = setInterval(callListBox,10000);
function callListBox(){
clearInterval(_global.timer);
//enter in statement to call the list box and play it
}
// Name of your list on the stage
var selectListener:Object = new Object();
selectListener.change = function(event) {

_global.timer = 10000;

}
myList.addEventListener("change",selectListener);

Testing.text = ("Miliseconds:" + _global.timer);
and with that, on my list box (component) I added the...

on (change) {
var m = this;
var n = this._parent.VideoPlay;
var p = true;
var playin = 0;
n.setMedia(m.selectedItem.data);


if (p == true) { n.play();
_root.Playing.gotoAndPlay("Start");
p = false;
}
else if (p == false) { n.stop();
_root.Playing.gotoAndPlay("Stopped");
p = true;
}
clearInterval(_global.timer); //THIS RIGHT HERE
}

I apologize if this is really sloppy code. This isn't going into production for anythign right now. It will be later on in a month or two, so I have time to perfect this. If anyone has any suggestions or examples of a working timer to fit my needs, please post it. Thanks again in advance.

Paerez
08-08-2005, 03:08 PM
1) p = true
...
if (p)
else if (!p) <----- will never happen

2) you can't say clearInterval(_global.timer).

straight from the AS help:

Example
The following example first sets and then clears an interval call:

function callback() {
trace("interval called: "+getTimer()+" ms.");
}
var intervalID:Number = setInterval(callback, 1000);

You need to clear the interval when you have finished using the function. Create a button called clearInt_btn and use the following ActionScript to clear setInterval():

clearInt_btn.onRelease = function(){
clearInterval( intervalID );
trace(“cleared interval”);
};

see? you have to track the interval's ID number and clear it that way, not with the interval itself.

Hope that helps somewhat.