Skip navigation
AnneLoesHuizing
Currently Being Moderated

Spacebar

Jun 21, 2012 6:42 AM

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

 
Replies
  • Currently Being Moderated
    Jun 21, 2012 8:42 AM   in reply to AnneLoesHuizing

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 21, 2012 3:58 PM   in reply to AnneLoesHuizing

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 5:34 AM   in reply to AnneLoesHuizing

    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

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 6:34 AM   in reply to AnneLoesHuizing

    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)

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 7:47 AM   in reply to AnneLoesHuizing

    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;

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 11:41 AM   in reply to AnneLoesHuizing

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points