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
Send the code on here
K just gimme a bit, I had stuff to do when I asked for help
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] }
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 all sorted, images don't matter name, also will probably need resizable thx!
@here help pls
Sucessful warning
Warned <@1087296987545743410> for attempting to ping @.here
#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] }
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] }
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)
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
Sorry it glitched
What happens rn
The array works and absolutely nothing else
@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?
Set ad to nullable and then check if it is nil in the view.
Also please for the love of everything use code blocks when posting code, it makes it so much more readable.
```swift
// your code here
```
@thatgoldman ^
Also I much nicer, imo, way to do timers without using combine would be something like:
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.Thx
So put the array in between I'm guessing?
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?