;

PDA

Click to See Complete Forum and Search --> : Controlling MovieClip Boolean from stage.


Bourkster
08-21-2008, 11:46 PM
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)
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
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.

neznein9
08-22-2008, 12:35 AM
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:

timeclip.paused = false;

And then add 'this' to the code inside the MC so it know's to look at itself:

if (!this.paused){

Bourkster
08-22-2008, 12:39 AM
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:

timeclip.paused = false;

And then add 'this' to the code inside the MC so it know's to look at itself:

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.

Bourkster
08-22-2008, 02:07 AM
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.


//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.

neznein9
08-22-2008, 10:02 AM
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.

Bourkster
08-22-2008, 10:09 AM
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.

neznein9
08-22-2008, 11:23 AM
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:

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_FRAME, calculateDifference);

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;
}