13 Replies Latest reply: Jun 22, 2012 11:41 AM by Ned Murphy RSS

    Spacebar

    AnneLoesHuizing

      Hi guys, here I am again!
      I solved the problem with the timer and the screen appears now.

       

      import flash.utils.Timer;
      import flash.events.TimerEvent;
      import flash.utils.setTimeout;
      import flash.media.SoundChannel;
      import flash.utils.*;
      import flash.system.ImageDecodingPolicy;
      import flash.display.BitmapData;
      import flash.events.KeyboardEvent;
      
      
      var customPanel:CustomPanel = new CustomPanel();
      
      
      var timer:Timer = new Timer(30,0);
      var timer2:Timer = new Timer(Math.random() * 60000,1);
      var myTimer:Timer = new Timer(1200000,0);
      
      
      var secCntr:int = 0;
      var minCntr:int = 40;
      var hourCntr:int = 23;
      var currentTime:String;
      
      
      var soundRequest:URLRequest = new URLRequest("sounds/Flash Clock.mp3");
      var mySound:Sound = new Sound();
      var soundChannel1:SoundChannel = new SoundChannel();
      
      
      mySound.load(soundRequest);
      soundChannel1 = mySound.play();
      
      
      timer.addEventListener(TimerEvent.TIMER, setTime);
      timer2.addEventListener(TimerEvent.TIMER, surprise);
      stage.addEventListener(KeyboardEvent.KEY_DOWN, pressedKey);
      
      
      timer.start();
      timer2.start();
      
      
      customPanel.visible = false;
      
      
      function surprise(e:TimerEvent):void{
                trace("Surprise!");
                this.addChild(new CustomPanel());
      }
      
      
      function pressedKey(event:KeyboardEvent):void{
                if(event.keyCode == 32){keyDetector()}
        
      }
      
      
      
      
      function keyDetector():void{
                trace("The spacebar key was pressed")
      }
      
      
      
      
        
        
      function setTime(e:TimerEvent){
          secCntr++;
          if(secCntr == 60){
              minCntr++;
              secCntr = 0;
          }
          if(minCntr == 60){
                hourCntr++;
                minCntr = 0;
                }
      
      
                if (hourCntr >= 24){
                hourCntr = 0;}
      
      
      
      
          if(secCntr < 10){
          currentTime = String(hourCntr) + ":" + String(minCntr) + ":0" + String(secCntr);    
          }
          else{
          currentTime = String(hourCntr) + ":" + String(minCntr) + ":" + String(secCntr);
          }
      
      
          countdown_txt.text = currentTime;
      }
      
      

       

      The only is thing that the screen haves to be removes while pressing spacebar. I tried it like this:

       

      function surprise(e:TimerEvent):void{
                trace("Surprise!");
                this.addChild(new CustomPanel());
      }
      
      
      function pressedKey(event:KeyboardEvent):void{
                if(event.keyCode == 32){keyDetector()}
                this.removeChild(customPanel);{
                }
      }
      
      
      
      
      function keyDetector():void{
                trace("The spacebar key was pressed")
      }
      
      

       

      But for some reason this doesn't work. The spacebar does react.

      Have somebody a solution for this?

       

      Greets,

      Anne-Loes

        • 1. Re: Spacebar
          Ned Murphy CommunityMVP

          What about it is not working?  If it's what you show in red, it's because that should have quotes around the string.  Other than that, the removeChild call will function for any key since it is outside the conditional

          • 2. Re: Spacebar
            AnneLoesHuizing Community Member

            They are quotes. For some reason it doesn't show here.

            I get error #1065 Variable TCMText isn't defined

            • 3. Re: Spacebar
              AnneLoesHuizing Community Member

              Now it's saying it's missing a rightbrace. But I don't see it

              • 4. Re: Spacebar
                Ned Murphy CommunityMVP

                What you should do is go into your Flash Publish Settings and select the option to  Permit Debugging.  What this often does is adds line numbers to the error message to indicate where the problemed code is.

                • 5. Re: Spacebar
                  AnneLoesHuizing Community Member

                  It was already set like that. So, it stil doesn't say it. I think it's quite strange, because

                  it worked before..

                  • 6. Re: Spacebar
                    Ned Murphy CommunityMVP

                    You should show the code as you have it now.  While it might not be the problem, it is not correct to have an extra set of brackets in your function below...

                     

                    function pressedKey(event:KeyboardEvent):void{

                              if(event.keyCode == 32){keyDetector()}

                              this.removeChild(customPanel);{    // this brace at the end does nothing

                              }   // this brace does nothing

                    }

                    • 7. Re: Spacebar
                      AnneLoesHuizing Community Member
                      import flash.utils.Timer;
                      import flash.events.TimerEvent;
                      import flash.utils.setTimeout;
                      import flash.media.SoundChannel;
                      import flash.utils.*;
                      import flash.system.ImageDecodingPolicy;
                      import flash.display.BitmapData;
                      import flash.events.KeyboardEvent;
                      
                      
                      var customPanel:CustomPanel = new CustomPanel();
                      
                      
                      var timer:Timer = new Timer(30,0);
                      var timer2:Timer = new Timer(Math.random() * 60000,1);
                      var myTimer:Timer = new Timer(1200000,0);
                      
                      
                      var secCntr:int = 0;
                      var minCntr:int = 40;
                      var hourCntr:int = 23;
                      var currentTime:String;
                      
                      
                      var soundRequest:URLRequest = new URLRequest("sounds/Flash Clock.mp3");
                      var mySound:Sound = new Sound();
                      var soundChannel1:SoundChannel = new SoundChannel();
                      
                      
                      mySound.load(soundRequest);
                      soundChannel1 = mySound.play();
                      
                      
                      timer.addEventListener(TimerEvent.TIMER, setTime);
                      timer2.addEventListener(TimerEvent.TIMER, surprise);
                      stage.addEventListener(KeyboardEvent.KEY_DOWN, pressedKey);
                      
                      
                      timer.start();
                      timer2.start();
                      
                      
                      customPanel.visible = false;
                      
                      
                      
                      
                      
                      
                      
                      
                      function surprise(e:TimerEvent):void
                                {
                                trace("Surprise!");
                                this.addChild(new CustomPanel());
                                }
                      
                      
                      function pressedKey(event:KeyboardEvent):void{
                                if(event.keyCode == 32)
                      
                                {
                                keyDetector()
                      
                                if(contains(customPanel))
                                {
                                removeChild(customPanel);
                                }
                      
                        }
                      
                      }
                      
                      
                      
                      
                      
                      
                      function keyDetector():void
                                {
                                trace("The spacebar key was pressed");
                                }
                      
                      
                      
                      
                      
                      
                      function setTime(e:TimerEvent)
                      {
                      
                          secCntr++;
                          if(secCntr == 60)
                                          {
                              minCntr++;
                              secCntr = 0;
                                    }
                      
                          if(minCntr == 60)
                                          {
                              hourCntr++;
                              minCntr = 0;
                              }
                      
                      
                          if (hourCntr >= 24)
                                          {
                              hourCntr = 0;}
                      
                      
                      
                      
                          if(secCntr < 10)
                                          {
                                          currentTime = String(hourCntr) + ":" + String(minCntr) + ":0" + String(secCntr);    
                                    }
                      
                          else
                                          {
                                          currentTime = String(hourCntr) + ":" + String(minCntr) + ":" + String(secCntr);
                                    }
                      
                      
                          countdown_txt.text = currentTime; 
                      
                      }
                      

                       

                      This is the new code. Thank you

                      • 8. Re: Spacebar
                        AnneLoesHuizing Community Member

                        It's working now. But the spacebar doesn't react.

                        • 9. Re: Spacebar
                          Ned Murphy CommunityMVP

                          You should format your code properly, it is very difficult to read all spread out and oddly indented as you show it.  I had to take you code and reformat it just to see what you have.  After removing any code related to things I do not have, I ran the file and the textfield displayed time counting, and when I press the spacebar I get the trace message: "The spacebar key was pressed" 

                           

                          So it is working fine for what the code tells it to do.  Since customPanel was never added as a child, it is not going to be removed (you create another new instance, but that other new instance is not customPanel)

                          • 10. Re: Spacebar
                            AnneLoesHuizing Community Member

                            Sorry for the confusing code. It's just how I learned to make it.

                            How can I make customPanel a child? Or isn't that possible?

                            • 11. Re: Spacebar
                              Ned Murphy CommunityMVP

                              To make it a child, use:  addChild(customPanel);    right after you create it in the beginning.

                               

                              Then change your function below to make the customPanel visible (you already made it invisible a line before this function)

                               

                              function surprise(e:TimerEvent):void {

                                        trace(Surprise!);

                                        customPanel.visible = true;

                              }

                              • 12. Re: Spacebar
                                AnneLoesHuizing Community Member

                                Thank you! Now it's really working!

                                Can I ask you one more question?

                                I want, if the time ends that it stays at 00:00:00 How can I do this?

                                • 13. Re: Spacebar
                                  Ned Murphy CommunityMVP

                                  In your setTime function, check for when the three time elements all equal zero and at that point stop the Timer that calls that function.