Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
adv banner
Flash & Swish  Home Flash & Swish Flash Tutorials Crosshair Targetting
rss

Crosshair Targetting

Author: Pipey.com More by this author


Download Source

You've probably seen this in a lot of shooting games. A crosshair follows the mouse around the screen. This tutorial demonstrates how to make horizontal and vertical lines follow the cursor.

Start a new movie.
Draw a horizontal line. Convert this to a Movie Clip symbol (F8).
Draw a vertical line and convert this to a Movie Clip as well.
Then draw a pair of concentric circles. Select both circles and convert these to a third Movie Clip.



Next, edit the properties of each symbol in turn (right-click on the symbol and choose Properties).
Give the symbols the Instance Names shown in the picture below.



Now Rename the layer containing all the graphics (call it 'Graphics'). You can rename a layer by double-clicking on its name on the right of the timeline.
Add another layer below this and give it the name 'Actions'. This is where the Actionscript is going to go.



Add the following actions to the keyframe at frame 1 of the 'Actions' layer.

Start Drag ("/Target", L=0, T=0, R=400, B=300, lockcenter)
Set Property ("/VLine", X Position) = GetProperty("/Target", _x)
Set Property ("/HLine", Y Position) = GetProperty("/Target", _y)

The first line starts a drag on the circles, causing it to follow the mouse.
Choose the 'Constrain to Rectangle' option, and insert values which fit the size of your movie (or an area where you want to confine the crosshairs).
My movie is 400x300, so I chose those values to allow the crosshairs to move around the entire movie stage.
The remaining two lines reposition the two lines so that they are lined up across the circles.



The next step is to add a second frame to both layers on the timeline. Insert a second keyframe (F6) at frame 2 in the 'Actions' layer.
Put in the following code:

Go to and Play (1)

This causes the movie to loop back to the previous frame (so that the positions of the crosshairs are continuously updated).



And that's all there is to it!
One quick note: you might find that the default frame rate of 12fps is too slow for this. A higher rate such as 40fps will reduce any jerkiness.

The result should look something line the example below:



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Crosshair Targetting"