Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

changing an event listener to a frame event

Enthusiast ,
Nov 03, 2009 Nov 03, 2009

I have a code in AS3 that works when you rollover it. I need to use the same code but not with an eventlistener for a mouse event but a frame event when the timeline plays the clip. any help?

var coordX:Number = shaker_mc.x;
var coordY:Number = shaker_mc.y;
var timer:Timer = new Timer(10);

shaker_mc.buttonMode = false;

shaker_mc.addEventListener(MouseEvent.MOUSE_OVER,startShake);
shaker_mc.addEventListener(MouseEvent.ROLL_OUT,stopShake);

timer.addEventListener(TimerEvent.TIMER, shakeImage);

function startShake(e:MouseEvent):void{
   timer.start ()
}

function stopShake(e:MouseEvent):void
{
   timer.stop();
   shaker_mc.x = coordX;
   shaker_mc.y = coordY;
   shaker_mc.rotation = 0;
}

function shakeImage(event:Event):void {
   shaker_mc.x = coordX+ getMinusOrPlus()*(Math.random()*5);
   shaker_mc.y = coordY+ getMinusOrPlus()*(Math.random()*5);
   shaker_mc.rotation = getMinusOrPlus()* Math.random()*6;
}

function getMinusOrPlus():int{
  var rand : Number = Math.random()*2;
   if (rand<1) return -1
   else return 1;
}

TOPICS
ActionScript
728
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 03, 2009 Nov 03, 2009

hmmm - well i'm not quite certain what you're looking for exactly, do you mean that you would like it triggered when a specific frame or label is hit? in this case one could monitor the frame number using an EnterFrame event listener, and a condition checking currentFrame or currentLabel properties.

another thought here is that you could use the addedToStage event, so that when shaker_mc is instantiated it trigger the shake.

and yet another methodology would be to use an event broadcaster placed directly on the frame you wish to use as a trigger, then add a listener to the clip that invokes the handlers. for instance, on the frame you wish to 'start' the shake, in shaker_mc use:

dispatchEvent(new Event(Event.INIT));

- and in the original code use the listener assignment:

shaker_mc.addEventListener(Event.INIT, startShake);

- same with the 'stop', on the frame you wish to stop the shake use:

dispatchEvent(new Event(Event.COMPLETE));

- and in the original:

shaker_mc.addEventListener(Event.COMPLETE, stopShake);

with the event listeners assigned in this way you could add as many dispatches as you would like as on succesive frames within the MC.

just some thoughts

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 03, 2009 Nov 03, 2009

The mc would instantate from the first frame of the timeline and stop on the 150th. SO I need the clip to instantate from fram 1 to 150.

which is the easiest way to acheive the code?

thx

rde

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 03, 2009 Nov 03, 2009

cool - ok well then perhaps the easiest way to go about this would be to simply call the startShake method when the clip is added, then on frame 150 call the stopShake method.  you could do both from within the shake_mc timeline by calling the method via parent.startShake() & parent.stopShake()

perhaps i'm not following though LOL

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 03, 2009 Nov 03, 2009
LATEST

are you looking to make something shake when you hit it with the mouse as the movieClip plays?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines