Jump to content

(Flash) AS 3.0 adding a delay to a script before proceeding to next frame


Recommended Posts

Posted

Hello,

 

I am in the middle of creating or trying to create an interactive game. Basically the idea of the game is the user has to dress a Teddy in a space suit ready for his adventure. When the user has done this then they will be moved on to the "next level" where they have to put Teddy inside the Rocket. When they complete this bit Teddy will then take off to the Moon.

 

Simple enough i thought...:eek:

 

So i have got to the point where i have the first level completed. I have two ways i could tackle the progression to the next stage. 1, have a delay after the completion of the first stage or 2, have a button pop up that the user clicks to progress.

 

The only problem is i don't know how to do either. I have tried looking through the help guide but to be honest may as well be in klingon . So how would i go about doing either or if someone is really good at actionscript and feeling generous, both.

 

Here's the script i have so far. It all happens on frame 1, with the next part happening on whatever frame. I don't think it matters...

 

stop();
var startX:Number;
var startY:Number;
var counter:Number = 0;
head_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
head_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
body_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
body_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
legs_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
legs_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
righthand_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
righthand_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
lefthand_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
lefthand_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
rightfoot_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
rightfoot_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
leftfoot_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
leftfoot_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
function pickUp(event:MouseEvent):void {
   event.target.startDrag(true);
reply_txt.text = "";
startX = event.target.x;
startY = event.target.y;
}
function dropIt(event:MouseEvent):void {
   event.target.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
   reply_txt.text = "Good Job!";
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
event.target.buttonMode = false;
counter++;
} else {
   reply_txt.text = "Try Again!";
	event.target.x = startX;
	event.target.y = startY;
}
if(counter == 7){
   reply_txt.text = "Well done. Teddy is now dressed!";
}
}
head_mc.buttonMode = true;
body_mc.buttonMode = true;
legs_mc.buttonMode = true;
righthand_mc.buttonMode = true;
lefthand_mc.buttonMode = true;
rightfoot_mc.buttonMode = true;
leftfoot_mc.buttonMode = true;

 

PS i found this stuff on this website if anyone is interested. Quite good really and lucky i found it to get this far.

 

Hope you can help. I will share the results when i finished if anyone is daft enough to be interested. Need to touch up on the graphics though :D

Posted
1, have a delay after the completion of the first stage or 2, have a button pop up that the user clicks to progress.

 

Option 2 sounds best - have a message on frame 2 saying "Congratulations, Teddy is now dressed!", along with a "Next" button. When your counter hits 7, instead of setting your message text, use gotoAndPlay() to go to frame 2, i.e.:

 

if(counter == 7) {
   gotoAndPlay(2);
}

 

Have your "Next" button on frame 2 have something similar to go on to frame 3 when it is clicked.

 

--

David Hicks

  • Thanks 1
Posted

Thanks dhicks. I have only just seen your post. I will give it a go. It's been absolutely ages since i last did any Flash work, and the work i did never involved Actionscript anyway :p

 

Thanks again. I will post up a link to show you the finish product once it's complete. Graphics have been touched up a little now. Not too good still though since i had my graphics tablet nicked when they took my Macbook pro :(

  • 4 weeks later...
Posted

The following might give you an idea

You can still load up a button & then fade/transition from that if you like

 

fadeEffect.duration = FADE_IMAGE_SECONDS * 1000;

fadeEffect.play();

 

sets the timer duration & triggers it

When the timer event fires - you action the next piece of code

In the timer event

Case some variable so that you can sequence through your program states

On other events you can set the state variable and / or fire new timers

 

 

 

package

{

import flash.display.MovieClip;

import flash.events.Event;

import flash.display.Loader;

import flash.events.TimerEvent;

import flash.net.URLRequest;

import mx.effects.easing.Cubic;

import mx.effects.Fade;

import mx.events.EffectEvent;

 

/**

* ...

* @author

*/

public class Upton extends MovieClip

{

// Number of seconds to show an image file

private const SHOW_IMAGE_SECONDS:int = 4;

// Number of seconds to fade to next image file

private const FADE_IMAGE_SECONDS:int = 2;

// Number of image files to loop through

// files are to be found in /images

// images files are to be named image1.jpg, image2.jpg .....

private const MAX_IMAGES:int = 17;

 

private var index:int = 1;

private var fadeEffect:Fade = new Fade();

private var loader:Loader = new Loader();

 

public function Upton()

{

// setup Image loader

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);

// setup Fader

fadeEffect.addEventListener(EffectEvent.EFFECT_END, fadeComplete);

fadeEffect.startDelay = 0;

fadeEffect.easingFunction = Cubic.easeInOut;

fadeEffect.alphaFrom = 1;

fadeEffect.target = this;

// Load 1st image

loadMyImage();

}

 

private function fadeComplete(e:EffectEvent):void

{

// fade complete -

if (fadeEffect.alphaTo == 1)

{

fadeEffect.alphaTo = 0;

fadeEffect.duration = FADE_IMAGE_SECONDS * 1000;

fadeEffect.play();

}

else

{

removeChildAt(0);

// load next image

if (index > MAX_IMAGES) {

index = 1;

}

loadMyImage();

}

 

}

 

function loadMyImage():void

{

var urlReq:URLRequest = new URLRequest("images/image" + index++ + ".jpg");

loader.load(urlReq);

}

 

private function imgLoaded(e:Event):void

{

var loader4:Loader = Loader(e.target.loader);

addChild(loader4.content);

fadeEffect.alphaTo = 1;

fadeEffect.duration = SHOW_IMAGE_SECONDS * 1000;

fadeEffect.play();

}

}

 

new Upton();

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...