So I am trying to add WASD movement to my playCharacter class in Cocoa Swift (not using Xcode):
import Cocoa
public class playerCharacter: NSView {
var x: Int
var y: Int
let color: NSColor
init(x: Int, y: Int) {
self.x = x
self.y = y
self.color = NSColor.darkGray
let frame = NSRect(x: x, y: y, width: 50, height: 50)
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Draw the player character square with the specified color
color.setFill()
dirtyRect.fill()
}
}
I want to incorporate top-down left-right WASD player movement for my character. I have tried looking all over the internet, but it never works.
I have tried using NSEvent and keyDown functions, but I am lost. I tried looking up how to, but found barely anything. Please help
Add the following to your NSView class: