Game Development Reference
In-Depth Information
Now that the program knows what the distance is between the two points, you simply need to set up
an eb/ahoa statement to move the object when the distance is greater than 1 pixel, and stop moving
it by removing the kjAjpanBn]ia event listener if it's less than 1 pixel:
eb$`eop]j_a:9-%
w
lh]uan*t'9$[p]ncapT)lh]uan*t%&A=OEJC7
lh]uan*u'9$[p]ncapU)lh]uan*u%&A=OEJC7
y
ahoa
w
pn]_a$Lh]uanna]_da`p]ncap%
naikraArajpHeopajan$Arajp*AJPAN[BN=IA(kjAjpanBn]ia%7
y
Easy easing!
Following the mouse with a bit of delay
Another little twist to the code will give you an object that follows the mouse with a slight bit of drag,
as if it's being pulled by a spring. In the chapter's source files, find the folder called IkqoaBkhhks and
open it as a project. Test it to see the effect.
The code is very similar to the previous example, except that the ikqoaT and ikqoaU target variables
are updated directly in the kjAjpanBn]ia event handler. This makes the object update its destination
every time the mouse moves. It's a very organic and pleasing effect, and you're sure to find this useful
for the basis of many games:
l]_g]ca
w
eilknpbh]od*`eolh]u*Ikrea?hel7
eilknpbh]od*arajpo*Arajp7
eilknpbh]od*arajpo*IkqoaArajp7
lq^he__h]ooI]ej[IkqoaBkhhksatpaj`oIkrea?hel
w
++@a_h]na_kjop]jpo
lner]pa_kjopA=OEJC6Jqi^an9,*-7
++@a_h]nar]ne]^hao
lner]par]n[p]ncapT6Jqi^an7
lner]par]n[p]ncapU6Jqi^an7
lner]par]n[rt6Jqi^an7
lner]par]n[ru6Jqi^an7
 
Search Nedrilad ::




Custom Search