Offline
Philly, PA, USA

so i have a flash animation that i'm working with,
and i want to be able to have one section of it loop until i press a key or click the mouse or something
i have a

gotoAndPlay(1);

to do the loop, and then I've tried several things, right now, in a separate layer from the loop, i have

import flash.events.MouseEvent;

addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void
{
    gotoAndPlay(47);
}

to try and make it jump to the next portion

i don't really know what i'm doing so any help would be appreciated

Offline
Finland
pixls wrote:

so i have a flash animation that i'm working with,
and i want to be able to have one section of it loop until i press a key or click the mouse or something
i have a

gotoAndPlay(1);

to do the loop, and then I've tried several things, right now, in a separate layer from the loop, i have

import flash.events.MouseEvent;

addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void
{
    gotoAndPlay(47);
}

to try and make it jump to the next portion

i don't really know what i'm doing so any help would be appreciated

In AS3 you need to have instance names (bottom left corner on preferences of movieclip) on movieclips.

So If you have a movieclip with your animation in it and you would name it animation1 or something:

import flash.events.MouseEvent;

animation1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void
{
    animation1.gotoAndPlay(47);
}

oh, and put that code on root-level of your .fla, it's easier to modify it later that way and easier to refer on movieclips inside other movieclips etc:)

If you dont have your animation on movieclip then you would need to do a invisible button over your animation and refer to the buttons instance name on your code.

Last edited by DKSTR (Jan 12, 2010 8:31 am)

Offline
Philly, PA, USA

thanks for your help, but i think i'm totally inept
so, naturally i started something completely different!
i now have on a layer separate from everything else
a big invisible square called "button"
and in that layer the following:

import flash.events.MouseEvent;

button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function get buttonDown():Boolean 
function set buttonDown(value:Boolean):void
{
if (buttonDown) {
    static.gotoAndPlay(47);
}
else {
    gotoAndPlay(1);
}
}

i'm getting and error "1126: Function does not have a body." for  the line

i might be doing something completely wrong, but i have no idea.

Offline
Finland

Hmm..

I meant

that you have for example three layers in your animation

1. a _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2. b u t t o n _ _ _ _ _ _ _ _ _ _
3. u r l o o p _ _ _ _ _ _ _ _ _ _ _

1. your actionscript layer (a marking the first frame where you put your code)
2. your invisible button that is equal to the frames of your looping animation (NOTE: make your button as movieclip, it doesnt matter in AS3)
3. your animation

name your button-movieclips instance name as button1 or something and just paste this code:

import flash.events.MouseEvent;

animation1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void
{
    animation1.gotoAndPlay(47);
}

And bam, it works. I just tested it in like 5 minutes. Your second piece of code looks really complicated and I dont really understand it. Then again, I'm graphic designer and only do this when I have to:)

Offline
▐▐▌▌▐▌▌█▐ ▐▐▌▌▐▌▌█▐ ▐▐▌▌▐▌▌█▐

ok,
so lets assume you have a movieclip called "SomeMovieClip"
that is 20 frames total. and you want it to play frame 1-10
before the click, and 11 = 20 after the click, and you want the
click event to toggle the boolean, this code *should* work...
(i wrote this in the comment box, so i haven't tested it)

var clicked:Boolean = false;
var mc:MovieClip = new SomeMovieClip();
mc.x = mc.y = 100;
mc.addEventListener(MouseEvent.CLICK, onClick);
addChild(mc);

this.addEventListener(Event.ENTER_FRAME, loop);

private function onClick(e:MouseEvent):void {
   if (clicked) { 
     clicked = false;
   } else {
     clicked = true;
}

private function loop(e:Event):void {
  if(!clicked) {
     if(mc.currentFrame > 10) {
         mc.nextFrame();
     } else {
         mc.gotoAndStop(1);
     }
  } else {
     if(mc.currentFrame < 21) {
         mc.nextFrame();
     } else {
         mc.gotoAndStop(11);
     }
  }
}

thinking about the code i just posted,
yew can actually to the same thing faster
and more efficiently with this code...

var clicked:Boolean = false;
var mc:MovieClip = new SomeMovieClip();
mc.x = mc.y = 100;
mc.play();
mc.addEventListener(MouseEvent.CLICK, onClick);
addChild(mc);

addEventListener(Event.ENTER_FRAME, loop);

private function onClick(e:MouseEvent):void {
   //--inline conditionals FTW!
   clicked = (clicked) ? (false) : (true);
}

private function loop(e:Event):void {
  if(!clicked) {
     if(mc.currentFrame == 10) {
         mc.gotoAndPlay(1);
     }
  } else {
     if(mc.currentFrame == 20) {
         mc.gotoAndPlay(11);
     }
  }
}

so what's the difference?
well the onClick function now uses an inline conditional
verses the classic if/else statement (a bit faster + less code).
also im letting flash handle the animation as opposed to
letting our code to the logic. in the first example in the
enterframe loop, im checking the movieclips current frame
then incrementing or resetting it. while in the second
example im just checking the current frame. when the
check condition fails THEN i take action, as opposed to
taking action on every frame loop. will that make a huge
difference? not in this example. but if you were doing this
many many times per frame it would start to...

i hope that helps!

Last edited by xero (Feb 2, 2010 7:30 pm)