Getting the most from Fusion's Behaviours
What are Behaviours?
Behaviours are self-contained lists of events, specific to an individual object in Fusion. You can add one or more behaviours in an object's Properties by clicking the tab. This allows you to program what an object will do, for example when you move the mouse over it you might animate it so in this way you can create objects which can be dropped into a frame to just function, without needing to add anything in the frame's main events.
The applications are endless...
- Automated buttons which react to the mouse (as we will show below).
- Objects with complex and self-contained movement or behaviours
- Preprogrammed functions or tools which can be dropped into your game/application, for example you could add behaviours to an edit box to always check the content when a user types in it, and automatically capitalise the first letter, or remove non-numbers and so on.
- For objects which can be shared globally in your application (eg. the active object), you can add behaviours to these which will then be shared between all frames in which that object is present. This could be useful in a player character for designing its movement routines, or interaction with enemies etc. so that when you come to add to these routines or fix bugs, those changes are reflected throughout your game/application, making code reuse possible.
Proper use of behaviours in a complex project can dramatically reduce the number and the complexity of the main list of events. We are going to discover behaviours by creating an automated button which highlights when the user clicks on it with the mouse.
- Download the behaviours.mfa file from the bottom of this guide.
- Open Fusion and load the mfa file where you will see an object named "Button".
- This button contains two images, the button in an unpressed state (direction number 16, left on the direction selector) and the depressed button (direction number 0 or right). So, if we want to press/release the button, we simply should change the direction of the button to the right or to the left, when the user clicks or releases on it.
|Direction 16||Direction 0|
- Click on the "Button" object to select it, which will display its properties in the Properties toolbar. In the Properties tabs, click on the Events tab icon (). From the Events tab, next click the "New" button.
- This will create a new Behaviour named "Behaviour #1". You can rename this to suit your requirements, perhaps "Button Click" in this instance. Click on the edit property area to the left of "Behaviour #1" where you see an ellipsis (...) which will open the Behaviour Editor. This looks almost identical to the Event Editor, except that it will contain only this specific set of events for this object, and far fewer objects (if you have more added to your frame).
- Enter the following events:
As well as making your events nice and tidy by attaching related events to the object rather than in the main event list, Behaviours allow you to reuse your code throughout your application. Certain objects including the active object allow you to configure them as global objects. This means that they are shared throughout the application, including their behaviours. You could even use this to create more event containers. This might include an invisible active object shared globally, which you drop into a frame and contains events to draw a level, or to spawn enemy characters. This has the enormous advantage that if you edit it in one place, it will change everywhere - especially useful if you use a frame per level!
Did you notice the little icon which appeared in the corner of the button icon? This shows that you have attached a Behaviour to this object.
Some more things to remember about Behaviours...
- They are a list of events, stored in the object itself rather than the main event list.
- Objects in the Behaviour event editor are shortcuts to objects in the main frame. You will notice that only the special objects and the object to which you attach the behaviour are visible in the editor. More objects can be imported to the behaviour by clicking the import button on the far right of the object list (depending on your version of Fusion, this looks like one of these: )
- Sometimes you will have imported objects into a behaviour and then copied the object with this behaviour to another frame, where the imported objects do not exist. This is not a problem! Fusion will simply ignore any actions, conditions or events references to these missing imported objects. In this way, you can add events inside your behaviours, especially for global use, which perform differently depending on they objects of the frame they are in.
- Taking the previous point a step further into a real-world scenario - if for example you shot an imported active object from inside a behaviour and then copied the original object with this behaviour to a new frame, unless you ensured that the object being shot exists in the second frame, nothing will be shot. This is an important thing to remember as many people do forget and wonder why their behaviours are not working properly.
- Behaviours can be copied and pasted, or dragged and dropped, between objects of the same type. So you could for example paste the behaviours from a player one character to a player 2 character. This creates a new copy of the behaviour on the second object so any subsequent changes to the first object's behaviour will not then be reflected on the second one. This is important to remember as unlike behaviours which can be shared globally on the same object, they cannot be shared between objects.
- If you are using Multimedia Fusion 2, when accessing the Alterable Values for objects from inside a behaviour, the names of the Alterable Values or Strings will be shown as A, B, C, D, E... and so on. This is something which has been addressed in Fusion 2.5.
- Similarly, in Multimedia Fusion 2, if you have created any custom animations for an active object, only the first 12 default ones will be shown. Any additional animations can be referred to via an expression from the relevant box by clicking the "Use a calculation" button and entering 12 or higher (0 to 11 are the default animations such as Stopped, Running, Jumping etc.). Again, this issue has been addressed in Fusion 2.5.
Spread the word!
You can share this document using the following buttons.