Ok here’s what I’m trying to do. I want to make a text based adventure game that relys on commands given by buttons. I have 4 buttons, “North”, “East”, “South”, and “West”. I want to set it up so that when you click on a directional button it will “Take you to a differen’t place in the game world” and thus display some text telling you what is happening in the world around you. I tried doing this in Script Editor with dialog boxes, but it wouldn’t work with more than 3 buttons. Now I am using AppleScript Studio with a window, 4 buttons, and i need something to display my text. I tried usinga text box, but that always has the flashing typing line thing. The text box is ment to by typed in. I want a box that will display my text and not allow it to be modified. As well, i need a command that will display my text. I tried:
on action TextBox–alias for text box
display text “Welcome to my text game!”
end action
but it doesn’t display the text in any of the given boxes. i’m not sure if it’s the box i’m using or the command i’m using. Either way it’s not working. I think there are a few more things i’m having problems with but right now those are the basics. I’m sure i’ll think of the others later. If anyone that has had experience using Project Builder could help me, that would be awesome.
You can use different kind of text boxes… Seems that you are using input-fields, but you can choose static-text instances… Go to the “Cocoa-views” (where are located the radio buttons and checkboxes) and choose one of “Label font text”, “Small system font text” or “System font text”. Drop it to your window and resize it to fit your needs.
Select such “static” text field and go to the applescript tab (command + 6); choose a name, eg “testField”.
Now, select your “East” button and assing it a name as well (eg, “east”). In the same pane, choose the “clicked” event for it (sub “Action”), and connect it to your applescript (below).
Go to the applescript code. You will see something similar to this:
on clicked theObject
--> do anything here
end clicked
Now, the funny part:
on clicked theObject
if name of theObject = "east" then
set contents of text field "testField" of window of theObject to "east clicked!"
end if
end clicked
Note that you must call your interface objects hierarchically. All the following maybe correct syntaxes:
text field "testField" of window 1
text field 1 of window 1
text field 1 of window "whateverNameHasThisWindow"
--> and, in our sample:
text field "testField" of window of theObject
--> "window of theObject" is a reference to the "container window of the clicked button", where is also located the text field.
Please, share a demo when you are closed to the end!
Ok, thanks for the help. Say, you wouldn’t know of a command to randomly generate a number between 1 and 5. As well i need the command that starts your script over. I am using repeats. But it’s really complicated. There are about 10 repeats inside each other but i’m sure there is a better way to do it. Ya, I’ll try to post the script when it’s near completion.
well i can have it display text on a button text, but I can’t have it display text when i start up the app. Is there a way in project builder to have it display text when the app is first started rather than when a button is clicked?
Select your main window in IB and go to the applescript pane. You will see thousands of events. The first in reacting is “awake from nib”. You can select this event, connect it to your applescript and change the code from the “clicked” to the “awake from nib”… (please, note that if you do this, “theObject” is now the window, not the button)
well i tried it with awake from nib. It doesn’t seem to be working. I don’t know what to set the "if name of theObject = “east” then, for the case of using awake from nib. Do you always have to use the “if” statement and the “theObject” variable or could you say.
"on clicked "east"
set contents of text field "testField" of window of "east" to "east clicked!"
end clicked
as well i still need a command to start the script over again. You can’t stay “restet script”, what is the command?
I’m still not getting how the “theObject” variable works. Is there a way to set the commands, ie: “on clicked theObject” to have a different variable. such as “buttonClick”? What i’m getting at is that it only lets you have the “On Clicked” handle once. Is it possible to use it more than once? And i still need to know a command that will reset my script.
After assigning your elements names in the AppleScript info panel and attaching the “clicked” handler to them and connecting it to the main script (all in Interface Builder), try this in your script:
on clicked theObject --> or "on clicked x", or "on clicked whatever"
log theObject
--> do whatever
end
Now, if you choose “Build & Run” in PB, every time you click such object (typically a button), you will see in PB’s log anything such as “button whatever of window whatever”.
For instance, if you have buttons “East” and “West” in the same window, and you attached the “clicked” event to both, you can see in the log:
button “East” of window “whatever”
button “West” of window “whatever”
If you attach an “awake from nib” event to your window, then the object is the window. If you state here also “log theObject”, you will see:
window “whatever”
If you connect such event (awake from nib) to several windows, tables or whatever you wish, you will receive all the logs:
window “whatever”
table view “whatever” of window “whatever”
I have a simple game that is based on the idea of “walking through a mine field” you pick a direction and either you will go safely 10 feet or blow up. If you blow up i want it to A) Display a dialog that has buttons “Quit” and “Try Again”, and B) when Try Again is clicked i want the script to start over from the beginning so they can, try again.
the problem i’m still having on the other subject is this:
I am trying to create a game that you click on the directional buttons and the text field will display text that tells the user what is happening in the environments ie:
you click button “North”—“As you walk north you see a clearing in the forest with a small cottage in the middle. To you West and east are more trees, and to you south is the entrance of the forst”
But once north is clicked i need it to display something different the next time one of the 4 directional buttons are clicked so you can navigate throught he virtual world. If iI add a second “on clicked theObject” tag, It gives an error so i can’t do that. I don’t want to use repeats and i’m assuming that wouldn’t work anyways. Am i limited to making a bunch of layers inside the on clicked tag? something like:
on clicked theObject
if theObject= "Noth" then
set contents of text field "textBox" of window of theObject to "You are walking through a fields...."
if theObject= "North" then
set contents of text field "textBox" of window of theObject to "You see a wild bear."
else if theObject= "East" then
' ' ' ' '
' ' ' ' '
' ' ' ' '
' ' ' ' '
end if
else if theObject= "East" then
' ' ' ' '
' ' ' ' '
' ' ' ' '
' ' ' ' '
end if
end clicked
And so on…It’s kind of confusing, but if you look at it you can see that i’m just making a bunch of “if” statements inside each other. It’s really confusing and not very effecient. So is there another way?
What you could do is create a matrix for your “world” so that every point has an x,y coordinate and an associated description of the location. x & y should be properties. Moving North: y = y + 1, South: y = y - 1, East: x = x + 1, West: x = x - 1 until you get to the limits of the matrix. In other words, start x, y at 5, 5. If x or y = 0 or 10, then you’ve hit the limit of the world and can’t continue in that direction and the button for that direction should be disabled. Hide your mine at a random x, y location at the beginning. If they hit the coordinate of the mine, boom, game over, reset x, y to 5, 5, rehide the mine and the treasure (assuming that is the goal), and start over. The “world” would be a list of records like thus {{x:1, y:1, description:“Trees North…”}, {x:2, y:1, description:“Trees North…”}, {x:3, y:1, description:“Trees North…”}, etc.} You would have a property for the mine location and the treasure location. On every move, look to see if the location is either the bomb or treasure, if so display the proper message and restart the game. If not, loop through all the records looking for the x, y match then provide the description. Coding this should take about 10 minutes but creating your world would take significantly longer and would be aided by a big piece of graph paper, a pencil, and a lot of imagination.
hmm i must have not explained it well enough. The “Land Mine Game” is differen’t than the game i’m trying to do now. The Mine game is completed, this game is an adventure/rpg that you systematically move around solving, whatever. For the land mine game i just needed a command that resets the script. If there is no such command thats ok. But i’m focusing on my present game titled “TBA-Text Based Adventure”. As for you advice, i understand the idea but i don’t quite know know what code to use for that. Could you give me some example code?
To make your script go back to the beginning, put the whole thing in a repeat like this:
repeat while i is less than 2
--play game
if blown_up then
display dialog "What would you like to do?" buttons {"Quit", "Try Again"}
if button returned of the result is "Try Again" then
set i to 1 -- it will repeat again
else
exit repeat -- it will exit
end if
end repeat