help please

I have a sorted array and timer, I just need help to get images to show up at the top or bottom of the screen when the timer runs out, please help me, ping for code
19 Replies
outsharded
outshardedβ€’16mo ago
Send the code on here
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
import SwiftUI import Combine struct ContentView: View { @State private var card = "Hord Of Paladin Goblins" @State private var timer = Timer.publish(every: 131.23, on: .main, in: .common).autoconnect() @State private var ad = "X ad" let ads = ["X ad", "Album Ad", "Pet Ad", "Discord Ad"]
func randomAd() -> String { let index = Int.random(in: 0..<ads.count) return ads[index] }
var body: some View { VStack { Text(card) .font(.largeTitle) .background(Color.black) .foregroundColor(.yellow)
Button(action: { self.card = randomCards() }) { Text("Draw a Card") .foregroundColor(.red) } } .onReceive(timer) { _ in self.ad = randomAd() } } } let cards = ["Hord Of Paladin Goblins", "Asteroid Strike", "Raining of Bones", "Acid Rain", "Dragon flies above doing nothing to effect the players", "Bugbears with wings", "The Avengers", "The area around the party engulfs in fire", "A distant mining explosion from the dwarves causes an avalanche heading directly for the party", "Lightning Drake"] func randomCards() -> String { let index = Int.random(in: 0..<cards.count) return cards[index] }
No description
No description
No description
No description
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
@outsharded all sorted, images don't matter name, also will probably need resizable thx! @here help pls
Odin
Odinβ€’16mo ago
Sucessful warning
Warned <@1087296987545743410> for attempting to ping @.here
outsharded
outshardedβ€’16mo ago
#how-to-ask Read rule 6.
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
I was reading still nothing bout pings, I thot couldn't be wasting time, reading back I get now srry! 6. You must listen to staff. You cannot evade punishment by saying 'it's not in the rules'. There are no loopholes. Wasting staff time, or attempting to, is punishable. import SwiftUI import Combine struct ContentView: View { @State private var card = "Hord Of Paladin Goblins" @State private var timer = Timer.publish(every: 131.23, on: .main, in: .common).autoconnect() @State private var ad = "X ad" let ads = ["X ad", "Album Ad", "Pet Ad", "Discord Ad"]
func randomAd() -> String { let index = Int.random(in: 0..<ads.count) return ads[index] }
var body: some View { VStack { Text(card) .font(.largeTitle) .background(Color.black) .foregroundColor(.yellow)
Button(action: { self.card = randomCards() }) { Text("Draw a Card") .foregroundColor(.red) } } .onReceive(timer) { _ in self.ad = randomAd() } } } let cards = ["Hord Of Paladin Goblins", "Asteroid Strike", "Raining of Bones", "Acid Rain", "Dragon flies above doing nothing to effect the players", "Bugbears with wings", "The Avengers", "The area around the party engulfs in fire", "A distant mining explosion from the dwarves causes an avalanche heading directly for the party", "Lightning Drake"] func randomCards() -> String { let index = Int.random(in: 0..<cards.count) return cards[index] }
outsharded
outshardedβ€’16mo ago
What are you trying to achieve?
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
As the main message says, I just want to get the images to show up when the timer goes off at the top or bottom of the code (images up above)
outsharded
outshardedβ€’16mo ago
What are you trying to achieve?
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
As I said, when the timer is up, a random one of the images will show up at the top or bottom of the screen depending, fake ads basically
outsharded
outshardedβ€’16mo ago
Sorry it glitched What happens rn
outsharded
outshardedβ€’16mo ago
@Community Coach is that how you would make a new image appear?
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
Idk, I was talking to a m8 who understands lot more than me and he said use timer @Coach help please Would I need an image array and the button being the timer?
nouun
nouunβ€’16mo ago
Set ad to nullable and then check if it is nil in the view.
@State private var ad: String? = nil

// In your view
VStack {
if let ad = this.ad {
// Display ad
}

// Rest of VStack
}
@State private var ad: String? = nil

// In your view
VStack {
if let ad = this.ad {
// Display ad
}

// Rest of VStack
}
Also please for the love of everything use code blocks when posting code, it makes it so much more readable. ```swift // your code here ```
// your code here
// your code here
@thatgoldman ^ Also I much nicer, imo, way to do timers without using combine would be something like:
Timer.scheduledTimer(withTimeInterval: 131.23, repeats: true) { _ in
self.ad = ads.randomElement()
}
Timer.scheduledTimer(withTimeInterval: 131.23, repeats: true) { _ in
self.ad = ads.randomElement()
}
You don’t need to manually find a random index as of Swift 4.2, you can use Array’s randomElement function. Beware that the function will return nil if there aren’t any elements in the array, you can safely force unwrap the element if you’re manually setting the array and know that it won’t be empty.
nouun
nouunβ€’16mo ago
Define the array wherever you see fit.
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
K, there were a few errors from missing code on my part, not yours, so they should show up?
Want results from more Discord servers?
Add your server