How do I display a button at a certain time, then have it disappear again in Actionscript 3? - actionscript-3

I am trying to make a game that involves hitting keys/clicking buttons at the right time. I want to have the button/indicator hidden until the right time but am having a bit of trouble getting it to display. This is the code I have for it so far.
var count:Number = 5;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent): void {
timer_txt.text = String((count)-myTimer.currentCount);
}
btnthing.addEventListener(MouseEvent.CLICK, btnclick);
btnthing.visible=false;
while (((count)-myTimer.currentCount)==1) {
btnthing.visible=true;
}
function btnclick(e:MouseEvent): void {
if (((count)-myTimer.currentCount) == 1) {
myTimer.stop();
btnthing.visible=false;
time_txt.text = "Yay!";
} else {
myTimer.stop();
gotoAndStop(3);
}
}
So far my code starts the timer and displays a countdown. If I remove the part that hides/shows the button, everything works fine.
var count:Number = 5;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent): void {
timer_txt.text = String((count)-myTimer.currentCount);
}
btnthing.addEventListener(MouseEvent.CLICK, btnclick);
function btnclick(e:MouseEvent): void {
if (((count)-myTimer.currentCount) == 1) {
myTimer.stop();
btnthing.visible=false;
timer_txt.text = "Yay!";
} else {
myTimer.stop();
gotoAndStop(3);
}
}
If anyone could help me get this right that would be awesome. Thanks!

You're not using the 'while' loop correctly. Change it to a 'if' statement and put it inside the countdown function like this:
function countdown(event:TimerEvent): void
{
timer_txt.text = String((count)-myTimer.currentCount);
if (((count)-myTimer.currentCount)==1) {
btnthing.visible=true;
}
}
so that your code tests for the currentCount on each tick of the timer and finally does something about it.
Read up on 'while' loops - they don't do exactly what you think they do and it's easy to get 'em into infinite repitions if you don't increment the thing that you're changing within the loop.

Related

How to stop the timer and move to next scene when puzzle is solved

I created a jigsaw in action script and kept it a separate class. In the project i linked the code and i added a timer to the scene so that the player have to solve the puzzle within the time specified. Now what i want is that if the player solved the puzzle correctly the timer will stop and go to next scene.
this is the puzzle.as
var xpos:Array=new Array();
var ypos:Array=new Array();
var numDone,i:int;
var numPieces:int=3;
var dif:int=25; //how close do they have to drag the piece
var sound:Sound=new Sound();
function playSound(file:String): void {
//var sound:Sound=new Sound();
var req:URLRequest=new URLRequest(file);
sound.load(req);
sound.play();
} //playSound
function scramble(): void {
for(i=0;i<numPieces;i++){
this["piece"+i].x=Math.random()*300;
this["piece"+i].y=Math.random()*500;
this["piece"+i].alpha=.8;
} //for each piece
numDone=0;
txtMessage.text="Drag the pieces to solve the puzzle";
sound.close();
} //scramble
function init(): void {
for(i=0;i<numPieces;i++){
xpos[i]=this["piece"+i].x;
ypos[i]=this["piece"+i].y;
this["piece"+i].addEventListener(MouseEvent.MOUSE_DOWN, beginMove);
this["piece"+i].addEventListener(MouseEvent.MOUSE_UP, endMove);
} //for each piece
txtMessage.text="Drag the pieces to solve the puzzle";
} //init
function beginMove(e:MouseEvent): void {
if(e.target.alpha<1)
e.target.startDrag();
} //beginMove
function endMove(e:MouseEvent): void {
if(e.target.alpha<1) {
e.target.stopDrag();
//figure out which piece it is
var piece:int=0;
while(e.target!=this["piece"+piece]){
piece++;
} //
var curX:int=this["piece"+piece].x;
var curY:int=this["piece"+piece].y;
if(curX<=xpos[piece]+dif && curX>=xpos[piece]-dif &&
curY<=ypos[piece]+dif && curY>=ypos[piece]-dif) { //close enough
this["piece"+piece].x=xpos[piece];
this["piece"+piece].y=ypos[piece];
this["piece"+piece].alpha=1;
numDone++;
if(numDone>=numPieces) gameOver();
else playSound("http://www.zebra0.com/langResource/ok.mp3");
} //in position
} //not already done
}//endMove
function gameOver(): void {
txtMessage.text="You did it!";
playSound("http://www.zebra0.com/langResource/win.mp3");
gotoAndStop(1,"Scene 2");
} //gameOver
function scrambleHandler(e:MouseEvent): void {
scramble();
} //clickHandler
btnScramble.addEventListener(MouseEvent.CLICK,scrambleHandler);
function solve(e:MouseEvent): void {
for(i=0;i<numPieces;i++) {
this["piece"+i].x=xpos[i];
this["piece"+i].y=ypos[i];
this["piece"+i].alpha=1;
} //for each piece
} //solve
the code for linking the puzzle.as to the project. i kept in a layer called 'ActionScript'
include "Puzzle.as"
numPieces=20;
init();
scramble();
stop();
timer code i kept in a layer called 'action'
var nCount:Number = 90;
var myTimer:Timer = new Timer(1000, nCount);
timer_txt.text = nCount.toString();
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
function countdown(e:TimerEvent):void
{
nCount--;
timer_txt.text = nCount.toString();
}
function onComplete(e: TimerEvent):void{
gotoAndStop(35);
}
if any one have an idea of how to achieve this.

Can't make MovieClip in function dissapear

I'm trying to make simple shooting game with Fiat Multipla falling up to bottom of the screen. I have created function to generate falling multipla and within this function I have a problem.
The main issue is that after change of multideath status to 1 "Death" function does nothing even if It is kept with ENTER_FRAME. Child becomes invisible as I implemented it in multipla movieclip, but even after response from there with Death = 1, nothing happens.
I'm new to all this, I've met and solved few issues during programming, but here's my brickwall for now. Code's either failing completely or I don't know something that's obvious. As I said, I'm newbie.
Thanks a lot for help!
Here's the function:
import flash.events.Event;
import flash.desktop.NativeApplication;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
Mouse.hide();
var velocity = 0;
var ammo = 6;
LGUI.LGUIammo.gotoAndStop(6);
var counter = 0;
function multiplarain()
{
var x1 = Math.ceil(Math.random() * 280);
var y1 = -200;
var random:Multipla = new Multipla();
var life = 265;
var multideath = 0;
random.x = 100 + x1;
random.y = y1
addChild(random);
random.gotoAndStop(1);
setChildIndex(random, +1);
addEventListener(Event.ENTER_FRAME, Death);
function Death(event:Event):void
{
if(multideath >= 1)
{
removeEventListener(Event.ENTER_FRAME, Death);
removeChild(random);
}
}
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
function fl_EnterFrameHandler(event:Event):void
{
if(random.y >= 680)
{
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler)
removeChild(random);
trace("rofl");
}
}
random.addEventListener(Event.ENTER_FRAME, fl_AnimateVertically);
function fl_AnimateVertically(event:Event)
{
velocity = velocity + 0.000035;
random.y += 1.5 + velocity;
}
random.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);
function fl_TapHandler(event:TouchEvent):void
{
counter = counter + 1;
ammo -= 1;
}
if(ammo == 6)
{
LGUI.LGUIammo.gotoAndStop(6);
}
if(ammo == 5)
{
LGUI.LGUIammo.gotoAndStop(5);
}
if(ammo == 4)
{
LGUI.LGUIammo.gotoAndStop(4);
}
if(ammo == 3)
{
LGUI.LGUIammo.gotoAndStop(3);
}
if(ammo == 2)
{
LGUI.LGUIammo.gotoAndStop(2);
}
if(ammo == 1)
{
LGUI.LGUIammo.gotoAndStop(1);
}
if(ammo <= 0)
{
LGUI.LGUIammo.gotoAndStop(7);
}
HGUI.saved.text = counter;
this.addEventListener( Event.ENTER_FRAME, handleCollision)
var kucyk = LGUI.LGUIlife.lifeitself;
function handleCollision(e:Event):void
{
if (random.hitTestObject(LGUI))
{
kucyk = LGUI.LGUIlife.lifeitself;
kucyk.width -= 0.1;
}
/*if (kucyk.width == 0.75)
{
trace("cycki");
NativeApplication.nativeApplication.exit();
}*/
}
}
and here's multipla's movieclip in library code:
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
this.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler2);
function fl_TapHandler2(event:TouchEvent):void
{
this.gotoAndPlay(2);
}
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
function fl_EnterFrameHandler(event:Event):void
{
if(this.currentFrame == 60)
{
this.visible = false;
MovieClip(root).multideath = 1;
trace(MovieClip(root).multideath);
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);
removeEventListener(Event.ENTER_FRAME, fl_TapHandler2);
}
}
it's been like 10 years since I last time worked with AS2 but I'd guess that this Multipla sets the multideath property in the wrong place. If i remember corrctly, root is the top-most level (your application). So if your first code is not on the main timeline but in a movieclip that is on the main timeline it won't work. Try to put a trace into the Death function to see if the multideath is really changing there:
trace(multideath);
try this in the multipla code:
parent.multideath = 1;
instead of
MovieClip(root).multideath = 1;
And I'm asking myself why do you need so many enter frame listeners? You can have just one and combine all animations in one function.
Also you don't need to check for multideath on every frame, just remove the movieclip in a separate function:
function removeMultipla():void
{
removeChild(random);
}
Just call this function from your Multipla instead of setting the multideath property:
parent.removeMultipla();

AS 3 Flash Countdown Timer For Game Over Screen

I am currently in a Flash game programming class with action script 3 and can't seem to find a clock anywhere that counts down and then does an action. I've been using Lynda tutorials and that hasn't been helpful and I have also Google searched quite a bit, but no luck. I have a Countdown clock and have been try "if" statements, but no luck.
Can someone guide me in the right direction on what I am doing wrong?
//game timer
var count:Number = 60; // amount of time
var myTimer:Timer = new Timer(1000,count); // time in ms, count is calling from line above
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
}
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
This should help ;) Without any magic numbers.
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
myText_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(2);
}
Add your if statement inside countdown like so:
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
}

How to fix this duplicate function definition error?

I am totally new at Flash CS6 Action script 3. I have to do an assignment of a scene that have to have mouse, keyboard, enter frame, and time events. Every time I test the movie out, I keep on getting an error that says, 'duplicate function definition' about the 'enter frame event' and 'timer event' functions'. I have tried to rename the functions, but it didn't work. Is there another solution to this? Also, could you please show some examples? Thanks!
QuestionMC.addEventListener(MouseEvent.CLICK, onClick);
QuestionMC.addEventListener(MouseEvent.ROLL_OVER, questionOver);
QuestionMC.addEventListener(MouseEvent.ROLL_OUT, questionOut);
function questionOver(event:MouseEvent):void
{
event.target.alpha = .5;
}
function questionOut(event:MouseEvent):void
{
event.target.alpha = 1;
}
function onClick(event:MouseEvent):void
{
//trace("click!!!");
event.target.y -= 15;
event.target.rotation += 45;
}
QuestionMC.buttonMode = true;
stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);
stage.addEventListener(KeyboardEvent.KEY_UP, land);
function jump (event:KeyboardEvent): void
{
trace(event.keyCode);
YoshiMC.y -= 50
stage.removeEventListener(KeyboardEvent.KEY_DOWN, jump);
stage.addEventListener(Event.ENTER_FRAME, flip);
}
function land (event:KeyboardEvent): void
{
YoshiMC.y += 50
stage.addEventListener(KeyboardEvent.KEY_DOWN, jump);
stage.removeEventListener(Event.ENTER_FRAME, flip);
}
function flip(event:Event):void
{
YoshiMC.rotation += 45;
YoshiMC.x += 20;
}
var jumpTimer:Timer = new Timer(5000,1);
jumpTimer.addEventListener(TimeEvent.TIMER, jump);
function jump (event:TimerEvent):void
{
planteaterMC.play();
}
jumpTimer.start();
var link:URLRequest = new URLRequest("");
buttongoeshere.addEventListener(MouseEvent.CLICK, clickHere);
function clickHere(event:MouseEvent):void
{
navigateToURL(link);
}
buttongoeshere.buttonMode = true;
Your jump function is declared twice, here:
function jump (event:KeyboardEvent): void
and here:
function jump (event:TimerEvent):void
You just need to rename one of them

AS3 loop movieClip based on set timer

I want to set a duration of time by hitting space bar twice. I then want a movieclip to play for that exact amount of time, then loop to play again at for that set amount of time, and so on. until I set a different amount of time by hitting the space bar twice again.
var beat:int;
var beatcount:int;
var tempopress:int;
var num:Number;
num = 0;
tempopress = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN,checker);
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
var myTimer:Timer=new Timer(20,0);
myTimer.addEventListener(TimerEvent.TIMER, stopWatch);
function stopWatch(event:TimerEvent):void {
beatcount = Number(myTimer.currentCount);
}
function checker(e:KeyboardEvent){
if(e.keyCode==Keyboard.SPACE){
if (tempopress == 0) {
trace('start');
beatcount = 0;
myTimer.reset();
myTimer.start();
tempopress = 1;
} else {
trace('stop');
myTimer.stop();
trace(beatcount);
tempopress = 0;
}
}
}
stage.addEventListener(Event.ENTER_FRAME, loopPlayback);
function loopPlayback() {
var loopTimer:Timer=new Timer(20,beatcount);
myTimer.addEventListener(TimerEvent.TIMER, loopWatch);
}
function loopWatch(event:TimerEvent):void {
if (MovieClipMan.currentFrame >= MovieClipMan.totalFrames ){
MovieClipMan.gotoAndStop(1);
} else {
MovieClipMan.nextFrame();
}
}
I know it's a mess haha. Please help! :]
I'd perhaps try something like this, which essentially is checking to see whether to do the loop or not each frame.
var timeStart:Number;
var loopDuration:Number;
var timeLastLoop:Number;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onKeyDown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE) {
if (!timeStart) { // First time SPACE is hit
timeStart = getTimer();
} else { // Second time SPACE is hit
loopDuration = getTimer() - timeStart; // set the loop duration
timeStart = NaN; // reset the start time
loop();
}
}
}
function onEnterFrame(e:Event):void {
if (loopDuration && timeLastLoop) {
if (getTimer() >= timeLastLoop + loopDuration) { // if it's time to loop
loop();
}
}
}
function loop():void {
timeLastLoop = getTimer();
someMovieClip_mc.gotoAndPlay(0);
}
First, use getTimer() to find the difference in time between space bar keypress.
Next, would be to stop creating a new Timer in every frame. It should be created outside of the enter frame handler. Then on the second keypress, you can set the delay property to the difference, and restart the timer.
The most important changes would be here:
if (tempopress == 0) {
trace('start');
myTimer.stop();
startTime = getTimer();
beatcount = 0;
tempopress = 1;
} else {
trace('stop');
myTimer.delay = getTimer() - startTime;
myTimer.reset();
myTimer.start();
tempopress = 0;
}
Then, the timer event handler can just send the MovieClip to frame 1.