Flash Example: AS3 make a cup vibration animation

Source: Internet
Author: User
Tags rand

The production process is as follows:

First prepare a picture as the background, as follows:

Then create a new layer, import a teapot material, then turn it into a movie clip, and then name the instance.

There are currently two layers, and then we create a new layer with the action, the code is as follows:

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

shaker_mc.buttonMode = true;

shaker_mc.addEventListener(MouseEvent.ROLL_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;
}

Try the effect!

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.