I was looking to find a way to find out if my SKSpriteNode with an SKPhysicsBody is in motion.
I create a box, raise it above the bar, then set the bar and box masks to collide. The bar is pinned, but would love to know when the box stops bouncing.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have an SkSpriteNode (ball) in a box. Gravity is set to false, otherwise it has an SkPhysicsBody to bounce off of walls.
Is there a method to give the ”ball” an initial push in a specified direction and velocity/strength wise?
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?
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
Below is a simple code patch I use to create a custom button, am new at this. I noticed the text does not appear and am wondering what the proper method is for making the text appear?
func makeButton (vControl: ViewController, action: Selector) {
let myButtonImage = UIImage(named: "Picture1.png")
let imageScale = myButtonImage!.size.width / myButtonImage!.size.height
let wwidth = vControl.self.view.bounds.width
let button = CreateButton(frame: CGRect(x: 100, y: 100, width: (myButtonImage?.size.width)!/3, height: (myButtonImage?.size.height)!/3))
button.setImage(myButtonImage, for: .normal)
button.backgroundColor = .clear
button.setTitleColor(.black, for: .normal)
button.setTitle("Test Button", for: .normal)
button.addTarget(vControl, action: action, for: .touchUpInside)
vControl.view.addSubview(button)
button.center = vControl.view.center
}
While I do need to get another HD, I currently have
34.85 GB (12.61 GB purgeable) available.
Yet for some reason when I try to run update on Xcode, it says I don't have enough HD space. That's three times the final size of the new Xcode.
Is there some way around this?
So sorry if I should't be asking this here, but am trying to find a current-ish tutorial on how to make an app that converts speech to text in real time.
Transcribing from text to speech as you're speaking.
I've found a few one's on YouTube, but they are quite old, or just transcribing from a recorded file, etc. etc.
If anyone is aware of a good tutorial, paid or not, I would so appreciate any link.
Thank you
So am watching a Speech To Text demo on YouTube, here:
https://www.youtube.com/watch?v=SZJ8zjMGUcY
There are no files, so am typing from the screen, and immediately run into an error that confuses me.
at
class ViewController : UIViewController, SFSpeechRecognizer {
here's a screenshot:
Swift gives me an error indicating that Multiple Inheritance is not allowed.
The programmer doesn't have files to download, and I like to start from scratch anyway, typing and copying so I am forced to read each line.
Is there something I have to change in the project itself that allows Multiple Inheritances?
This video is from last year, and is on Swift 5.0. So I don't think there could be that much of a major change in Swift in that time.
Thanks
Below is my code. I have the ViewController, which takes up entire screen (set background color, makes sure status bar is visible, etc.
It then calls up the MainController, which is set to be only in the safeAreaLayout frame. It has a button that brings up a third view controller when clicked.
Everything works, the ViewController covers the entire screen, the MainController rests within the safeAreaLayouts of the iPhone X, and the third view controller comes up the same size and position as the MainController.
It's that last part I want to make sure of, that that is the way it is supposed to come up. Can I count on that? Or must I set its frame myself to be sure?
ViewController
class ViewController: UIViewController {
var mainController = MainController()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.addChild(mainController)
self.view.addSubview(mainController.view)
setConstraints(vc: mainController, pc: view)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = bgColor
}
override var prefersStatusBarHidden: Bool {
return false
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .darkContent
}
}
func setConstraints (vc: UIViewController, pc: UIView) {
vc.view.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint]()
constraints.append(vc.view.leadingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.leadingAnchor))
constraints.append(vc.view.trailingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.trailingAnchor))
constraints.append(vc.view.bottomAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.bottomAnchor))
constraints.append(vc.view.topAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.topAnchor))
NSLayoutConstraint.activate(constraints)
}
MainController
class MainController: UIViewController {
private lazy var countController = CountController()
var invButton : MyButton!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
...button code ....
}
override var prefersStatusBarHidden: Bool {
return false
}
@objc func buttonAction(sender: UIButton!) {
guard let theButton = sender as? MyButton else { return}
self.addChild(countController)
self.view.addSubview(countController.view)
}
}
ThirdViewController
class CountController : UIViewController {
var backButton : MyButton!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .gray
}
}
At the very bottom is my code where MainController initiates a subview called setController. The new subview is created when I click the button
However, within setController's code I get back a nil when I try to the superview:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("\(self.view.superview)" ?? "count->no parent")
}
I am assigning the second view as a subview, but obviously I am missing something. Have I misunderstood how UIView hierarchy works?
class MainController: UIViewController {
private lazy var setController = SetController()
var invButton : MyButton!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
invButton = makeButton(vControl: self, btype: ButtType.inv, action: #selector(self.buttonAction(sender:)))
invButton.frame.origin.x = self.view.frame.width * 0.1
invButton.frame.origin.y = self.view.frame.height * 0.1
invButton.setTitle("Settings", for: .normal)
}
override var prefersStatusBarHidden: Bool {
return false
}
@objc func buttonAction(sender: UIButton!) {
guard let theButton = sender as? MyButton else { return}
UIView.transition(with: self.view, duration: 0.5, options: .transitionCurlDown, animations: { [self] in
self.addChild(setController)
self.view.addSubview(setController.view)
setController.didMove(toParent: self)
}, completion: nil)
}
}
Copied the code below from a tutorial, and I mostly understand what is going on. But I'd like to be able to fully read it.
What I do get is that:
handler can be nil, but here it's the code to run upon the completion of UIAlertAction.
But am unsure what (_) in is. I have also sometimes seen it as [self] in
Thank you
controller.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in controller.dismiss(animated: true, completion: nil)}))
Am using the demo code below to flesh out an audio recording app in Swift 5.x
I would like to monitor certain aspects of the AVAudioRecorder as it is recording. Such as: frequency, power, volume, etc. but in live time.
I found an example in Swift 3 where the user sets up a callback timer for 0.5 sec. I was wondering if this was still the case, or that in the latest version of Swift, there might be a callback function in the AVAudioEngine that gets called at a regular frequency?
do {
audioRecorder = try AVAudioRecorder(url: audioFilename!, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
recordButton.setTitle("Tap to Stop", for: .normal)
} catch {
finishRecording(success: false)
}
}
Expanding a speech to text demo, and while it works, I am still trying to learn Swift. Is .installTap the Swift version of a C callback function?
From what I interpret here, every time the buffer becomes full, the code in between the last { } runs, as well, the code below it is also run.
It almost feels like a callback combined with a GOTO line from basic.
yes, it works, but I'd like to understand that I am getting the flow of the code correctly.
func startSpeechRecognition (){
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, _) in self.request.append(buffer) }
audioEngine.prepare()
do {
try audioEngine.start()
} catch let error {
...
}
guard let myRecognition = SFSpeechRecognizer() else {
...
return
}
if !myRecognition.isAvailable {
...
}
task = speechRecognizer?.recognitionTask(with: request, resultHandler: { (response, error) in guard let response = response else {
if error != nil {
print ("\(String(describing: error.debugDescription))")
} else {
print ("problem in repsonse")
}
return
}
let message = response.bestTranscription.formattedString
print ("\(message)")
})
}
I created the playground below to answer my question "If I created a class instance using DispatchQueue.global().async would that class remain in its own asynchronous queue? Even if the main app called one of that classes methods, would that method run asynchronously compared to the main app?
With the sleep line I discovered that the answer is "no."
But I am curious if there is a legit way to do this? Or even if there is, it is considered bad programming?
import UIKit
class MyFunx : NSObject {
var opsCount = 0
override init() {
super.init()
}
func addValues (a: Int, b: Int) {
let c = a + b
opsCount += 1
sleep(1)
}
}
var firstVar = 0
var secondVar = 0
var myFunx : MyFunx?
while secondVar < 100 {
print ("starting")
if myFunx == nil {
print ("making myFunx")
DispatchQueue.global().async {
myFunx = MyFunx()
}
} else {
myFunx!.addValues(a: firstVar, b: secondVar)
}
firstVar += 1
secondVar += 1
}
print ("myFunx = \(myFunx)")
print ("\(myFunx?.opsCount)")
print ("exiting")
I have an iOS app with multiple subclasses of UIViewControllers. There are many type of UIAlertControllers I might need to use based on user interaction, internet connection, and catching any other fatal errors.
So I wrote the extension for UIViewController below, which works just fine. And I can call from any of my UIViewControllers as simply as:
myErrors(error: MyErrors.e1.rawValue, title: "Internet Error", msg: "Unable to connect to Internet\nTry Again?")
While this works, I do not know if it's proper to add an extension to UIViewController. Is this considered bad practice? Is there another way I should be pursuing this?
extension UIViewController {
func myErrors(error: MyErrors, title: String, msg: String)
{
var title = ""
var message = ""
switch error {
case .e1:
title = String(format: "%@", title)
message = String(format: "Database Error %03d%@\n", error.rawValue, msg)
case .e2:
title = String(format: "%@", title)
message = String(format: "Internet Error %03d%@\n", error.rawValue, msg)
case .e3:
title = String(format: "%@", title)
message = String(format: "User Error %03d%@\n", error.rawValue, msg)
}
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
switch error {
case .e1:
alert.addAction(UIAlertAction(title: "No", style: .init(rawValue: 0)!, handler: { (action: UIAlertAction!) in
// ..log error
//...proceed to code based on No ....
}))
alert.addAction(UIAlertAction(title: "Yes", style: .init(rawValue: 0)!, handler: { (action: UIAlertAction!) in
// ..log error
//...code based on Yes ....
}))
case .e2:
// No user option availabe in this alert, just OK
// ... do all logging of errors
// proceed
case .e3:
// Add specific acctions to this error
// ... do all logging of errors
// proceed
}
self.present(alert, animated: true, completion: nil)
}
}