| How
to make your own behaviours
Provided by Adam
Montandon
You will need:
Basic understanding of
lingo.
Good understanding of
the director environment.
Director 8 or above.
Introduction
When many students start
out coding lingo, they
often cut-and-paste multiple
copies of the same script,
and attach it to different
sprites, perhaps changing
a variable here and there.
Often, if you want 10
sprites to behave in a
similar way, you may have
to copy and paste the
script, and alter it,
10 times. This is way
too time consuming, so
instead I like to make
my own behaviours.
Behaviours are very easy
to make in director, and
they have many benefits.
Firstly it stops you having
to copy and past frequently
used code. Secondly you
can add them to your own
behaviour library, so
it takes just seconds
to add functional scripts
to your movies. This means
faster code development
time, which its just what
as deadlines approach.
Firstly
I will introduce you to
the spritenum property.
This is a very useful
property, as it allows
sprites to pretty much
take care of themselves.
Introducing the Spritenum
Property
1)
Make a scratch
graphic castmember using
the pain tool.
2)
Drag it in to
sprite channel 1
3)
Now, in frame
one, add the usual code:
On
exitframe
Go
to the frame
(this makes it loop on the frame)
5)
Now, make a new
script:
property
spritenum
on
mousedown
me
Put
"I am in sprite channel"
&& spritenum
end
6)
Make sure that
this new script is set
to be a behaviour, and
give it a name, such as
–Chan display”
7)
Drag and drop
the Chan Display behaviour
icon onto your bitmap
in channel 1 in the score.
Click play.
Click on the graphic on
the stage, you will notice
that the messagewindow
reads -- "I am in
sprite channel 1"
Click stop.
Move your graphic down
the score to channel 4
Click play
Click on the graphic on
the stage, you will notice
that the message window
now reads -- "I am
in sprite channel 4"
Isn't that great? Without
changing any code at all,
your sprite has automatically
changed its behaviour.
How behaviours reduce
code.
Now, make several
more different scratch
graphics and drag and
drop them onto frame 1
of the score, in any sprite
channel.
Next, drag and drop the
same behaviours we just
made onto all of them.
Play the movie and click
on your different graphics.
You will notice that all
the graphics now behave
in a similar way, (they
all output what channel
they are in) but they
do not behave in EXACTLY
the same way, as each
graphic is in its own
unique channel.
Notice how there is no
more code to write for
10 graphics as there is
for 1. This can save a
lot of time!
Now, putting the sprite
channel isn't very interesting,
lets do something cool,
by using User Defined
Properties.
User Defined Properties.
User defined properties
are used in a similar
way to global variables,
but global variables belong
to the whole movie, where
as user defined properties
are –personal” and belong
to that particular sprite.
If we take a group of
human beings, the –global”
name for these might be
–People” or –Humans”.
But within that group
there may be somebody
who's name is –Fred” another
person called –Jeff” and
there may be two people
called –Dave”. There could
even be a person who was
called –Edward” but likes
to be called –Ted”. But
the thing is, the name
belong to the individual.
So, you could have several
rooms full of –humans”
but each room would have
people with different
names in them. The people
themselves can change
their name if they want
to, and if they go into
a different room, their
name is still the same.
Using GetPropertDescriptionList
to make things really
move!
Start a new movie,
and make some scratch
graphics.
Add your:
on
exitFrame
me
go
to the frame
end
script
in frame 1
Drag 3 or 4 of your
graphics onto different places on the screen.
Now, lets write our
new behaviour to get things animated.
We are going to give
or sprite a new user defined property called –Speed” that will tell a
sprite how fast to go. We will also give it the property Spritenum so
that it can refer to itself (I.e. tell itself how fast to go.)
property
spritenum
property
speed
--Next,
we add this great handler:
on
getPropertyDescriptionList
description = [:]
addProp description,#speed, [#default:1,
#format:#integer, #comment:"How
fast should I go?"]
return description
end
Ok, this may look a
little scary if you've never seen this handler before, but don't worry,
its not so hard.
All you really need to care about is the addprop line, that adds your
property to the description list.
#speed is our property name. #Default:1 is the default setting, so if
you don't put in a speed value, it will set it to be 1. #Format:#integer
is the format that the property should be. #integer means that the property
must be a whole number. Instead of just #integer you could have any of
the following: #Format:#integer #Format:#float #Format:#string #Format:#symbol
#Format:#member #Format:#bitmap #Format:#filmloop #Format:#field #Format:#palette
#Format:#picture #Format:#sound #Format:#button #Format:#shape #Format:#movie
#Format:#digitalvideo #Format:#script #Format:#richtext #Format:#ole #Format:#transition
#Format:#xtra #Format:#frame #Format:#marker #Format:#ink #Format:#Boolean
The most useful are
#Format:#integer (whole numbers only) #format:#Boolean (True or false)
#format:#float (a floating point number) and #format:#string (a text string)
The #comment part
#comment:"How
fast should I go?"
is added as an aid so that you know what property you are defining. Whatever
is in the comment string pops up as a question when you use the behaviour
for the first time. The comment is just to remind you what you are doing.
Now,
that's your properties sorted out, let's get on to animation.
Putting Exitframe inside sprites.
Most of you are familiar with putting your exitframe script within
the score. But did you know that every time you –go to the frame” you
pass a message to all the sprites on the stage that says –you are exiting
a frame?” It's really handy, because you can use it as a handler. Add
this code BENEATH your Getproperydescriptionlist code.
on
exitframe
me
sprite(spritenum).loch
= sprite(spritenum).loch
+ sprite(spritenum).speed
end
Notice, there is no
GO TO THE FRAME, that is only used in your score. This script is done
every time the sprite receives the message –you are exiting the frame”
The actual script
is simple, it uses the sprites own loch property (that you should already
be familiar with) and its NEW speed property that we made earlier! Notice
that we use the sprites –speed” in the same way as we use its loch, locv,
visibility, height, width etc. Only we made –speed” ourselves, rather
than using something built in.
Now, making sure your
script is set to behaviour, drop and drag it onto a graphic in the score.
You will get a pop up window saying –How fast should I go?” (That's the
comment from earlier.) Input a number, 3 is a good one to start with,
and click OK.
Now, drop and drag
your behaviour onto lots of other graphics in the score, one by one, each
time putting a different value for its speed. Try low numbers for slow,
high numbers for fast, even negative numbers to go backwards.
Run your movie!
If it has all worked
you should see your sprites moving around the stage at different speeds!
All with the same piece of code! A great timesaving invention!
Of course, you can
really expand on this; this is only a basic demo. Go and experiment.
|