A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [CS3] [AS2] Creating a Timer

  1. #1
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63

    [CS3] [AS2] Creating a Timer

    Is there a way to create a timer in Actionscript 2.0 similar to using the Timer class in AS 3.0? My problem is that in my game I sometimes have a lot going on at once on screen, lots of layers, things fading in or out, etc. I also have music playing in the background that I need to loop (AIFF files exported as MP3). The problem is that if I set the framerate to a normal speed (24-30 FPS), there's an audible skip sometimes when the music loops, which I don't get if I set the framerate higher (60+ FPS). When I'm using the higher framerate, though, my animations slow down when there's too much going on at once onscreen.

    So I'm wondering if there's a way I can keep the framerate low so my animations all play smoothly, but then have a timer for the audio loops so they aren't locked to the framerate. I was originally going to use setInterval() and have it check every 10 milliseconds to see if the song had finished, but from what I've read/heard setInterval() is usually pretty inaccurate, which would defeat the whole purpose of using it in this case.

    If there's no way of doing this, can anyone recommend a good AS 3.0 book similar to Game Development with ActionScript? I've been meaning to make the switch for a while now but for some reason I can't seem to get my head around some of the concepts in 3.0.

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    that sounds like a bad situation,- at least try to optimize some things on the visual side liek bitmap caching, quality to "MEDIUM" or "LOW" and some other stuff.
    Another way btw. of getting delays is the getTimer(); function it returns the elapsed time the flash player is already running in ms. I use it alot for example to script frame independent animations by checking on each frame the time delay and fixing the tween input value of motions based on that.

    here is a little example using a button that starts a timer
    PHP Code:
    var time 0;
    btn.onPress = function(){
    time getTimer();
    }
    _root.onEnterFrame = function(){
    if (
    time != 0){
    trace("time elapsed: "+(getTimer() - time)+" ms");
    }


  3. #3
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    So basically what I would need to do is set time to 0 when the song starts, then when it reaches however long the song is in milliseconds, have it loop the song and set time back to 0? Wouldn't that still be tied to the framerate, though, since I'd be using an onEnterFrame function to determine when the song ends? I might try upping the framerate again and setting it to Low quality like you said, but I'd really like to keep it around 25-30 fps if possible just to make animating easier. For some reason once I get past 30-40 fps I can't see how any animation will look without publishing it and then opening the SWF in Flash Player; if I just test the movie in Flash it doesn't play at the right speed.

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I have always used onSoundComplete event for looping songs.

  5. #5
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    That's actually exactly what I'm doing. Here's my code:

    PHP Code:
    var bgMusic:Sound = new Sound();
    bgMusic.attachSound("musicLoop");

    function 
    startBGMusic() {
        
    bgMusic.start();
        
    bgMusic.onSoundComplete = function() {
            
    bgMusic.start(7.12);
        };

    What's weird is the skip isn't consistent at a lower framerate; sometimes it loops back perfectly and sometimes there's an obvious skip.

    Also, I'm having some trouble handling leveling up after a battle. It works if I write a ridiculously long if... else if... statement to determine how much EXP is needed to reach each level, but I'd prefer to do it a shorter way if possible. I've been trying to figure out how to use a switch statement instead, but now matter how I do it it doesn't work. Would it be easier to just create an array and store the information that way? I haven't gotten into arrays yet so I'd have to put my game on hold until I get to that chapter (currently reading Macromedia Flash MX Game Programming).

  6. #6
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    I'd say the problem lies in the sound file itself. I want to say I remember reading in the sound forum that there's always a little "empty space" at the end of the mp3 track.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  7. #7
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Yeah, the old ID3 standard stored ID3 info in a header at the end of an MP3 track, and the new standard stores it in a header at the beginning of the track. That's only if you're using MP3 files with ID3 tags, though, and from what I've been told Flash doesn't create ID3 tags, it just converts the file to MP3. Plus, the skip is there even when exporting as RAW.

    Anyway, a thought just occurred to me. Is onSoundComplete() even tied to the framerate? I just assumed it was since the skip is only there when using a lower framerate but nothing I've read mentions anything about how often it checks to see if the sound has ended.

  8. #8
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    It depends on the sound itself too. You cant have it loop without very slight breaks in Flash so its good idea to create a loops which have natural silence in the end.

  9. #9
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    Sounds like you are trying to loop a single file? Or am I wrong?

    If it's just one file, just stick it into a frame ( select frame and in properties for that frame, look under sound, select that file and set it to loop ), you don't need any code. And you should check the file in some sound editing app to make sure it loops without any gaps.

  10. #10
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Yes, it's a single file, but I need to be able to actively switch the song that's playing so I can stop the background music loop and start the battle music when a battle starts, and vice-versa. Plus it starts 7.12 seconds in when it loops back because I have a little intro before the actual loop begins. It's a song I created in GarageBand and there aren't any silent areas at the end, so there shouldn't be any gaps (unless for some reason GarageBand creates a small gap at the end of a file when it exports it). I'll check it with Audacity just to make sure.

    I wonder if my problem might be my laptop. The CPU has been running ridiculously hot lately (around 50ºC during normal use, sometimes over 60º under 90-100% load) and I've been having a lot of other weird issues with it. If I can't find any gaps in Audacity I think I'm going to try playing the SWF on my PC upstairs and see if it still skips.

  11. #11
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    Then I would separate intro from the rest of the file. so you only have to worry about matching the end of intro to the beginning of the main part. From there it's easy looping, just start the sound, and set it to loop forever ( music.start(0, 9999); ). Then when you need the battle music to jump in, don't stop the main music loop, just set volume to 0 and let it keep looping ( even better if you use setInterval to fade it out while fading battle music in, and vice versa )

  12. #12
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Ok well here's what I found out. Increasing the framerate didn't help at all, so I'm not sure why it worked before, but at least now I know onSoundComplete isn't tied to the framerate. Playing it on my PC upstairs didn't fix the problem, so it had nothing to do with my laptop. I also tried converting the AIFF file to various other formats and exporting the original GarageBand file in different formats and nothing worked. The weird thing is, if I attach another AIFF file that wasn't created in GarageBand, it works fine. I also have some MP3 loops I downloaded from FlashKit that work perfectly. Apparently either Flash doesn't like GB or there's something weird about the way GB exports files that causes the skip.

    What I ended up doing was setting both the sound output in GB and the sound input in Audacity to Soundflower and recording the song from GB into Audacity, then exporting it as a WAV file. It's kind of a pain, especially since I'm going to have to record upwards of 20 audio loops this way by the time I'm done with the game, but at least it works.

    Anyway, thanks to everyone who answered, I actually picked up a few interesting ideas from some of the responses.

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