Posts

Post not yet marked as solved
0 Replies
206 Views
Hi, I am trying to focus a textbox once a button is clicked, and I am not sure how to do this? Present TextField  @State var isHidden: Bool = true ... if !isHidden {         withAnimation{           TextField("Add Note...", text: $textValue)             .padding(.top, 10.0)                       }         .transition(.move(edge: .trailing))         .textFieldStyle(RoundedBorderTextFieldStyle())                   } Button to show Text Field if (buttonCount == 0){         isHidden = false         buttonCount += 1 } At the moment everything works fine but when I click the button to show the textfield its not auto-selected by default. MacOS Version: > 11.0
Posted
by marcusc.
Last updated
.
Post not yet marked as solved
0 Replies
278 Views
I am trying to add a record which contains the order and text of it. I can add a new record within the same file using the function "addItem()", but when I try to call this same function from another file I get the error  Thread 1: EXCBADINSTRUCTION (code=EXCI386INVOP, subcode=0x0)  on the line  newItem.order = (listItems.last?.order ?? 0) + 1. var body: some View { &#9;&#9;NavigationView { &#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;List { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(listItems, id: \.self) { item in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;NavigationLink(destination: ListItemView(listItem: item)) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("\(item.name) - \(item.order)") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onDelete(perform: deleteItem) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onMove(perform: moveItem) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9; // Button(action: addItem) { &#9;&#9;&#9;&#9;&#9;&#9;//&#9;&#9;Text("Add Item") &#9;&#9;&#9;&#9;&#9; // } &#9;&#9;&#9;&#9;&#9;&#9;Button(action: { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.isShowingAlert.toggle() &#9;&#9;&#9;&#9;&#9;&#9;}) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Add Item") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.navigationBarItems(trailing: EditButton()) &#9;&#9;} &#9;&#9;.addSujectPopUp(isShowing: $isShowingAlert, text: $alertInput, title: "Add Subject") } func moveItem(indexSet: IndexSet, destination: Int) { &#9;&#9;let source = indexSet.first! &#9;&#9; &#9;&#9;if source < destination { &#9;&#9;&#9;&#9;var startIndex = source + 1 &#9;&#9;&#9;&#9;let endIndex = destination - 1 &#9;&#9;&#9;&#9;var startOrder = listItems[source].order &#9;&#9;&#9;&#9;while startIndex <= endIndex { &#9;&#9;&#9;&#9;&#9;&#9;listItems[startIndex].order = startOrder &#9;&#9;&#9;&#9;&#9;&#9;startOrder = startOrder + 1 &#9;&#9;&#9;&#9;&#9;&#9;startIndex = startIndex + 1 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;listItems[source].order = startOrder &#9;&#9;&#9;&#9; &#9;&#9;} else if destination < source { &#9;&#9;&#9;&#9;var startIndex = destination &#9;&#9;&#9;&#9;let endIndex = source - 1 &#9;&#9;&#9;&#9;var startOrder = listItems[destination].order + 1 &#9;&#9;&#9;&#9;let newOrder = listItems[destination].order &#9;&#9;&#9;&#9;while startIndex <= endIndex { &#9;&#9;&#9;&#9;&#9;&#9;listItems[startIndex].order = startOrder &#9;&#9;&#9;&#9;&#9;&#9;startOrder = startOrder + 1 &#9;&#9;&#9;&#9;&#9;&#9;startIndex = startIndex + 1 &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;listItems[source].order = newOrder &#9;&#9;} &#9;&#9; &#9;&#9;saveItems() } func deleteItem(indexSet: IndexSet) { &#9;&#9;let source = indexSet.first! &#9;&#9;let listItem = listItems[source] &#9;&#9;managedObjectContext.delete(listItem) &#9;&#9;saveItems() } func addItem() { &#9;&#9;let newItem = ListItem(context: managedObjectContext) &#9;&#9;newItem.name = alertInput &#9;&#9;newItem.order = (listItems.last?.order ?? 0) + 1 &#9;&#9;saveItems() } func saveItems() { &#9;&#9;do { &#9;&#9;&#9;&#9;try managedObjectContext.save() &#9;&#9;} catch { &#9;&#9;&#9;&#9;print(error) &#9;&#9;} } Pop Up Code button: let subject = SubjectController() Button(action: self.subject.addItem) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Add") &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}
Posted
by marcusc.
Last updated
.
Post marked as solved
5 Replies
854 Views
I am currently making a game on Xcode 11, I am having a problem where in which my sprite will spawn in straight away at the start, even through their is a wait delay, but after the first spawn the sprites spawn in at their current time. How can I make it so the sprites wont spawn in untill 3 seconds after the scene has started. Any help would be greatly appreciated.The photo attached shows what happens at the start of the game.I have attached part of my code for one of the sprites as their setup is similar to each other.//Setup Bird func setupBird() { bird = SKSpriteNode(imageNamed: "bird-1") bird.name = "Bird" bird.zPosition = 20.0 bird.setScale(1.5) let birdHeight = bird.frame.height let random = CGFloat.random(min: -birdHeight, max: birdHeight*2.0) bird.position = CGPoint(x: cameraRect.maxX + bird.frame.width, y: size.height/2.0 + birdHeight + random) bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.width/2.0) bird.physicsBody!.affectedByGravity = false bird.physicsBody!.isDynamic = false bird.physicsBody!.categoryBitMask = PhysicsCategory.Bird bird.physicsBody!.contactTestBitMask = PhysicsCategory.Player addChild(bird) bird.run(.sequence([.wait(forDuration: 15, withRange: 5), .removeFromParent()])) //Animation For Birds var textures: [SKTexture] = [] for i in 1...3 { textures.append(SKTexture(imageNamed: "bird-\(i)")) } bird.run(.repeatForever(.animate(with: textures, timePerFrame: 0.15))) } func spawnBird() { let random = CGFloat.random(min: 15.0, max: 30.0) run(.repeatForever(.sequence([ .wait(forDuration: TimeInterval(random)), .run { [weak self] in self?.setupBird() } ]))) }
Posted
by marcusc.
Last updated
.