Hi guys!
You were so helpful last time, Im hoping I can get the same result this time!
Im still making the banner but have come across a problem with my shake variable. When my user holds their mouse over an animal it shakes, this happens by using a timer event and some math random equations. My problem is though, when my user clicks on the monkey and it takes them to the next frame, he continues to shake when the mouse is put over him, whereas none of my other animals when clicked have this problem (ultimately I want none of them to shake once they have been clicked and go to a separate scene). I’ve tried copying and pasting my copy from the other animals (without the problem) and changing it back to the monkey values but alas, the monkey still shakes.
Can anyone tell me what Im doing wrong, it’s the wheres wally of code problems!
Thanks!
Code:
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;
stop();
//Stops the frame from running more than once.
elephant_mc.buttonMode = true;
//When the mouse is positioned over the elephant, the cursor will be a hand.
giraffe_mc.buttonMode = true;
//When the mouse is positioned over the giraffe, the cursor will be a hand.
monkey_mc.buttonMode = true;
//When the mouse is positioned over the giraffe, the cursor will become a hand.
var timer: Timer = new Timer (10);
var positionX:Number = monkey_mc.x = 451.70;
var positionY:Number = monkey_mc.y = 45.5;
monkey_mc.addEventListener(MouseEvent.MOUSE_OVER, startShake);
function startShake(event:MouseEvent): void
{
timer.start();
}
monkey_mc.addEventListener(MouseEvent.MOUSE_OUT, stopShake);
function stopShake(event:MouseEvent): void
{
timer.stop();
monkey_mc.x = 451.70;
monkey_mc.y = 45.5;
monkey_mc.rotation = 0;
}
timer.addEventListener(TimerEvent.TIMER, shakeImage);
function shakeImage(event:Event):void
{
monkey_mc.x = positionX + getMinusOrPlus() * (Math.random()*5);
monkey_mc.y = positionY + getMinusOrPlus() * (Math.random()*5);
monkey_mc.rotation = getMinusOrPlus() * (Math.random()*5);
}
function getMinusOrPlus():int
{
var ran : Number = Math.random() * 2;
if (ran < 1)
{
return -1;
}
else
{
return 1;
}
}
monkey_mc.addEventListener(MouseEvent.CLICK, interestingFact);
function interestingFact(event:MouseEvent): void
{
gotoAndStop(“monkey_fact”);
}
//The following code follows the exact same principles as the above
//code but applies to when the user interacts with the giraffe vector image.
var timer_2: Timer = new Timer (10);
var postX:Number = giraffe_mc.x;
var postY:Number = giraffe_mc.y;
giraffe_mc.addEventListener(MouseEvent.CLICK, interestingFact_2);
function interestingFact_2(event:MouseEvent): void
{
gotoAndStop(“giraffe_fact”);
}
giraffe_mc.addEventListener(MouseEvent.MOUSE_OVER, startShake_2);
function startShake_2(event:MouseEvent): void
{
timer_2.start();
}
giraffe_mc.addEventListener(MouseEvent.MOUSE_OUT, stopShake_2);
function stopShake_2(event:MouseEvent): void
{
timer_2.stop();
giraffe_mc.x = postX;
giraffe_mc.y = postY;
giraffe_mc.rotation = 0;
}
timer_2.addEventListener(TimerEvent.TIMER, shakeImage_2);
function shakeImage_2(event:Event):void
{
giraffe_mc.x = postX + getMinusOrPlus_2() * (Math.random()*5);
giraffe_mc.y = postY + getMinusOrPlus_2() * (Math.random()*5);
giraffe_mc.rotation = getMinusOrPlus_2() * (Math.random()*5);
}
function getMinusOrPlus_2():int
{
var ran_2 : Number = Math.random() * 2;
if (ran_2 < 1)
{
return -1;
}
else return 1;
}
var timer_3:Timer = new Timer(10);
//The timer has one parameter (10), this means the timer will
//be dispatched every ten milliseconds.
var posX:Number = elephant_mc.x;
//Setting a variable to store the elephants current position on the x axis.
var posY:Number = elephant_mc.y;
//Setting a variable to store the elephants current position on the y axis.
elephant_mc.addEventListener(MouseEvent.CLICK, interestingFact_3);
function interestingFact_3(event:MouseEvent): void
{
gotoAndStop(“elephant_fact”);
//When the the elephant image is clicked, the user
//is taken to a seperate frame.
}
elephant_mc.addEventListener(MouseEvent.MOUSE_OVER, startShake_3);
/* Creating a mouse event which listens for when the mouse is over
the image. When the mouse is, the function “startShake” will begin.*/
function startShake_3(event:MouseEvent): void
//void = not returning a value.
{
timer_3.start();
//Starting the timer, which will run every 10 milliseconds.
}
elephant_mc.addEventListener(MouseEvent.MOUSE_OUT, stopShake_3);
/* Creating a mouse event which listens for when the mouse is taken
away from the image and stops the timer and the shaking. */
function stopShake_3(event:MouseEvent):void
{ timer_3.stop();
elephant_mc.x = posX;
//Takes the elephant back to its original position, that was
//defined in the above varaibles.
elephant_mc.y = posY;
//Taking the elephant back to original y axis position.
elephant_mc.rotation = 0;
//Setting the elephant back to the original position.
}
timer_3.addEventListener(TimerEvent.TIMER, shakeImage_3);
//Listening for when the timer is started.
function shakeImage_3(event:Event):void
//This function gives the commands to shake the animals.
/The following code and equations were taken and used from the tutorial:
“How to create the Shaking Effect” [n.d], Accessed 17/4/2011:
http://www.riacodes.com/flash/shaking-effect/#more-365
Modifications have been made to make the tutorial suitable to my objective/
{
elephant_mc.x = posX + getMinusOrPlus()*(Math.random()5);
//This will set the x axis by using the current x position,
//coupled with the math.random class which will use a random
//number to complete the equations, this will make the shake
//look different everytime the timer runs through.
elephant_mc.y = posY + getMinusOrPlus()(Math.random()5);
elephant_mc.rotation = getMinusOrPlus() Math.random()*6;
//Setting the rotations of how the image will shake.
}
function getMinusOrPlus_3():int
//Returning an int value for the function shakeImage.
{
var ran_3 : Number = Math.random()*2;
if (ran_3 < 1)
{
return -1;
}
else return 1;
}