PDA

View Full Version : [AS3] Try again-button


rulator
04-17-2009, 01:49 AM
Hi,
I'm making this game as a schoolexam and I'm almost there..but I need a "Try again" button. But I can't get it to work, you can try yourself here:
http://www-ia.hiof.no/~olavv/v09/flash/kanonspill1.swf (http://www-ia.hiof.no/%7Eolavv/v09/flash/kanonspill1.swf)
Just hold the mouse down for some seconds and release. When it stops your score gets up with a try again button that says "Prøv igjen".
I have some code for it, but I can't get it to work.
My game is all in 1 frame.
Here is the code i got, the try again button is at the end "btnRetry" "Retry"


boks.visible=false;
splash.visible=false;
btnRetry.visible=false;

function flytt(evt:MouseEvent)
{
var vinkel:Number = Math.atan2(stage.mouseY-lop.y,stage.mouseX-lop.x);
lop.rotation = (vinkel/Math.PI)*180;
figur.rotation=lop.rotation+90;

evt.updateAfterEvent();
}

stage.addEventListener(MouseEvent.MOUSE_MOVE,flytt );

function bevegelse(evt:Event)
{
figur.rotation+=0.45;
if(figur.y>lop.y)
{
stage.removeEventListener(Event.ENTER_FRAME,bevege lse);
}
}

var avstand:Number=0;

function flyttFigur(evt:Event)
{
avstand+=Math.round(figur.dx)*0.020;

trace(avstand);

txtTeller.text = avstand.toFixed(0);

if(figur.x<350)
{
figur.x+=figur.dx;
}
else
{
bakgrunn.x-=figur.dx;
if(bakgrunn.x<-bakgrunn.width+stage.stageWidth)
bakgrunn.x=-160;
lop.x-=figur.dx;
}
if(figur.y < stage.stageHeight-60)
{
figur.y+=figur.dy;
}

figur.dy+=0.09;
if(figur.dx>0.05)
{
figur.dx-=0.05;
}
else
{
figur.dx=0;
}

if(figur.dx==0)
{
txtGratulerer.text="Gratulerer, du kom:";
txtGratulerer.visible=true;
txtResultat.text=""+int(txtTeller.text);
txtResultat.visible=true;
txtMeter.text="Meter";
txtMeter.visible=true;
btnRetry.visible=true;
boks.visible=true;
}

if(figur.dx<0.5)
{
figur.visible=false;
figur.dx=0;
figur.dy=0;
splash.visible=true;
splash.x=figur.x;
splash.y=figur.y;
}

}

var startTid:int;

function fyr(evt:MouseEvent)
{
stage.addEventListener(Event.ENTER_FRAME,flyttFigu r);
var hastighet:int = (getTimer()-startTid)/250;
figur.dx= Math.cos((lop.rotation/360)*Math.PI*2)*hastighet;
figur.dy= Math.sin((lop.rotation/360)*Math.PI*2)*hastighet;
stage.removeEventListener(MouseEvent.MOUSE_MOVE,fl ytt);
stage.addEventListener(Event.ENTER_FRAME,bevegelse );
stage.removeEventListener(MouseEvent.MOUSE_UP,fyr) ;
}

function startTidtaking(evt:MouseEvent)
{
startTid=getTimer();
}


stage.addEventListener(MouseEvent.MOUSE_DOWN,start Tidtaking);

stage.addEventListener(MouseEvent.MOUSE_UP,fyr);

function retry(evt:MouseEvent)
{
avstand=0;
figur.x=lop.x;
figur.dx=lop.x;
figur.y=lop.y;
figur.dy=lop.y;
boks.visible=false;
splash.visible=false;
btnRetry.visible=false;
txtGratulerer.visible=false;
txtResultat.visible=false;
txtMeter.visible=false;

}


btnRetry.addEventListener(MouseEvent.CLICK,retry);

bluemagica
04-17-2009, 05:15 AM
the problem is because it is all on one frame! Having it this way, dosen't actually reset the code, unless you reenter the frame....and the glitch is most probably occurring in your flyttFigur function! throw a bunch of trace statements, to trace the exact problem!(i think the problem might be with the value of figur.dx)

as a makeshift solution, try going to frame 2 for retry screen, then come back to frame 1, when someone hits the retry button!

rulator
04-17-2009, 09:27 AM
as a makeshift solution, try going to frame 2 for retry screen, then come back to frame 1, when someone hits the retry button!

how do i do that?

bluemagica
04-17-2009, 01:07 PM
put the retry screen mc on frame 2 and make it visible! In the code where you make it visible, use gotoAndStop(2)

rulator
04-17-2009, 02:11 PM
ok, thanks alot!
But I finally got it to work, in 1 frame only :D
I just had to reset everything hehe