- Home /
how to use Gui.Buttons and keyboard ?
my code
static var level2time : float = 0.0/3;
var spiller : GameObject;
var high : int = -100;
var b : int = 75;
var h1 :int = 90;
var h2 :int = 50;
var h3 :int = 10;
function Start () {
level2time = 0;
player.playerScore = 0;
Screen.showCursor = false;
}
function Update () {
if (player.Quit == true){
h1 = 90;
h2 = 50;
h3 = 10;
}else{
h1 = 2290;
h2 = 2250;
h3 = 2210;
}
if (player.Quit == true){
Screen.showCursor = true;
Time.timeScale = 0.0;
}
if ((player.Quit == false)&& player.paused == false){
Screen.showCursor = false;
Time.timeScale = 1.0;
}
if(player.paused == true){
(VolOut());
}else{
(Volin());
}
if(player.playerScore <= 5){
level2time += Time.deltaTime;
}
if(player.playerScore >= 6){
spiller.rigidbody.isKinematic = true;
player.gameover = true;
(VolOut());
if(level2time < PlayerPrefs.GetFloat("level2")){
PlayerPrefs.SetFloat("level2",level2time);
PlayerPrefs.SetString("name2",scoremenu.mainname);
high = 130;
}
LoadLevel(4, 2.5);
}
if(Input.GetKeyDown("m")) {
if(audio.mute)
audio.mute = false;
else
audio.mute = true;
}
}
var Design : GUISkin;
function OnGUI () {
var someString = String.Format("{0:00}:{1:00}", Mathf.Floor(level2time/60), level2time % 60);
GUI.skin = Design;
GUI.Label(Rect(10,10,200,50),"Level: 2");
GUI.Label(Rect(10,40,200,50),"Coins: " + player.playerScore);
GUI.Label(Rect(115,40,200,50),"/6");
GUI.Label(Rect(10,70,200,50),"Lives: " + player.playerLives);
GUI.Label(Rect(10,100,200,50),"Timer: " + someString);
GUI.Label(Rect(10,high,200,50),"New HighScore");
if (GUI.Button(Rect(Screen.width/2-b,Screen.height/2-h1,150,30),"Return To Game")){
player.q2 = false;
player.Quit = false;
}
if (GUI.Button(Rect(Screen.width/2-b,Screen.height/2-h2,150,30),"Main Menu"))
Application.LoadLevel(0);
if (GUI.Button(Rect(Screen.width/2-b,Screen.height/2-h3,150,30),"Quit Game")){
Application.Quit();
}
}
function LoadLevel(level: int, delay: float){
yield WaitForSeconds(delay);
Application.LoadLevel(level);
}
function VolOut(){
while(audio.volume>0.05){
audio.volume-=0.001;
yield;
}
}
function Volin(){
while(audio.volume<0.2){
audio.volume += 0.001;
yield;
}
}
i am trying to do this
#pragma strict
static var level1time : float = 0.0/3;
var spiller : GameObject;
var time01 : String;
var high : int = -100;
var b : int = 75;
var h1 :int = 90;
var h2 :int = 50;
var h3 :int = 10;
var menuOptions = new String[3];
menuOptions[0] = "Return To Game";
menuOptions[1] = "Main Menu";
menuOptions[2] = "Quit Game";
var selectedIndex = 0;
function Start () {
player.q2 = false;
level1time = 0;
player.playerLives = 3;
player.playerScore = 0;
Screen.showCursor = false;
}
function Update () {
if (player.Quit == true){
h1 = 90;
h2 = 50;
h3 = 10;
}else{
h1 = 2290;
h2 = 2250;
h3 = 2210;
}
if (Input.GetAxis("Vertical")* -1) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "down");
}
if (Input.GetAxis("Vertical")) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "up");
}
if (player.Quit == true){
Screen.showCursor = true;
Time.timeScale = 0.0;
}
if ((player.Quit == false)&& player.paused == false){
Screen.showCursor = false;
Time.timeScale = 1.0;
}
if(player.paused == true){
(VolOut());
}else{
(Volin());
}
if(player.playerScore <= 4){
level1time += Time.deltaTime;
}
if(player.playerScore >= 5){
spiller.rigidbody.isKinematic = true;
player.gameover = true;
(VolOut());
if(level1time < PlayerPrefs.GetFloat("level1")){
PlayerPrefs.SetFloat("level1",level1time);
PlayerPrefs.SetString("name1",scoremenu.mainname);
high = 130;
}
LoadLevel(3, 2.5);
}
if(Input.GetKeyDown("m")) {
if(audio.mute)
audio.mute = false;
else
audio.mute = true;
}
}
function menuSelection (menuItems, selectedItem, direction) {
if (direction == "up") {
if (selectedItem == 0) {
selectedItem = menuItems.length - 1;
} else {
selectedItem -= 1;
}
}
if (direction == "down") {
if (selectedItem == menuItems.length - 1) {
selectedItem = 0;
} else {
selectedItem += 1;
}
}
return selectedItem;
}
var Design : GUISkin;
function OnGUI () {
var someString = String.Format("{0:00}:{1:00}", Mathf.Floor(level1time/60), level1time % 60);
GUI.skin = Design;
GUI.Label(Rect(10,10,200,50),"Level: 1");
GUI.Label(Rect(10,40,200,50),"Coins: " + player.playerScore);
GUI.Label(Rect(115,40,200,50),"/5");
GUI.Label(Rect(10,70,200,50),"Lives: " + player.playerLives);
GUI.Label(Rect(10,100,200,50),"Timer: " + someString);
GUI.Label(Rect(10,high,200,50),"New HighScore");
GUI.SetNextControlName ("Return To Game");
if (GUI.Button(Rect(Screen.width/2-b,Screen.height/2-h1,150,30),"Return To Game")){
player.q2 = false;
player.Quit = false;
}
GUI.SetNextControlName ("Main Menu");
if (GUI.Button(Rect(Screen.width/2-b,Screen.height/2-h2,150,30),"Main Menu")){
Application.LoadLevel(0);
}
GUI.SetNextControlName ("Quit Game");
if (GUI.Button(Rect(Screen.width/2-b,Screen.height/2-h3,150,30),"Quit Game")){
Application.Quit();
}
GUI.FocusControl (menuOptions[selectedIndex]);
}
function showtime(){
var min:int;
var sec:int;
var time01 :String;
min = level1time/60;
sec = level1time%60;
time01 = min.ToString()+ ":" + sec.ToString("D2");
}
function PlayerCompletedGame() {
guiText.text = Time.timeSinceLevelLoad.ToString();
}
function LoadLevel(level: int, delay: float){
// wait the delay time...
yield WaitForSeconds(delay);
// then load the level:
Application.LoadLevel(level);
}
function fadeOut(){
audio.volume -= 0.05 * Time.deltaTime;
yield WaitForSeconds(0.1);
audio.volume -= 0.05 * Time.deltaTime;
yield WaitForSeconds(0.1);
audio.volume -= 0.05 * Time.deltaTime;
}
function VolOut(){
while(audio.volume>0.05){
audio.volume-=0.001;
yield;
}
}
function Volin(){
while(audio.volume<0.2){
audio.volume += 0.001;
yield;
}
}
but i am getting this errors
Assets/Scripts/score.js(95,36): BCE0019: 'length' is not a member of 'Object'. Assets/Scripts/score.js(97,26): BCE0051: Operator '-' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'. Assets/Scripts/score.js(101,40): BCE0019: 'length' is not a member of 'Object'. Assets/Scripts/score.js(104,14): BCE0051: Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'.
i am beginder trying the best i can to make my game, i am thankful for eny help
looks good now i only get this error
Assets/Scripts/score.js(91,62): BCE0018: The name 'Integer' does not denote a valid type ('not found'). Did you mean 'System.Runtime.CompilerServices.IndexerNameAttribute'?
Ok so i think it's int ins$$anonymous$$d of Integer in my code.
i change Integer to int, all work now, thx for the help, i am so happy
I converted my comment to an answer so that you can accept it to mark the question as solved.
Answer by ExTheSea · May 10, 2013 at 02:48 PM
You have this function
function menuSelection (menuItems, selectedItem, direction) {
. Like you have it now you don't declare their type so they default to the Object Type. What you have to do is change the part in brackets to something like this:
function menuSelection (menuItems : String[], selectedItem : int, direction : String) {
Maybe you have to change it a bit when you if you need slightly different types.