A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: math.random() not working on Timer?!

  1. #1
    Custom User Title
    Join Date
    Sep 2009
    Location
    New Zealand
    Posts
    6

    Question math.random() not working on Timer?!

    First up, I'm a complete noob with AS3 and I've only done maybe 3? projects using it so far? At any rate, my current little project requires text to be displayed for random time intervals. The timer code is as follows:

    Code:
    var timer:Timer = new Timer(Math.random() * 10000);
    timer.addEventListener(TimerEvent.TIMER, changeQuotes);
    timer.start();
    Pretty simple and straight forward stuff HOWEVER the random intervals are not random at all. In actuality the interval is *always* about 5 seconds between each text change for the current code. I've tried *A LOT* of different things to try and get it to be random but no matter what I do, the interval between is always the same. I want the intervals between to be random e.g. text appears for 2 seconds then goes and the next text appears for 5 seconds, then the next for 1 second, then the next for 8 seconds etc etc.

    I would very much appreciate it if any one can help me fix the timer code so it does actually make random times. I've been trying for two days now and I'm about ready to rip my hair out. Thanks in advance!

  2. #2
    Member
    Join Date
    Nov 2005
    Posts
    75
    that's because the expression generated by (Math.random() * 10000) is a Number where the argument must be presented as an integer (6123.6543667 = Number, 6124 = int), try this and this will work:

    Code:
    var timer:Timer = new Timer(Math.round(Math.random() * 10000));
    timer.addEventListener(TimerEvent.TIMER, changeQuotes);
    timer.start();
    Last edited by jazz090; 09-13-2009 at 05:20 AM.

  3. #3
    Custom User Title
    Join Date
    Sep 2009
    Location
    New Zealand
    Posts
    6
    Ok, tried that as well and it's still making it always about 8 seconds apart rather than random. Over the last couple of days, I also tried Math.ceil but same thing. That changed the amount of time but it's still the same amount of time and not random. I've also tried combos like
    Code:
    var timer:Timer = new Timer(Math.round(Math.random()*9000) + 1000);
    but still no random

  4. #4
    Member
    Join Date
    Nov 2005
    Posts
    75
    well this is a completely different issue. in order for the times to be randomised on EACH iteration, you would have to re-instantiate the Timer. since that's not applicable, you have to do it like this:

    Code:
    var timer:Timer = new Timer(Math.round(Math.random()*10));
    timer.addEventListener(TimerEvent.TIMER, iterate);
    timer.start();
    function iterate(e:TimerEvent):void{
    	timer.delay = Math.round(Math.random()*10000);
    }
    Last edited by jazz090; 09-13-2009 at 08:50 AM.

  5. #5
    Custom User Title
    Join Date
    Sep 2009
    Location
    New Zealand
    Posts
    6
    That made it just sit there :/ It will prolly help if I just give the full code lol:
    Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    
    var xml:XML;
    var xmlPath:String = "funnies.xml";
    var loader:URLLoader = new URLLoader();
    var req:URLRequest = new URLRequest(xmlPath);
    loader.addEventListener(Event.COMPLETE, xmlComplete);
    loader.load(req);
    var myTween:Tween;
    
    var timer:Timer = new Timer(Math.random() * 10000);
    timer.addEventListener(TimerEvent.TIMER, changeFunnies);
    timer.start();
    
    function xmlComplete(e:Event):void{
    	xml = new XML(e.target.data);
    	addFunnies();
    	randomFunnyTween();
    }
    
    var funniesArray:Array = new Array();
    var loadingsArray:Array = new Array();
    
    var randomNumber:Number;
    
    function addFunnies(){
    	for each(var item:XML in xml.quotes.quote){
    		funniesArray.push(item.content);
    		loadingsArray.push(item.loading);
    	}
    }
    
    function randomFunnyTween():void{
    	randomNumber = Math.round(Math.random() * (funniesArray.length - 1));
    	funnyMC.funnyTxt.text = funniesArray[randomNumber];
    	loadingsMC.loadingsTxt.text = loadingsArray[randomNumber];
    
    	myTween = new Tween(funnyMC, "alpha", Strong.easeOut, 0, 1, 3, true);
    	myTween = new Tween(funnyMC, "x", Strong.easeOut, 550, 500, 3, true);
    }
    
    function changeFunnies(e:TimerEvent){
    	randomFunnyTween();
    }
    I've been fiddling with it a bit so I may have missed something. At any rate, I really appreciate your help! As I said, I'm new to AS3 and still trying to wrap my head around it.

  6. #6
    flash absorption vessel
    Join Date
    Jul 2009
    Posts
    31
    jazz's code is fine, i think you just didn't wait long enough!

    Also, using Math.ceil instead will prevent a 0 timer delay.

  7. #7
    Custom User Title
    Join Date
    Sep 2009
    Location
    New Zealand
    Posts
    6
    ok.... well I left it going for an hour an 16 minutes and it did nothing other than sit on the first xml text entry. Desirable is to have them change randomly anywhere from about 0 - 10 seconds.

  8. #8
    Member
    Join Date
    Nov 2005
    Posts
    75
    just pull up a new AS3 file, in the first frame paste this and look at the trace outputs, seems to work fine. this is for anywhere between 1-5 seconds so you see the outcome:

    Code:
    var timer:Timer = new Timer(Math.round(Math.random()*5000));
    timer.addEventListener(TimerEvent.TIMER, iterate);
    timer.start();
    function iterate(e:TimerEvent):void{
    	trace(timer.delay);
    	timer.delay = Math.round(Math.random()*5000);
    }

  9. #9
    Custom User Title
    Join Date
    Sep 2009
    Location
    New Zealand
    Posts
    6
    Understandably but when I apply this to the code I already have it just sits there and does nothing
    Bearing in mind, I really know nothing about AS3. I've done the best I can but figuring out where I went wrong and how to make the changes to make it work correctly is beyond my current scope of knowledge, hence my asking nice peeps to help me out

  10. #10
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Why not use setTimeout instead of a Timer? A Timer has more overhead than you need, plus it's not really meant to work like how you want it to work. setTimeout is just a simple function that takes a function and a delay value. It runs once per time. So you just call the function, it basically waits the amount of time, then calls the function you specify. So you can just call that inside your changeFunnies() function, and it'll just keep going on it's own.

  11. #11
    Member
    Join Date
    Nov 2005
    Posts
    75
    then the problem isnt your Timer, its your code

  12. #12
    Junior Member
    Join Date
    Sep 2009
    Location
    Greece
    Posts
    6
    You must use a TimerComplete event

    PHP Code:
    var timer:Timer = new Timer(Math.random() * 100001);//Will run once and call the TimerComplete Event
    timer.addEventListener(TimerEvent.TIMER_COMPLETEonTimerComplete);
    timer.start();

    function 
    onTimerComplete(event:TimerEvent):void
    {
                
    //show/change text here
                
    timer.delay=Math.round(Math.random()*10000);
                
    timer.start();


    http://www.gemstonedragon.com/

  13. #13
    Custom User Title
    Join Date
    Sep 2009
    Location
    New Zealand
    Posts
    6
    MyFriendIsATaco, could you possibly explain how I would implement that? Sorry to ask but I really don't know.

    I'm not stupid, I design and code websites for a living and run my own business. XHTML, CSS, PHP are my fortes and I've only started looking at AS3 in the last week. This is for a personal website of mine www.barbecusealtar.com and between work and having M.E. I just don't have the time or ability right now to learn AS3 which is why I have to rely on people's help to get me started.

    The code that I am using, I created from a tutorial and while I understand what most of it is for and does, I am no where remotely familiar enough with AS3 to know the correct syntax, structure etc which is why, again, I very much appreciate people's help

    jazz090, the code works perfectly so long as I want text to show for set amounts of time (which is not what I want it to do)

    gemstonedragon, I replaced the timer code with what you suggested, but it again just sits there on the first quote and doesn't change at all.

  14. #14
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    PHP Code:
    import flash.utils.setTimeout;

    doLoop();

    function 
    doLoop():void
    {
        var 
    delay:int = (Math.random()*8000)+2000;  // random between 2 and 10 seconds
        
    trace("next delay: "+delay);
        
    setTimeout(doLoopdelay);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center