Moving Objects in ActionScript Using _x and _y

When creating/developing a presentation in Adobe Flash, there may be instances when you may need or want to move certain objects by either keyboard and mouse interaction. Moving objects around in your Adobe Flash presentation can help make your presentation more interactive and more alive to the audience who you are presenting it to.

First, for Flash to recognize the object to be moved, it must be converted to either a movie clip or button. The object cannot stay as a graphic/bitmap. To convert the graphic to one of these two behaviors, click on the object on the stage and press the F8 key. This will open the “Convert to Symbol” window, where the user can designate which behavior they would like to use, depending on its functionality in the presentation.

For the ActionScript in Flash to apply movement to the object, the object will need to be provided with an Instance Name. This can be designated by clicking once on the object on the stage, and entering a one-word name in the “Instance Name” field in the Properties Inspector.

After the object has been converted to either a movie clip or button, and the “Instance Name” has been entered, the object is now ready to be moved by ActionScript code. Please enter the following ActionScript code in the Actions Inspector, modifying the instance name to what you named your object.

To move the object in question by using the arrow keys on your keyboard, click on the object once and open the Actions Inspector. With the Actions Inspector open, enter the following code:

on (keyPress "<Left>") {
	this._x = this._x - 1;
}

on (keyPress "<Right>") {
	this._x = this._x + 1;
}

on (keyPress "<Up>") {
	this._y = this._y - 1; 
}

on (keyPress "<Down>") {
	this._y = this._y + 1; 
}

Please feel free to leave any questions or feedback in the comments section below.

Published
Categorized as Flash

By Chad Sweely

Chad Sweely is a staff writer and Flash Designer for FlashNewz.

Leave a comment