79556064

Date: 2025-04-04 18:53:47
Score: 0.5
Natty:
Report link

Okay, so eventually the code that finally made a MovieClip to shake is the following.

import flash.display.MovieClip;
import flash.events.Event;

function shake(which:MovieClip, power:Number, count:int = -1):void {
    if (!which) return;
    unshake(which);
    which.preset = {x: which.x, y: which.y, power: power, count: count};
    which.addEventListener(Event.ENTER_FRAME, onShaking);
}

function unshake(which:MovieClip):void {
    if (!which) return;
    which.removeEventListener(Event.ENTER_FRAME, onShaking);
    if (which.preset) {
        which.x = which.preset.x;
        which.y = which.preset.y;
        which.preset = null;
    }
}

function onShaking(e:Event):void {
    var which:MovieClip = e.currentTarget as MovieClip;
    if (!which.preset) {
        unshake(which);
        return;
    }
    var count:int = which.preset.count;
    if (count > 0) {
        count--;
        if (count == 0) {
            unshake(which);
            return;
        }
        which.preset.count = count;
    }
    var power:Number = which.preset.power;
    which.x = which.preset.x + Math.random() * power - power / 2;
    which.y = which.preset.y + Math.random() * power - power / 2;
}

//For insertNamehere, change it to the name of the MovieClip in the properties panel

shake(insertNamehere, 10, 100);

//Thanks again Organis, you are a great help!
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Johnson Yan