Basic Flash ActionScript for Designers - Buttons, Movie Clips, and Making them Draggable
(Page 2 of 4 )
Buttons and Movie Clips
Buttons can be used to control movies. To accomplish this, you could directly attach actions to the button or movie clip with the on() function. Alternatively, you could write actions in the Timeline using event handler functions with instance names of button or movie clips. This second method is a better practice becuause it handles all your code at one common place instead of spreading it out over all buttons and clips.
See the example below:
// Event handler function for button with instance name
// “button1_btn” which executes the code inside the
// function when the button is pressed
button1_btn.onPress = function(){
anim_mc.gotoAndPlay(5);
}
You can use other events in this way as well, such as onRelease, onRollOver, onDragOver, onKeyUp, etc. They also work on movie clips and their instance names.
Dragging Movie Clips
You can use the global startDrag()function or the MovieClip.startDrag()method to make a movie clip draggable. This would work with games, customizable interfaces, scroll bars, etc. A movie clip remains draggable until explicitly stopped by stopDrag() method or until another movie clip is targeted with startDrag() method. Only one movie clip at a time can be dragged in a movie.
Let us see an eample below:
// Starts dragging when “anim_mc” is pressed
anim_mc.onPress = function() {
anim_mc.startDrag();
};
// Stops dragging when “anim_mc” is released
anim _mc.onRelease = function() {
anim_mc.stopDrag();
};
startDrag() has two optional set of parameters that allow you to control the dragging functionality. The first is a boolean true or false parameter, which allows you to lock the dragging to the centre of the movie clip. Using "false" doesn’t lock the dragging to center, and "true" locks it to centre. The default is false.
anim_mc.startDrag(false);
The second set of parameters allows you to control the dragging to a specific area of your movie. You should specify 4 values (left, top, right, bottom) based on your movie size.
anim_mc.startDrag(false, 0, 0, 200, 400);
Next: Navigating, URLs, and Loading Images >>
More Flash Articles
More By Adi Reddy Mora