A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Bad tempo for metronome

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    11

    Bad tempo for metronome

    Hi all.
    First, sorry for my english, i'm french.
    My problem, i try to make a metronome with AS3 but i m very newbie. A french flash forum help me to do it.
    The tempo is not very good, not like a true metronome.
    Please somebody can help me to resolve it.
    Code:
    stop();
    speed = 60;
    stopBt.visible = false;
    
    //cursor//
    var rectangle:Rectangle = new Rectangle(0, 36, 0, 252);
    
    animation.needle.cursorBt.buttonMode = true;
    animation.needle.cursorBt.y = speed*1.2;
    
    
    
    function Stop(e:MouseEvent):void{
    	animation.stop();
    }
    
    
    function Restart(e:MouseEvent):void{
    	animation.play();
    }
    
    animation.needle.cursorBt.addEventListener(MouseEvent.MOUSE_DOWN, Move);
    function Move(e:MouseEvent):void{
    	e.currentTarget.startDrag(false, rectangle);
    	stage.frameRate = 0;
    }
    
    animation.needle.cursorBt.addEventListener(MouseEvent.MOUSE_MOVE, Adapt);
    function Adapt(e:Event):void{
    	stopBt.visible = true;
    	playBt.visible = false;
     speed = animation.needle.cursorBt.y/1.2;
     this.stage.frameRate = speed;
     meter.meter.text = speed;
     trace(speed);
    }
    
    animation.needle.cursorBt.addEventListener(MouseEvent.MOUSE_UP, Release);
    function Release(e:MouseEvent):void{
    	animation.play();
    	animation.needle.cursorBt.stopDrag();
    }
    
    //buttons//
    playBt.addEventListener(MouseEvent.CLICK, Play);
    function Play(e:Event):void{
    	stopBt.visible = true;
    	playBt.visible = false;
    animation.needle.cursorBt.addEventListener(MouseEvent.MOUSE_OVER, Stop);
    animation.needle.cursorBt.addEventListener(MouseEvent.MOUSE_OUT, Restart);
     this.stage.frameRate = speed;
     meter.meter.text = speed;
     animation.play();
     if (meter.meter.text == speed){
      speed = speed;
      }
    }
    
    stopBt.addEventListener(MouseEvent.CLICK, Stop);
    function Stop(e:Event):void{
    	playBt.visible = true;
    	stopBt.visible = false;
     this.stage.frameRate = 0;
     speed = speed;
     animation.gotoAndStop(1);
    }
     
    //Speed//
    var speed:int;
    meter.meter.text = "60";
    trace(speed);
    
    
    incrBt.addEventListener(MouseEvent.MOUSE_DOWN, Increase);
    function Increase(e:Event):void{
     speed = speed + 1;
     this.stage.frameRate = speed;
     compteur.compteur.text = speed;
     animation.needle.cursorBt.y = speed*1.2;
     trace(speed);
     if (speed == 240){
      meter.meter.text = "240";
      }
      if (meter.meter.text >= 240){
       meter.meter.text = "240";
       speed = 240;
       }
    }
    
     decrBt.addEventListener(MouseEvent.MOUSE_DOWN, Decrease);
    function Decrease(e:Event):void{
     speed = speed - 1;
     this.stage.frameRate = speed;
     meter.meter.text = speed;
     animation.needle.cursorBt.y = speed*1.2; 
     trace(speed);
     if (speed == 30){
      meter.meter.text = "30";
      }
      if (meter.meter.text <= 30){
       meter.meter.text = "30";
       speed = 30;
       }
    }
    Here my .fla
    Thank you .

  2. #2
    Member
    Join Date
    Jul 2011
    Location
    United States
    Posts
    37
    Your Fla link is broken

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    11

  4. #4
    Member
    Join Date
    Jul 2011
    Location
    United States
    Posts
    37
    What version of Flash are you using, it does not work in either Flash CS4 or Flash 8?

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Quote Originally Posted by Sethfx View Post
    What version of Flash are you using, it does not work in either Flash CS4 or Flash 8?
    He is using Flash CS5 -.-*
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Member
    Join Date
    Jul 2011
    Location
    United States
    Posts
    37
    How do you know?

  7. #7
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Quote Originally Posted by Sethfx View Post
    How do you know?
    lol, because I have CS5, and it worked, and since it's not working on your version, which is CS4, it's pretty obvious :P
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Yes Sethfx and nig 13, i m using CS5.
    So do you have a solution for my problem, please?

  9. #9
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Quote Originally Posted by cite carter View Post
    Yes Sethfx and nig 13, i m using CS5.
    So do you have a solution for my problem, please?
    What do you mean like a true metronome? Do you want to change the speed or do you want it to kinda "bounce" or "ease" back when it reaches either of the sides? I don't think I can help you either way, because I don't use AS3
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    A true metronome is very regular about time, not mine, especially with high speed

  11. #11
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Does it have to be real? I think those who helped you, did a pretty good job
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  12. #12
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    Quote Originally Posted by cite carter View Post
    A true metronome is very regular about time, not mine, especially with high speed
    If you want to keep your beat constant, you can use the Timer class. By using the Timer class, you can have a constant beat. Read the code below, you only need to worry about Metronome() and startBeat(e:TimerEvent). It is however recommended that you learn to understand everything if you are going to continue using AS3. You can change the value of beatsPerMinute to whatever value you like.

    You can use http://www.metronomeonline.com/ to check against. It's also made in Flash. The example I'm giving you should be accurate enough.

    Actionscript Code:
    package
    {
        import flash.display.MovieClip;
        import flash.display.Shader;
        import flash.display.Shape;
        import flash.events.TimerEvent;
        import flash.utils.Timer;
       
        public class Metronome extends MovieClip
        {
            private var timer:Timer;
            private var beatsPerMinute:Number;
            private var timerDelay:Number;
            private var beatAppear:Boolean;
            private var beat:Shape;
           
            public function Metronome()
            {
                //draw up a circle that flashes every timer event
                drawBeat();
                beat.x = 200;
                beat.y = 200;
               
                beatAppear = false;
               
                //in a metronome, 60 means 60 beats per minute
                //so 60 beats last 60000 ms
                //so 1 beat lasts 1000ms
                //Similarly, x means x beats per minute
                //so x beats last 60000 ms
                //so 1 beat relatively to x will last 60000 / x
                //hence timerDelay is (60000 / x) / 2 because you want the beat to flash
                    //I don't think I make sense here
                //which is (60000/beatsPerMinute) / 2
               
                //for this example, I'm going to use 120 for beatsPerMinute
                beatsPerMinute = 120;
                timerDelay = (60000 / beatsPerMinute) / 2;
                timer = new Timer(timerDelay);
                timer.addEventListener(TimerEvent.TIMER, startBeat);
                timer.start();
            }
           
            private function startBeat(e:TimerEvent):void
            {
                if (beatAppear == false)
                {
                    beatAppear = true;
                    addChild(beat);        
                }
                else
                {
                    beatAppear = false;
                    removeChild(beat);
                }
            }
           
            private function drawBeat():void
            {
                beat = new Shape();
                //gray
                beat.graphics.beginFill(0xCCCCCC);
                beat.graphics.drawCircle(0, 0, 40);
                beat.graphics.endFill();
            }
        }
    }

    By the way, next time, use [HIGHLIGHT=Actionscript], it is easier to read. And both of your links are broken.
    Last edited by flashmed; 08-04-2011 at 11:06 AM.

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thank you, i ll try to understand and use it.
    I don't know what s happen with my links. It s usually work.
    So you can try to see this one:
    http://www.citecarter.com/metronome.html
    http://www.citecarter.com/metronome.fla

  14. #14
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    With your current FLA, I come up with a reason why the beat is not consistent:

    Your animation is based on frames. This means that if you want to change the beat, you have to change the frame rate of your movie. So, I'd suggest that you switch to use the Timer class to not having to change the frame rate of your movie frequently as this may cause lag.

    Secondly, there is a few problems you are going to have to deal with. You probably have used a real metronome before. So:
    1. When you increase or decrease the tempo, the weight on the metronome should move down or move up.
    2. When you increase the tempo, the rotation angle of the 'animation' or the bar would be less and less.

    This poses three problems:
    1. Using keyframes to locate where the metronome or that thin and long metal piece (I'm going to call it a bar from now on) that rotates is not going to work because as the tempo increases, the rotation angle is going to decrease.
    2. If the weight is going to move down as the the tempo increases, it is going to have a new position each time and its rotation speed is going to be different each time too.
    3. The weight is not going to move linearly down as the tempo increases. This means you have to come up with a formula to calculate the weight's position that fits onto the scale.

    These are the steps I suggest to make the program work:
    1. Switch to Timer
    2. Delete all animations and limit your FLA to graphics, movie clips and buttons.
    3. Put all static graphics to the bottom layer, including the wooden frame, scale.
    4. Put all buttons, texts into the next above layer.
    5. Put your bar line that supposed to carry the weight without the weight into the next above layer.
    6. Put your weight onto the next above layer.
    7. AS as the topmost layer.

    This graphics layout minimise the number of layers within layers of your movie clips you have to deal with and make coding easier.

    Now you have to think about:
    1. rotational speed for the bar for each tempo, using a formula,
    2. rotational speed for the weight at each tempo, using a formula
    3. how to make sure the weight moves with the bar, basically linking the two rotational speed together somehow with ratios.

    In my opinion, this is not an easy project mainly because of the maths and the thinking. But with mathematical skills, some reading on how to rotate objects, using timers in AS3, this is definitely possible. The Actionscript is going to be a piece of cake once you work out the maths.
    Last edited by flashmed; 08-05-2011 at 07:33 AM.

  15. #15
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    look at post above
    Last edited by flashmed; 08-05-2011 at 07:32 AM. Reason: flashkit stuffed up

  16. #16
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    multipost, sorry.

  17. #17
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    thank you flashmed
    A french guy have a new solution for me using a formula
    Code:
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import flash.events.Event;
    import flash.display.MovieClip;
    import flash.media.Sound;
    import flash.filters.GlowFilter;
    //Déclaration des variables
    var cadenceBase:Number = 30;
    var enMouvement:Boolean = false;
    var currentCadence:int = 30;
    var timeline;
    compteur.text = currentCadence.toString(10);
    //Création de la timeline qui gere le mouvement
    var tweensArray = [
        TweenLite.to(balancier, 1, {rotationZ:-20,ease:Linear.easeNone,onComplete:playToc}),
        TweenLite.to(balancier, 1, {rotationZ:0,ease:Linear.easeNone}),
        TweenLite.to(balancier, 1, {rotationZ:20,ease:Linear.easeNone,onComplete:playToc}),
        TweenLite.to(balancier, 1, {rotationZ:0,ease:Linear.easeNone})
    ];
    
    timeline = new TimelineMax({tweens:tweensArray,repeat:-1,align:TweenAlign.SEQUENCE});
    timeline.stop();
    
    //Traitement du curseur
    var drag:Boolean = false;
    var overCurs:Boolean = false;
    balancier.aiguille.curseur.buttonMode = true;
    var halo:GlowFilter = new GlowFilter(0xffffff,1,8,8,2,3,false);
    balancier.aiguille.curseur.addEventListener(MouseEvent.MOUSE_OVER, overCurseur);
    balancier.aiguille.curseur.addEventListener(MouseEvent.MOUSE_OUT, outCurseur);
    balancier.aiguille.curseur.addEventListener(MouseEvent.MOUSE_DOWN, downCurseur);
    function overCurseur(e:MouseEvent):void{    overCurs = true;
        timeline.pause();
        balancier.aiguille.curseur.filters = [halo];
    }
    function outCurseur(e:MouseEvent):void{
        if(!drag){
            balancier.aiguille.curseur.filters = [];
            if(enMouvement){
                timeline.play();
            }
        }
        overCurs = false;
    }
    function downCurseur(e:MouseEvent):void{
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dragging);
        drag = true;
        stage.addEventListener(MouseEvent.MOUSE_UP, upCurseur);
    }
    function upCurseur(e:MouseEvent):void{
        stage.removeEventListener(MouseEvent.MOUSE_UP, upCurseur);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragging);
        drag = false;
        if(enMouvement && !overCurs){
            timeline.play();
            balancier.aiguille.curseur.filters = [];
        }
    }
    function dragging(e:MouseEvent):void{
        var posY:Number = balancier.aiguille.mouseY;
        if(posY<-325){posY=-325;
    }
        else if(posY>-70){posY=-70;
    }
        balancier.aiguille.curseur.y = posY;
        currentCadence = Math.round(30 + ((posY + 325)/255) * 210);
        majCadence();
    }
    
    //Traittement des boutons
        //Bouton Play & Stop
        btPlay.buttonMode = true;
        btPlay.addEventListener(MouseEvent.MOUSE_DOWN, downPlay);
        function downPlay(e:MouseEvent):void{
            btPlay.visible = false;
            if(!enMouvement){
                timeline.play();
                enMouvement = true;
            }
    else{
                timeline.stop();
                enMouvement = false;
            }
            stage.addEventListener(MouseEvent.MOUSE_UP, upPlay);
        }
        function upPlay(e:MouseEvent):void{
            stage.removeEventListener(MouseEvent.MOUSE_UP, upPlay);
            btPlay.visible = true;
        }
        //Bouton +
        btPlus.buttonMode = true;
        btPlus.addEventListener(MouseEvent.MOUSE_DOWN, downPlus);
        function downPlus(e:MouseEvent):void{
            stage.addEventListener(MouseEvent.MOUSE_UP, upPlus);
            btPlus.visible = false;
            if(currentCadence<240){
                currentCadence += 1;
                majCadence();
            }
        }
        function upPlus(e:MouseEvent):void{
            stage.removeEventListener(MouseEvent.MOUSE_UP, upPlus);
            btPlus.visible = true;
        }
        //Bouton -
        btMoins.buttonMode = true;
        btMoins.addEventListener(MouseEvent.MOUSE_DOWN, downMoins);
        function downMoins(e:MouseEvent):void{
            stage.addEventListener(MouseEvent.MOUSE_UP, upMoins);
            btMoins.visible = false;
            if(currentCadence>30){
                currentCadence -= 1;
                majCadence();
            }
        }
        function upMoins(e:MouseEvent):void{
            stage.removeEventListener(MouseEvent.MOUSE_UP, upMoins);
            btMoins.visible = true;
        }
        //Fonction permettant le changement de la cadence + maj
     compteurfunction majCadence():void{
        timeline.timeScale = currentCadence / cadenceBase;
        var posCurseur:Number = ((currentCadence -30)/210*255) -325;
        if(posCurseur<-325){posCurseur=-325;
    }
        else if(posCurseur>-70){posCurseur=-70;
    }
        balancier.aiguille.curseur.y = posCurseur;
        compteur.text = currentCadence.toString(10);
    }
    //Fonction lancant le son
    var toc:Sound = new Toc();
    function playToc():void{
        toc.play();
    }
    You can find it here http://www.citecarter.com/metronome.rar

    It's pretty good but the tempo can be improve probably.

  18. #18
    Senior Member
    Join Date
    Dec 2010
    Posts
    121
    Very nice. I don't think I should complain because it is actually really nice, but sometimes the beat seems to be a bit off or lagging. It would actually be a great feature if you add time signature beats... I don't think that's how you call it, but it's like 2/4, 3/4, 4/4, 6/8 kind of thing when you have a stronger beat 1. But I've got to say, this is great. Good work to the French guy who helped you.

    About the tempo, I'm not sure what you want to improve, it sounds pretty fine to me.

  19. #19
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    sometimes the beat seems to be a bit off or lagging
    It s exactly my problem, musicians can't work with that, a metronome need to be very exact.

  20. #20
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    I find this website http://simple.bestmetronome.com/ it s exactly what i want to do.
    I'd like to have the same precision without lagg for mine.

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