ActionScript moves components using the keyboard

Source: Internet
Author: User

This article describes the way ActionScript2.0 uses the keyboard to move components.

First, basic preparation

1, first, open flash, create a new ActionScript2.0 file. After the new is created directly, such as "ActionScript" using copy movie Duplicatemovieclip and mouse drag follow StartDrag to make mouse movement effect (click to open the link), by inserting a new component (or ctril+ F8) method, create a new instance of the type: Movie clip. Then in component 1, for example, using the Rectangle tool, such as dragging a rectangle, using the selection tool, select it, then set its position and size, here is the size of 100x100px that is, the width of the height is 100, and X, Y is the opposite of the width of the 1/2, that is, 50, so that it is centered.


2, then cut back to Scene 1, point open the Library panel, or press F11, drag into the newly created good movie clip, for example, in the property panel, set its instance name square, and then position and size, respectively, x:275,y:200, so that it is located in the center of scene 1, width and height with the width of the movie. After that, using the Text tool, in Scene 1, drag out a text, set it to dynamic text, the position and size of x:0,y:0, so that it is on the left, the width is 400, the height is 50, the font size is 40, anti-aliasing to use the device font, so as not to compile the warning. Enter the characters "x:275,y:200" in the inside. After that, in the variable name, set its variable name to text. Control the script for a while.


3, after, for example, different from the previous several articles, this time is the rectangle of the movie clip, right click, select Action, set the script on it.


Second, script programming

The task of ActionScript is to set the motion of this movie clip, which is applied when the keyboard is pressed. As follows:

Onclipevent (KeyDown) {//When the keyboard is pressed to trigger this function var intcode=key.getcode ();//Get keyboard key code var intx=getproperty ("_root.square", _x) ;//Gets the X, y-coordinate of the square var inty=getproperty ("_root.square", _y); _root.text= "X:" +intx+ ", Y:" +inty;//assigns coordinates to dynamic text if (intcode== PNS and INTX>50) {//if it is ← and the square is not to the left border, move the rectangle to the left by 3 pixels, and then setproperty ("_root.square", _x,intx-3);} if (intcode==38 and inty>100) {setProperty ("_root.square", _y,inty-3);} if (intcode==39 and intx<500) {setProperty ("_root.square", _x,intx+3);} if (intcode==40 and inty<350) {setProperty ("_root.square", _y,inty+3);}}
With GetProperty, you can get the coordinates of a movie clip, SetProperty can set the coordinates of the movie clip, _x and _y represent the properties of the setting.

The above script does different actions on the movie clip depending on which keyboard keys are pressed.

Since there are scenes and movie clips here, you must start with _root to indicate that this is something in the scene, not a movie clip.

Although the size of this Flash movie is 550x400px, it calculates the bounding coordinates to subtract half the size of the movie clip. Because the coordinates of the movie clip are calculated by the center.

After compiling by Ctrl+enter, we get the following effect:


ActionScript moves components using the keyboard

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.