Game Development Reference
In-Depth Information
Finally, to complete the initialization, we need to set up the available buttons based on the
settings for the command bar. This is done in three parts:
• Looping through the columns and rows for the command bar
• Creating new command bar buttons for each position
• Aligning the display position for each button
Note
Like with the inventory system, there is a cooperative relationship between the Com-
mandBar class and its command bar buttons. So, the code won't be compiled until we are
done. Bear this in mind as we progress.
First we need to be able to create a button, so add the following function:
CommandButton CreateButton()
{
// Create our new game object
GameObject go = new GameObject("CommandButton");
// Add components
go.AddComponent<SpriteRenderer>();
go.AddComponent<BoxCollider2D>();
go.transform.parent = transform;
// Init
CommandButton button = go.AddComponent<CommandButton>();
button.Init(this);
return button;
}
This function performs the following:
• Creates a new empty game object (and names it CommandButton )
• Adds two game components ( SpriteRenderer2D and BoxCollider2D )
that are required for the CommandButton game object to function; more on this
later
• Makes the button a child of the command bar by setting its parent transform
Search WWH ::




Custom Search