Imagine that you have to move an object in a straight line.
No problem: in enterFrame you put something like
this._x+=this.speed and the object moves.
It gets a bit
more complicated when you need the object to follow some sort
of a path. To solve problems of that kind they have been using
MotionTween for a good while now. Unfortunately, this
solutiton does not always work exactly the way the developer
needs.
- what to do if the speed of the object has to change
depending on the user's action? - what to do if the actual
path has to change dynamically? - is it possible to
recalculate the object position on the changing path without
big math knowledge? Anyway, if I am asking these questions
it probably means that I might know the answers to them:) Read
on.
A few simple steps towards our goal.
1. Creating changing path.
Nothing difficult. Put a few movies together, create a class
for them, render the result using movies as control points.
(FLA):
- sort of waves on a sea:).
2.Making a ship. Nothing difficult
again. On the event onEnterFrame move the object horizontally,
which is controlled by user's action. . E.g. by pressing
left-right arrows. If it doesn't work with the example given
below click it as it has to be in the focus. Have a look at FLA
if something is not clear:
3. Usin Path in the end. Nothing
complicated. Be aware though, that it is not a lesson on AS2 If
you are not sure how to use AS2 class I advise that you look up
info on the net first. So, as we have a constantly changing
path in onEnterFrame, we have to create an example of the class
Path using the coordinates of the movies that create the actual
object path as arguments. Instead of direct positioning the
movies we feed the calculated variable x to method getPoint of
the example of the class Path. This method calculates and
returns a point coordinate on the path which is located on the
curve in a given distance. Even more than that. It returns the
angle of a tangent to the curve in this point. Then we take all
that and assign it to out ship parametres. Of course, looking
at FLA, to
understand how it is done. The result:
Suddenly, when testing we find an unexpected problem: the
length of the path is changing all the time. The longer is the
way along the curve from the very left to the ship the closer
it is to the start of x axis. It has to be fixed.
4. Moving over to relative system of position
calcualtion. There is nothing extraordinary.
Instead of simple x calcualtion we find the position of the
ship as the percentage of the made distance. Then we calcualte
the absolute position on the path multiplying the length of the
path and dividing by 100. Have a look at FLA
and the result:
That would do for demonstration of Path advantages. But.
5. Adding a bit of realism. The ship
does not react ot the waves. It goes straight like a tank
regardless of going up an incline or down. It is worth changing
it. Have a look how I did it in FLA.
- the simplest way is the best way. I add to the length of
the gone distance one hundredth of a rotation.
6. Making a picture of unreal beauty. To
do that: - bring the ship out of the movie into the root so
that the ship would be in the background beyond the waves. -
set the end points outside the visible field to get rid of
glitches. - change the wave appearance see the result and
go through FLA.
Beautiful! The only thing is that the visitors of my
website did not like the waves at all. My exuses that the waves
are not the point of the work weren't interesting to anyone.
OK.