In this final part of my series of Phaser Form UI, we will look at adding drop downs to the form. I originally came up with this for adding a form to report abuse in a virtual world. It can also be useful for choosing a range of options when starting a game, especially if there are too many to display with buttons or other graphics. <\/p>\n\n\n\n
This project builds on the code written in the other 2 blog post of this series. Adding input text <\/a>and adding HTML buttons<\/a>.<\/p>\n\n\n\n First let’s add a drop down to the index.html. Place this at the end of the HTML just above the <\/body> tag<\/p>\n\n\n\n We can place and scale the optList drop down using the functions we built before. Place these at the end of the create function in SceneMain<\/p>\n\n\n\n Our FormUtil will need some new functions to deal with drop downs. The first being to add an option. We will pass in the id of the drop down, the text to display and an object that can contain data for the item. The code will get the drop down From Phaser we can add options to the drop down like this<\/p>\n\n\n\n We can use the functions already written to add a change callback<\/p>\n\n\n\n Then we need to be able to get information that is selected. There are 3 different functions we can create for this.<\/p>\n\n\n\n Get the selected data<\/p>\n\n\n\n Get the selected index, such as 0,1,2,3<\/p>\n\n\n\n Get the text displayed in the dropdown<\/p>\n\n\n\n Then all that’s left is to create the function that we referenced earlier.<\/p>\n\n\n\n Since the Here is the final result<\/p>\n\n\n\nAdding the drop down to the html<\/h2>\n\n\n\n
<select class=\"form-control\" id=\"optList\" name=\"selectbasic\">\n \n <\/select><\/code><\/pre>\n\n\n\n
this.formUtil.scaleToGameW(\"optList\", .8);\nthis.formUtil.placeElementAt(27, \"optList\");<\/code><\/pre>\n\n\n\n
Adding new functions to formUtil<\/h2>\n\n\n\n
addOption(dropDown, text, item) {\n var select = document.getElementById(dropDown);\n var option = document.createElement('option');\n option.text = text;\n option.data = item;\n select.add(option, 0);\n }<\/code><\/pre>\n\n\n\n
this.formUtil.addOption(\"optList\", \"Wizard\", {\n id: 1,\n magic: 10,\n str: 5\n });\n this.formUtil.addOption(\"optList\", \"Elf\", {\n id: 2,\n magic: 6,\n str: 6\n });\n this.formUtil.addOption(\"optList\", \"Warrior\", {\n id: 3,\n magic: 1,\n str: 10\n });<\/code><\/pre>\n\n\n\n
Listen for Events<\/h2>\n\n\n\n
this.formUtil.addChangeCallback(\"optList\", this.optListChanged, this);<\/code><\/pre>\n\n\n\n
getSelectedItem(dropDown) {\n var e = document.getElementById(dropDown);\n return e.options[e.selectedIndex].data;\n }<\/code><\/pre>\n\n\n\n
getSelectedIndex(dropDown) {\n var el = document.getElementById(dropDown);\n return el.selectedIndex;\n }<\/code><\/pre>\n\n\n\n
getSelectedText(dropDown) {\n var e = document.getElementById(dropDown);\n return e.options[e.selectedIndex].text;\n }<\/code><\/pre>\n\n\n\n
Callback function<\/h2>\n\n\n\n
optListChanged() {\n console.log(\"optListChanged\");\n var obj=this.formUtil.getSelectedItem('optList');\n console.log(obj);\n\n }<\/code><\/pre>\n\n\n\n
Adding Labels<\/h2>\n\n\n\n
var label=this.add.text(0,0,\"Character name\",{fontSize:game.config.width\/20,color:'#ffffff'});\n label.setOrigin(0.5,0.5);\n this.formUtil.alignGrid.placeAt(5,0.5,label);<\/code><\/pre>\n\n\n\n