A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Controlling MovieClip Boolean from stage.

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    4

    Controlling MovieClip Boolean from stage.

    I have a small actionscript problem. My final one until my game is complete.

    I'm looking to get and receive variables from and to a movieclip.

    I have given my movieclip and instance name of 'timeclip' and am trying to send to it that
    var paused:Boolean = false() and trying to receive the variables
    minutes and seconds from my movieclip.

    Currently my code looks like this -

    timeclip (movieclip)
    Code:
    var startingTime:int = getTimer ();
    if (!paused){
    addEventListener(Event.ENTER_FRAME, calculateDifference);
    
    function calculateDifference (event:Event){
    var timePassed:int = getTimer() - startingTime;
    var seconds:int = Math.floor (timePassed / 1000);
    var minutes:int = Math.floor (seconds / 60);
    seconds -= minutes * 60;
    if (seconds < 10){
    timeText.text = String ("You've been playing for: " + minutes +":0" + seconds);
    }
    else if (seconds >= 10){
    timeText.text = String ("You've been playing for: " + minutes + ":" + seconds);
    }
    }
    }

    Main stage
    Code:
    stop()
    timedisplay.text = String ("You've been playing for: " + timer.minutes + ":" + timeclip.seconds);
    var paused:Boolean = false;
    I am currently recieving the error

    1120: Access of undefined property paused.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    MovieClips are 'dynamic' meaning you can give them new properties at runtime...which is what you're doing with paused. The trouble is that those are stuck to the outside of the MC and the inside doesn't know to look for them. Try changing this on your main stage:

    PHP Code:
    timeclip.paused false
    And then add 'this' to the code inside the MC so it know's to look at itself:

    PHP Code:
    if (!this.paused){ 

  3. #3
    Junior Member
    Join Date
    Aug 2008
    Posts
    4
    Quote Originally Posted by neznein9
    MovieClips are 'dynamic' meaning you can give them new properties at runtime...which is what you're doing with paused. The trouble is that those are stuck to the outside of the MC and the inside doesn't know to look for them. Try changing this on your main stage:

    PHP Code:
    timeclip.paused false
    And then add 'this' to the code inside the MC so it know's to look at itself:

    PHP Code:
    if (!this.paused){ 
    Works perfectly. Thanks so much.

    Also, is there any way I can get the timepassed variable from the movie clip to the main stage?

    At the moment it comes up as undefined.

  4. #4
    Junior Member
    Join Date
    Aug 2008
    Posts
    4
    Having more problems.

    Where exactly would I put the !pause in my movieclip's actionscript to prevent it from still counting. At the moment I have it placed like this.

    Code:
    //var startingTime:int = getTimer ();
    addEventListener(Event.ENTER_FRAME, calculateDifference);
    
    function calculateDifference (event:Event){
    if (!this.paused){
    var timePassed:int = getTimer() - startingTime;
    var seconds:int = Math.floor (timePassed / 1000);
    var minutes:int = Math.floor (seconds / 60);
    seconds -= minutes * 60;
    }
    if (seconds < 10){
    timeText.text = String ("You've been playing for: " + minutes +":0" + seconds);
    }
    else if (seconds >= 10){
    timeText.text = String ("You've been playing for: " + minutes + ":" + seconds);
    }
    }
    I have the first frame as paused, then continue to the second frame which is unpaused and yet, when I unpause it, it'll just instantly jump up to 5 - 8 seconds instead of starting at 0.

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    getTimer() tells you when the swf started...it's a system command so you can't really stop it and start it. You could either use a Timer or else save the current time when you pause and subtract that away when you resume - but you'd need to keep a running tally summing the time you've been paused.

  6. #6
    Junior Member
    Join Date
    Aug 2008
    Posts
    4
    Quote Originally Posted by neznein9
    getTimer() tells you when the swf started...it's a system command so you can't really stop it and start it. You could either use a Timer or else save the current time when you pause and subtract that away when you resume - but you'd need to keep a running tally summing the time you've been paused.
    What would be the most effective way of incorporating this into my code?

    Sorry, I have never worked with timers before.

  7. #7
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    When I cracked open textmate I realized that Timer is probably a bad choice for this...instead just keep track of the current time and tally the play time vs. the time you've been paused. Try this:

    PHP Code:
    const MILLISECOND:Number 1;
    const 
    SECOND:Number MILLISECOND 1000;
    const 
    MINUTE:Number SECOND 60;
    const 
    HOUR:Number MINUTE 60;

    const 
    startTime:int getTimer();
    var 
    lastTick:int startTime;
    var 
    playTime:int 0;
    var 
    nullTime:int 0;

    addEventListener(Event.ENTER_FRAMEcalculateDifference);

    function 
    calculateDifference(event:Event):void{
        const 
    now:int getTimer();

        if(
    this.paused){
            
    nullTime += (now lastTick);
        } else {
            
    playTime now startTime nullTime;
        }
        
        
    lastTick now;
        
        var 
    seconds:int = (playTime SECOND) % MINUTE;
        var 
    minutes:int playTime MINUTE;

        
    timerText.text "You've been playing for: " minutes ":" + ((seconds 10) ? "0" "") + seconds


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