Normy Haddad
Normy Haddad

Reputation: 181

Name of button not changing

I am trying to build a flappy bird game where you can choose from 3 different difficulties. But the label of the button will not change. Right now, I have a function that is supposed to change the difficulty. Here is the code

I have tried changing the capitalisation of the code, although this didn't work.

This is the code for the difficulty button:

on mouseUp
    changeDiff
end mouseUp

This is the code on the card:

put 1 into difficulty    

on changeDiff
   if difficulty = 2 then
      put 1 into difficulty
      set the Label of btn "Difficulty" to "normal"
   end if
   if difficulty = 1.5 then
      put 2 into difficulty
      set the Label of btn "Difficulty" to "DEMON"
   end if
   if difficulty = 1 then
      put 1.5 into difficulty
      set the Label of btn "Difficulty" to "hard"
   end if
end changeDiff

Upvotes: 0

Views: 104

Answers (2)

Devin
Devin

Reputation: 603

Consider this approach:

Use an option menu button with the choices 1, 1.5, and 2 for choosing your difficulty level. Script the option menu like this, passing the chosen option as a parameter:

on menuPick pItemName
    changeDiff pItemName
end menuPick

Then in your card script:

local difficulty 

on changeDiff pDiff
    switch pDiff
        case 2
            put 1 into difficulty
            set the label of btn "Difficulty" to "normal"
            break
        case 1.5
            put 2 into difficulty
            set the label of btn "Difficulty" to "DEMON"
            break
        case 1
            put 1.5 into difficulty
            set the label of btn "Difficulty" to "hard"
            break
    end switch
end changeDiff

Upvotes: 1

Scott Rossi
Scott Rossi

Reputation: 885

It's not clear how you're handling "put 1 into difficulty". As written, this won't do anything. If I understand what you're trying to do, this value needs to be stored in a variable, and if the default value always starts with "1", you could do something like:

local difficulty = 1

Also, your changeDiff handler should use "else" statements, or consider exiting your handler once a value has been set to avoid one "if" statement setting up a true condition for another -- as it stands, your code will only alternate between the first two "if" options. You could write the handler like:

local difficulty = 1

on changeDiff
   if difficulty = 2 then
      put 1 into difficulty
      set the label of btn "Difficulty" to "normal"
   else
      if difficulty = 1.5 then
         put 2 into difficulty
         set the label of btn "Difficulty" to "DEMON"
      else
         if difficulty = 1 then
            put 1.5 into difficulty
            set the label of btn "Difficulty" to "hard"
         end if
      end if
   end if
end changeDiff

You might consider using a switch statement (a little easier to read) like the following:

local difficulty = 1

on changeDiff
   switch difficulty
      case 2
         put 1 into difficulty
         set the label of btn "Difficulty" to "normal"
         break
      case 1.5
         put 2 into difficulty
         set the label of btn "Difficulty" to "DEMON"
         break
      case 1
         put 1.5 into difficulty
         set the label of btn "Difficulty" to "hard"
   end switch
end changeDiff

If you want to be efficient (fewer lines of code), you could use a single "set label" statement and grab the label name from a set of label items (difficulty values are 1, 2, 3):

local difficulty = 1

on changeDiff
   add 1 to difficulty
   if difficulty > 3 then put 1 into difficulty
   set the label of btn "Difficulty" to item difficulty of "Normal,Hard,Demon"
end changeDiff

Hope this helps.

Upvotes: 3

Related Questions