Hello, new here. Thanks for the tutorials. I’m working my way thru them as I try to make progress on my own inspiration.
I’m trying to make an interactive fiction story for my son. Something like the “Fighting Fantasy” or “Choose your own Adventure” series. For the time being, I just want to make one story and for my son and I to be able to input the story and options. I’m thinking this is a really simple app. What I’m having trouble with is how to code in a page number variable and an - if - statement so that I can program each “page” and vary the buttons. My code below already works, but only for one page. When I try to ad an pageNum variable and if statements, I get declaration errors (not show). I’m also assuming there is a better way of programming this app.
Code and screenshot below.
import UIKit
class ViewController: UIViewController {
// main story text
@IBOutlet weak var storyText: UILabel!
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func button1(_ sender: Any) {
storyText.text = "You walk a short way downhill towards the docks. There are several men sitting and talking around a small stove. They appear to be smilling and enjoying themselves. Would you like to introduce yourself or go directly to one of the ships?"
// Change the buttons titles
button1.setTitle("Introduce yourself.", for: .normal)
button2.setTitle("Go to a ship.", for: .normal)
// The 3rd button is not needed, so set to blank - note: it is still clickable; need to remove button, don't know how.
button3.setTitle("", for: .normal)
}
@IBAction func button2(_ sender: Any) {
storyText.text = "You enter one of the provision stores. Inside, you see a man behind the counter, smoking a cigar and busy putting things away. He looks at you impatiently and says that he is closing the shop for the day but if you chose quickly, you may buy or barter whatever you like. You have only the small silver ring that your aunt gave you and two copper pieces. Would you like to barter with your ring or use your copper money?"
button1.setTitle("Barter with your ring.", for: .normal)
button2.setTitle("Use your copper money.", for: .normal)
// The 3rd button is not needed, so set to blank - note: it is still clickable; need to remove button
button3.setTitle("", for: .normal)
}
@IBAction func button3(_ sender: Any) {
storyText.text = "You approach the inn but the people outside are blocking the entrance. Do you try to push through them, hoping your machismo will intimidate them or engage them in conversation?"
button1.setTitle("Push thru the people.", for: .normal)
button2.setTitle("Coverse.", for: .normal)
// The 3rd button is not needed, so set to blank - note: it is still clickable; need to remove button
button3.setTitle("", for: .normal)
}
}