Game Development Reference
In-Depth Information
Why?
Note that this new private variable isn't typed. This is a funky reality of
JavaScript—the type does not have to be defined. It can usually help in
performance, but in this case where we're accessing a particular script, we can
leave the type blank, which is often the practice when working with scripts.
Step 20: Create a new OnMouseDown function that starts setting
Booleans via this script. Now that the script is going out and finding the
script, start passing commands to it:
function OnMouseDown (){
if (name == "InventoryButton_EMP"){
toolFunctionality.flashlightActive = false;
toolFunctionality.pistolActive = false;
toolFunctionality.empActive = true;
}
if (name == "InventoryButton_Pistol"){
toolFunctionality.empActive = false;
toolFunctionality.flashlightActive = false;
toolFunctionality.pistolActive = true;
}
if (name == "InventoryButton_Flashlight"){
toolFunctionality.empActive = false;
toolFunctionality.pistolActive = false;
toolFunctionality.flashlightActive = true;
}
}
Why?
That looks like a lot of script, but it's a pretty straightforward concept. It says,
”when the user clicks on this object (function OnMouseDown), check if this
object's name is InventoryButton_EMP. If it is, go to toolFunctionality (which
we just defined as the component (script) attached to mainCamera) and
there, set the Boolean flashLightActive to false, set the boolean pistolActive
to false, and set the Boolean empActive to true. If the object clicked is not
InventoryButton_EMP, then check if it is InventoryButton_Pistol and if it is…”
and so on. There are more elegant solutions as well (take a look at Switch
Statements or Singletons); but this one is particularly easy to use.
Firing Animations in Script
So far we've got the InventoryButtonScript so that it can go talk to the mini-
state-engine included in AC_ToolFunctionalityScript and turn on and off
Booleans contained there. But before this works really smoothly, we need
to do a bit of additional beefing up of the functions that are included in
AC_ToolFunctionalityScript.
Search WWH ::




Custom Search