Post

Replies

Boosts

Views

Activity

How can I have an SKSpriteNode follow the same path back and forth?
My code is compartmentalized over many files, so I hope I can describe this easily. I have a path that goes from point A to point B I place an SKSpriteNode at point A and execute: iconPath[0] = the path SKAction.follow(iconPath[0], asOffset: false, orientToPath: false, duration: 1)) I execute that SKACtion and as I expected, my SKNode goes from point A to point B. Fine. I'm not sure how to reverse it, I assumed from the dox that calling that same SKAction as is, would make the SKNode go from point B to A.
2
0
659
Feb ’21
Having strange trouble with touchesMoved, need help.
Am trying to make a game, where I drag gems around. The hierarchy is: GameScene (this is the view controller) gemBase, which holds the gems (light grey) SKSpriteNode gems SKSpriteNode The gems are children of gemBase, which is a child of GameScene. When the gemBase is at 0,0 everything works fine when I drag the gems. They are in the same place I am touching the screen. But I want to offer a left-handed feature, where I offset the gemBase to the rightsize of the screen. All gems automatically move with the base so I don't need to recalculate their positions. But then, when I try to move the gems, they are offset to the right of where I am touching the screen. They are offset by as much as I move the gemBase. Below is my only code where I handle touchesMoved (in GameScene) If you're having problems visualizing what I am describing, the screen shot is at: http: // 98.7.37.117/ss.gif Do I have to convert the touch.location?     override func touchesMoved(_ touches: SetUITouch, with event: UIEvent?){         guard touches.first != nil else { return }         if toggleHigh {highliteGem(theGem: myGems[0], clearAll: true)}         if let touch = touches.first, let node = myGV.currentGem, node.isMoving == true {             let touchLocation = touch.location(in: self)             node.moved    = true             node.position = touchLocation             node.isMoving = true             node.inSlot = false             //addTrailToTwinkle(theNode: node)         }     }
8
0
880
May ’21
How can I access an array in one of my classes?
myGV is a structure where I store a handful of global variables. The following are all sub-classes of SKSpriteNode: I have a class called guessingBar which holds 6 guessSlots. The former class has an array of the guessSlots for me to loop through. I just don't know the syntax of how to access the array. myGV holds multiple variables, so the SKSpriteNode guessBar can be found at: myGV.guessBar I expected to be able to read the array with: myGV.guessBar.guessSlots[x] but as you can see from the debugger screenshot, I cannot. In the screenshot you can see that everything is initialized. Am I missing some silly typo, or is the syntax escaping me? http: //98.7.37.117/s.png
3
0
618
Jun ’21
Is there a way to translate touches to screen coordinates
As you can see in the last two lines of the code below, I specify a specific SKSpriteNode to get the correct (or is it, adjusted?) touch coordinates. The last line is just left in there to compare while I am debugging. I was curious if there was already a method in Swift that translates any coordinates handed to it into physical screen coordinates? It would just be easier than having to first find out: Is this item that I am tracking, owned by the main GameScene, or a SKSpriteNode that has been placed somewhere other than 0,0 on the GameScene? override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {         super.touchesEnded(touches , with:event)         var delta = CGPoint(x: 0, y: 0)         guard touches.first != nil else { return }         if let touch = touches.first,            let node = myGV.currentGem,            node.isMoving == true {             let touchLocation = touch.location(in: myGV.guessBar!)             let touchLocation2 = touch.location(in: self)
1
0
820
Jun ’21
How can I return a nil in Swift
I have a subclass of SKSpriteNode called MyGem. There are multiple instances of this class at runtime. They are all in an array of MyGems. At a specific point I would like to find out which MyGem is twinkling. The problem I am running into is if no MyGem is twinkling. What do I return? I can't return a nil. 'nil' is incompatible with return type 'MyGem' So what do I return? I thought of returning the index number of the MyGem in the array class, and then passing -1 if none were twinkling. But that seems kludgy. func getHighGem() -> MyGem {    for gem in myGems {     if gem.twinkling == true {     return gem       }    }     return nil //this line causes the IDE error }
1
0
3.7k
Jun ’21
Will casting "as?" fail if not the correct type?
I know this is a silly question, but I just want to be safe. I have multiple subclasses of SKSpriteNode in my app. In touchesBegan I am looking for a specific type, MyPeg. I just want to make sure that the code below fails if the node is not a MyPeg. I have this fear that if another of my subclasses is the same size, or too similar, or even has the same variables that I might get a positive. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){ super.touchesBegan(touches , with:event) guard let touch = touches.first else { return } let location = touch.location(in: self) let touchedNodes = self.nodes(at: location) for theNode in touchedNodes{ if let node = theNode as? MyPeg{
2
0
471
Jul ’21
Can I get an subclass's property from a function call that returns said subclass?
While debugging I would like to get a specific subclass's property. So far I can do this: if let myPeg = getTargetSlot(node: node, location touchLocation){           print (myPeg.index)             } Is it possible to write it in one line? Something like this? print ("\(getTargetSlot(node: node, location touchLocation).index") Because if there is a way, I cannot figure out the syntax. Thanks
1
0
443
Aug ’21
Why am I getting touchesCanceled instead of touchesEnded?
I am using debug labels so I know when I get any touchesBegan, touchesMoving, touchesEnded, and touchesCanceled. I get everything I expect, touchesBegan, touchesMoved, and touchesEnded. However, it would appear that if I barely, just barely move an SKSPriteNode, I get a touchesCanceled instead of touchesEnded. Even thought the call right before is touchesMoved. Is this as intended? Is there some threshold you have to reach to get an ended instead of a canceled? And if so, can I see and change it? Thanks More than glad to put my code, tho not sure how that would change anything.
1
0
672
Aug ’21
What are the SKPhysics settings to make a ball bounce?
I've created a ball, and boxed my game scene. The balls physics settings are: self.physicsBody!.affectedByGravity = true self.physicsBody!.restitution = 1.0 self.physicsBody!.linearDamping = 0 self.physicsBody!.friction = 0.3 self.physicsBody!.isDynamic = true self.physicsBody!.mass = 0.5 self.physicsBody?.contactTestBitMask = bodyMasks.blankMask.rawValue self.physicsBody?.collisionBitMask  = bodyMasks.edgeMask.rawValue self.physicsBody?.categoryBitMask   = bodyMasks.ballMask.rawValue When I box the game scene with: self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) the ball bounces back up as intended. However, if I replace edgeLoopFrom with my own custom borders: yourLine.lineWidth = 5 yourLine.path = pathToDraw yourLine.isUserInteractionEnabled = false yourLine.strokeColor = .clear yourLine.isHidden = false yourLine.zPosition = 10 yourLine.physicsBody = SKPhysicsBody(edgeLoopFrom: pathToDraw) yourLine.physicsBody?.affectedByGravity = false yourLine.physicsBody?.pinned = true yourLine.physicsBody?.categoryBitMask = bodyMasks.edgeMask.rawValue yourLine.physicsBody?.collisionBitMask = bodyMasks.ballMask.rawValue yourLine.physicsBody?.contactTestBitMask = bodyMasks.blankMask.rawValue the ball just comes down with a thud, stops, and doesn't bounce., So am trying to figure out what physics settings I have to give to my custom made edges so that the ball bounces?
7
0
1.4k
Oct ’21
How can I pass a function pointer in Swift?
I would like to take this sample code override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50)) button.backgroundColor = .greenColor() button.setTitle("Test Button", forState: .Normal) button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) self.view.addSubview(button) } func buttonAction(sender: UIButton!) { print("Button tapped") } ...and place the code to create the button in another file. So it would look more like this: class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() makeButton(vControl: self) } @objc func buttonAction(sender: UIButton!) { print("Button tapped") } } Of course the main flaw is that I would have to pass a pointer to the function buttonAction. Something like we do in C makeButton(vControl: self, bFunc: &buttonAction) Have Googled a lot but can't seem to find the way to do this. How do I set up the makeButton page to recieve this? func makeButton (vControl: ViewController, bFunc: ???) What would the ??? be in reality? thanks
3
0
1k
Oct ’21