A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: I can't get an object to flash exactly 154 times every minute.

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    58

    I can't get an object to flash exactly 154 times every minute.

    I'm doing a flash game to go with this song on a band's website (sweet band, by the way (www.lessages.com)), and the song would be playing during the game, and I want certain events to go with the beat of the song, and that would be so much easier if I could get a metronome on my flash project, and I'm running into problems with that.

    I can't get an object to flash exactly 154 times every minute, because the song plays at a constant rate of 154 beats per minute. It's always a little bit off, so that after about 30 seconds, it ceases to be on beat. The object that I want to blink is called "blinker." Here is my actionscript:

    Code:
    var beatTimer = new Timer(60000/154); // A timer for how often I want it to appear (154 times every minute, or 154 times every 60,000 milliseconds)
    var disappearTimer = new Timer(20, 1); // A timer for how long I want it to remain before it dissapears (20 milliseconds)
    
    blinker.alpha = 0; // blinker starts out invisible
    beatTimer.start(); // beatTimer starts the moment the swf starts
    
    beatTimer.addEventListener(TimerEvent.TIMER, tickTock);
    disappearTimer.addEventListener(TimerEvent.TIMER_COMPLETE, disappear);
    
    function tickTock(e:TimerEvent):void { // What I want flashplayer to do every time it cycles through beatTimer
    	trace("tick");
    	blinker.alpha = 100; // Show blinker
    	disappearTimer.start(); // Set blinker to disappear in 20 milliseconds
    }
    function disappear(e:TimerEvent):void {
    	blinker.alpha = 0; // after 20 milliseconds, blinker disappears
    }

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    Maybe flash is trying to round that number , causing it be off over time. if not then do you think your framerate could be dropping ? That might cause the timer to slowdown.
    Last edited by AttackRabbit; 12-27-2009 at 10:11 AM.

  3. #3
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    I believe the timer fires according to the frame rate. 154 fps is pretty intense, I doubt it will work how you want, due to frame dropping. If you want to try, it will need to be set with actionscript (stage.frameRate=154), the flash IDE properties panel only allows a max of 120.

    Also, if you do increase the frame rate, it would probably be more accurate to just make your blinker instance into a two frame MovieClip, with one frame being empty. That way, every frame it's always blinking on or off, and you wont need to worry about rounding numbers. (frame dropping will still be an issue though)
    Search first, asked questions later.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    per minute, not per second.

    It's probably due to an accumulation of small errors due to occasional slowdown or dropped frames. You may want to check against actual elapsed time using getTimer, and apply a small correction factor to your timer each execution. I doubt you'll be able to get a perfect metronome going, though.

  5. #5
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    <<< [ Idiot ] I need to slow down and stop wasting brain power on miss-reads...

    Anyway, Flax's solution is probably the best.

    Otherwise, the only way to keep it truly synced would be to create a video (flv) that has the blinking synchronized w/ the music. But unless its a vital part of the game, I doubt it would be worth the file size increase.
    Search first, asked questions later.

Tags for this Thread

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