Game Development Reference
In-Depth Information
Selecting the weapon
Now that we have our command bar with a selection of buttons and items to display, it
would be good if we could actually use it as well.
The pattern I've opted for with this bar is to only make it single select (clicking on another
button will clear any existing selected items). This is just one pattern of course, and you
could update/modify the code in this section as you wish. For now, let's see what's in-
volved in managing this selection and also inform the battle manager that something has
been selected.
In brief, what we will do is the following:
• Use the OnMouseDown function on a command button to detect a user's click
• This will cause a message to be fired to tell the BattleManager state that a
button has been clicked
• The BattleManager state will then take whichever inventory item has been
selected and use it as an active weapon
• If the user clicks on another button, this will override the previous selection, even
if it is empty
So let's see what's involved in doing that.
Selecting a command button
To complete the story of the button, we need to add a couple of handlers to let it be
clicked; change its selection state, the button's sprite, and a function to safely clear the
button's selection state.
First, we'll add the UpdateSelection function to the CommandButton script:
void UpdateSelection()
{
var renderer = gameObject.GetComponent<SpriteRenderer>();
renderer.sprite = selected ?
commandBar.SelectedButtonImage :
commandBar.DefaultButtonImage;
}
Search WWH ::




Custom Search