I'm trying to make a magnifying glass that shows up when the user presses a button and follows the user's finger as it's dragged across the screen.
I came across a UIKit-based solution (https://github.com/niczyja/MagnifyingGlass-Swift), but when implemented in my SKScene, only the crosshairs are shown. Through experimentation I've found that magnifiedView?.layer.render(in: context) in:
public override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.translateBy(x: radius, y: radius)
context.scaleBy(x: scale, y: scale)
context.translateBy(x: -magnifiedPoint.x, y: -magnifiedPoint.y)
removeFromSuperview()
magnifiedView?.layer.render(in: context)
magnifiedView?.addSubview(self)
}
can be removed without altering the situation, suggesting that line is not working as it should. But this is where I hit a brick wall. The view below is shown but not offset or magnified, and any attempt to add something to context results in a black magnifying glass.
Does anyone know why this is? I don't think it's an issue with the code, so I'm suspecting its something specific to SpriteKit or SKScene, likely related to how CALayers work.
Any pointers would be greatly appreciated.
.
.
.
Full code below:
import UIKit
public class MagnifyingGlassView: UIView {
public weak var magnifiedView: UIView? = nil {
didSet {
removeFromSuperview()
magnifiedView?.addSubview(self)
}
}
public var magnifiedPoint: CGPoint = .zero {
didSet {
center = .init(x: magnifiedPoint.x + offset.x, y: magnifiedPoint.y + offset.y)
}
}
public var offset: CGPoint = .zero
public var radius: CGFloat = 50 {
didSet {
frame = .init(origin: frame.origin, size: .init(width: radius * 2, height: radius * 2))
layer.cornerRadius = radius
crosshair.path = crosshairPath(for: radius)
}
}
public var scale: CGFloat = 2
public var borderColor: UIColor = .lightGray {
didSet {
layer.borderColor = borderColor.cgColor
}
}
public var borderWidth: CGFloat = 3 {
didSet {
layer.borderWidth = borderWidth
}
}
public var showsCrosshair = true {
didSet {
crosshair.isHidden = !showsCrosshair
}
}
public var crosshairColor: UIColor = .lightGray {
didSet {
crosshair.strokeColor = crosshairColor.cgColor
}
}
public var crosshairWidth: CGFloat = 5 {
didSet {
crosshair.lineWidth = crosshairWidth
}
}
private let crosshair: CAShapeLayer = CAShapeLayer()
public convenience init(offset: CGPoint = .zero, radius: CGFloat = 50, scale: CGFloat = 2, borderColor: UIColor = .lightGray, borderWidth: CGFloat = 3, showsCrosshair: Bool = true, crosshairColor: UIColor = .lightGray, crosshairWidth: CGFloat = 0.5) {
self.init(frame: .zero)
layer.masksToBounds = true
layer.addSublayer(crosshair)
defer {
self.offset = offset
self.radius = radius
self.scale = scale
self.borderColor = borderColor
self.borderWidth = borderWidth
self.showsCrosshair = showsCrosshair
self.crosshairColor = crosshairColor
self.crosshairWidth = crosshairWidth
}
}
public func magnify(at point: CGPoint) {
guard magnifiedView != nil else { return }
magnifiedPoint = point
layer.setNeedsDisplay()
}
private func crosshairPath(for radius: CGFloat) -> CGPath {
let path = CGMutablePath()
path.move(to: .init(x: radius, y: 0))
path.addLine(to: .init(x: radius, y: bounds.height))
path.move(to: .init(x: 0, y: radius))
path.addLine(to: .init(x: bounds.width, y: radius))
return path
}
public override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.translateBy(x: radius, y: radius)
context.scaleBy(x: scale, y: scale)
context.translateBy(x: -magnifiedPoint.x, y: -magnifiedPoint.y)
removeFromSuperview()
magnifiedView?.layer.render(in: context)
//If above disabled, no change
//Possible that nothing's being rendered into context
//Could it be that SKScene view has no layer?
magnifiedView?.addSubview(self)
}
}
SpriteKit
RSS for tagDrawing shapes, particles, text, images, and video in two dimensions using SpriteKit.
Post
Replies
Boosts
Views
Activity
I'm trying to display a right-aligned timecode in my game. I had expected that digits would all have the same width, but this doesn't seem to be the case in SpriteKit, even though it seems to be the case in AppKit.
In SpriteKit, with the default font there is a noticeable difference in width between the digit 1 and the rest (1 is thinner), so whenever displaying a number with the least significant digit 1 all preceding digits shift slightly to the right. This happens even when setting a NSAttributedString with a font that has a fixedAdvance attribute.
class GameScene: SKScene {
override func didMove(to view: SKView) {
let label = SKLabelNode(text: "")
view.scene!.addChild(label)
// label.horizontalAlignmentMode = .left
label.horizontalAlignmentMode = .right
var i = 11
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { _ in
label.text = "\(i)"
// let font = NSFont(descriptor: NSFontDescriptor(fontAttributes: [.name: "HelveticaNeue-UltraLight", .fixedAdvance: 20]), size: 30)!
// let paragraphStyle = NSMutableParagraphStyle()
// paragraphStyle.alignment = .right
// label.attributedText = NSAttributedString(string: "\(i)", attributes: [.font: font, .foregroundColor: SKColor.labelColor, .paragraphStyle: paragraphStyle])
i += 5
}
}
}
With AppKit, when using SpriteKit's default font HelveticaNeue-UltraLight, this issue doesn't exist, regardless whether I set the fixedAdvance font attribute.
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let font = NSFont(descriptor: NSFontDescriptor(fontAttributes: [.name: "HelveticaNeue-UltraLight"]), size: 30)!
// let font = NSFont(descriptor: NSFontDescriptor(fontAttributes: [.name: "HelveticaNeue-Light", .fixedAdvance: 20]), size: 30)!
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .right
let textField = NSTextField(labelWithString: "")
textField.font = font
textField.alignment = .right
// textField.alignment = .left
textField.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
view.addSubview(textField)
var i = 11
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { _ in
textField.stringValue = "\(i)"
// textField.attributedStringValue = NSAttributedString(string: "\(i)", attributes: [.font: font, .paragraphStyle: paragraphStyle])
i += 5
}
}
}
Is there a solution to this problem?
I filed FB15553700.
This game is where you can play over 100 games and every game is very different and unique and you can save your favorite game over the 100 in store them and you can store over 100 if you like them all make your wildest dreams that you can search up as games and they could have them Youtubers, you can make good videos with this game, the Creator.
:D
Hope you enjoy it also I’m a kid so I don’t know how to make an update.
Hi all,
I'm new to swift and I've just gotten started by making a simple pong-like game using SpriteKit. I'm trying to use the scroll wheel input to spin an object, however, nothing seems to make this work. From my googling and AI advice the way I've been doing it should, as shown in the snippet below, however debugging suggests the scrollWheel function isn't even being called.
#if os(OSX)
extension GameScene {
override func scrollWheel(with event: NSEvent ) {
print("Scroll detected: \(event.scrollingDeltaY)")
let scrollDelta = event.scrollingDeltaY
self.rotatePaddle(value: scrollDelta)
}
}
#endif
I have changed the build targets in Xcode to Mac os, not the designed-for-ipad version of it, and the app does launch and draw sprites correctly as well as detect other mouse or trackpad inputs like mouseDown and rightMouseDown, so it only seems to be this one specific input not working. I've also tried hardware debugging like restarting Xcode and my Mac, but no luck there either. Maybe I've missed something or am doing it completely wrong, so any help would be much appreciated.
Thanks heaps
What are the specific characteristics that trigger Game Mode in an iOS game? I have several casual SpriteKit games in the App Store but only one of them triggers Game Mode.
What does GCSupportsGameMode do when set to true? Will it trigger Game Mode or will the OS still decide by itself?
On macOS, system symbols displays in a SKTexture as expected, with the correct color and aspect ratio.
But on iOS they are always displayed in black, and sometimes with slightly wrong aspect ratio.
Is there a solution to this problem?
import SpriteKit
#if os(macOS)
import AppKit
#else
import UIKit
#endif
class GameScene: SKScene {
override func didMove(to view: SKView) {
let systemImage = "square.and.arrow.up"
let width = 400.0
#if os(macOS)
let image = NSImage(systemSymbolName: systemImage, accessibilityDescription: nil)!.withSymbolConfiguration(.init(hierarchicalColor: .white))!
let scale = NSScreen.main!.backingScaleFactor
image.size = CGSize(width: width * scale, height: width / image.size.width * image.size.height * scale)
#else
let image = UIImage(systemName: systemImage)!.applyingSymbolConfiguration(.init(pointSize: width))!.applyingSymbolConfiguration(.init(hierarchicalColor: .white))!
#endif
let texture = SKTexture(image: image)
print(image.size, texture.size(), image.size.width / image.size.height)
let size = CGSize(width: width, height: width / image.size.width * image.size.height)
addChild(SKSpriteNode(texture: texture, size: size))
}
}
Good morning everyone,
I'm building a simple game (my first game) using SwiftUI and SpriteKit that contains multiple views. I'm writing my game based on a main scene loaded into the GameView using a SpriteView. From there, using buttons, I move from one scene to another using self.scene?.view?.presentScene(...) and also with some cool transitions (.crossFade(withDuration: 0.5))).
But I'm not sure if this is the best approach. I would need some guidance because I cannot find any material discussing the best way to create a proper navigation with SpriteKit.
Do you have an updated article, tutorial, or reference that I can follow to learn about the best way to implement navigation in a SpriteKit game?
What I'm doing right now is working, but I have limitations, for example, if I want to mix SwiftUI views and SpriteKit scenes. I want to add a Credits scene with some text and images that I want to do in SwiftUI and a Statistic scene with some cool graphics to show the players, but I don't know if I can navigate from an SKScene into a View, or if I need a completely different approach. Can I add UI components directly in a SpriteKit scene instead of using a different navigation system and full SwiftUI views?
I really appreciate any help you can provide. As you can see, I'm a little bit lost 😅
Thanks a lot in advance 🙏
How do I set the static position of a SKSpriteNode so that it tilts toward the UIBezierPath as if it were following this Path?
When I first start my App, these Nodes are all aligned in a straight line
When I call:
var trainAction = SKAction.follow(trainPath.cgPath,
asOffset: false,
orientToPath: true,
speed: thisSpeed)
for moving the train, the train + each car will orient its tilt to hug the trainPath.
But I want the identical tilt to hug the trainPath for its initial static presentation.
How do I do that?
Can you set isPaused = true of a SKSpriteNode and keep its SKEmitterNode still moving?
Hi,
I’m looking for a way to keep some custom buttons in SpriteKit the same physical size (inches) accross iOS devices (or only slightly vary their size so they’re not humongous on large screens).
How do I get PPI in Swift? (cannot be library code which doesn’t compile in Swift Playgrounds).
I will use PPI for determining total screen size which I will use to determine how to adjust the button sizes while also respecting some physical desirable dimensions for the buttons.
I'm only asking for handheld (same distance from eyes to screen) use, so I don't care about Apple TV (longer distance).
So I have an SKScene in SpriteKit, and I am trying to export the scene as an image, but have no idea how.
https://developer.apple.com/documentation/spritekit/sktransition/configuring_whether_animations_play_during_the_transition
the figure 1 Frame progression during transitions,the last pair of pauseIncomingScene and pauseOutgoingScene. the pauseOutgoingScene should be false not true
Calling SKAction.follow(..) causes my SKSpriteNode to rotate 90 degrees CW and not stay horizontal as it follows my UIBezierPath?
I have this code (within my GameViewController Class) which implements the following of a SKSpriteNode along a UIBezierPath.
=====
Please note that a brilliant contributor solved the above challenge by creating a new Class, e.g., class NewClass: NSObject. Nevertheless, I need the solution to appear in an extension of my GameViewController
=====
func createTrainPath() {
trackRect = CGRect(x: tracksPosX - tracksWidth/2,
y: tracksPosY,
width: tracksWidth,
height: tracksHeight)
trainPath = UIBezierPath(ovalIn: trackRect)
} // createTrainPath
func startFollowTrainPath() {
var trainAction = SKAction.follow(
trainPath.cgPath,
asOffset: false,
orientToPath: true,
speed: theSpeed)
trainAction = SKAction.repeatForever(trainAction)
myTrain.run(trainAction, withKey: runTrainKey)
} // startFollowTrainPath
func stopFollowTrainPath() {
guard myTrain == nil else {
myTrain.removeAction(forKey: runTrainKey)
savedTrainPosition = getPositionFor(myTrain, orPath: trainPath)
return
}
} // stopFollowTrainPath
I'm trying to add an image in UIImage format to SpritKit's SKSpriteNode.
When converting a UIImage into a texture to SKTexture and adding it to SKSpriteNode, the image that takes into account the orientation of the image held by the UIImage is not displayed on the screen.
I tried the code below, but the result is identical.
Code1:
let image: UIImage?
let texture: SKTexture = SKTexture(image: image!)
ver imageNode = SKSpriteNode(texture: texture)
Code2:
let image: UIImage?
let cgImage = image?.cgImage
let ciImage = CIImage(cgImage: cgImage!)
let orientedImage = UIImage(cgImage: CIContext(options: nil).createCGImage(ciImage, from: ciImage.extent)!, scale: 0, orientation: image!.imageOrientation)
let texture: SKTexture = SKTexture(image: orientedImage)
ver imageNode = SKSpriteNode(texture: texture)
Code3:
let image: UIImage?
guard let cgImage = image?.cgImage else { return }
let orientedImage = UIImage(cgImage: cgImage, scale: image!.scale, orientation: .up)
let texture = SKTexture(image: orientedImage)
ver imageNode = SKSpriteNode(texture: texture)
Is there a way to ensure that the image orientation is taken into account when displayed?
I am trying to make an application for the Vision Pro where the particles don't move but rather stay still so that there is no lag. For example I am trying to spawn in a 100 particles here:
I want the particles to remain static but spawning in many causes the simulator to lag. Also is there maybe a way i can get a particle system to follow a specific shape like the one i have in the image.
Currently, I have multiple model entities that take on a particle system component
for i in 0..<100 {
let newEntity = ModelEntity()
var particleSystem = particleSystem(color: newColor)
newEntity.components.set(particleSystem)
newEntity.position = position
newEntity.scale = scale
stars.append(newEntity)
}
}
func particleSystem(color: UIColor) -> ParticleEmitterComponent {
var particles = ParticleEmitterComponent()
particles.emitterShapeSize = .init(repeating: 0.02)
// make burst smaller
particles.emitterShape = .sphere
particles.mainEmitter.birthRate = 1
particles.mainEmitter.lifeSpan = 2
particles.mainEmitter.size = 0.02
particles.burstCount = 50
particles.speed = 0.01
particles.mainEmitter.isLightingEnabled = false
particles.mainEmitter.color = .constant(.single(color))
return particles
}
An SCNNode is created and used for either an SCNView or an SKView.
SceneKit and SpriteKit are using default values.
The SceneView has an SCNScene with a rootNode of the SCNNode.
The SpriteKitView has a SpriteKitScene with an SK3DNode that has an SCNScene with a rootNode of the SCNNode.
There is no other code changing or adding values.
Why are the colors for the SCNView less vibrant than the colors for the SKView?
Is there a default to change to make them equivalent, or another value to add? I have tried changing the default SCNMaterial but only succeeded in making the image black or dark.
Any help is appreciated.
How do I change a UIBezierPath.currentPoint to a SKSpriteNode.position?
Here are the appropriate code snippets:
func createTrainPath() {
let startX = -tracksWidth/2,
startY = tracksPosY
savedTrainPosition = CGPoint(x: startX, y: startY!)
trackRect = CGRect(x: savedTrainPosition.x,
y: savedTrainPosition.y,
width: tracksWidth,
height: tracksHeight)
trainPath = UIBezierPath(ovalIn: trackRect)
trainPath = trainPath.reversing() // makes myTrain move CW
} // createTrainPath
Followed by:
func startFollowTrainPath() {
let theSpeed = Double(5*thisSpeed)
var trainAction = SKAction.follow(
trainPath.cgPath,
asOffset: false,
orientToPath: true,
speed: theSpeed)
trainAction = SKAction.repeatForever(trainAction)
createPivotNodeFor(myTrain)
myTrain.run(trainAction, withKey: runTrainKey)
} // startFollowTrainPath
So far, so good (I think?) ...
Within other places in my code, I call:
return trainPath.currentPoint
I need to convert trainPath.currentPoint to myTrain.position ...
When I insert the appropriate print statements, I see for example:
myTrain.position = (0.0, -295.05999755859375)
trainPath.currentPoint = (392.0, -385.0)
which obviously disqualifies a simple = , as in:
myTrain.position = trainPath.currentPoint
Since this = is not correct, what is ?
After more investigation, my guess is that .currentPoint is in SKSpriteNode coordinates and .position is in SKScene coordinates.
I am using the following code to create a texture atlas at runtime using a single .png image sprite sheet:
func createSpriteTextureAtlas(atlasNumber atlas:Int, forWorld world:Int) {
//load the png file
let image = UIImage(named: "world\(world)_spritesheet\(atlas)_2048x2048.png")
//create the dictonary
var imageDictionary = [String: UIImage]()
//iterate through all rows and columns and get the subimage
var imageIndex = 0
for row in 0...7 {
for column in 0...7 {
let sourceRect = CGRect(x:column * 256, y:row * 256, width:256, height:256)
let sourceImage = image?.cgImage!.cropping(to: sourceRect)
let subImage = UIImage(cgImage: sourceImage!)
//add the sub image and name to the dictionary
imageDictionary["\(imageIndex)"] = subImage
imageIndex = imageIndex + 1
}
}
//create the texture atlas using the dictionary
spriteTextureAtlas[atlas] = SKTextureAtlas(dictionary: imageDictionary)
}
I have a different sprite sheet for every world. I made all the sprite sheets myself using the same tool. This code works 100% of the time for most images.
For some images however, the program crashes at: SKTextureAtlas(dictionary: imageDictionary) with the error: Thread 4: EXC_BAD_ACCESS (code=1, address=0x105ff2000). The stack trace says it is crashing inside: #0 0x00000002178e2d34 in -[SKTextureAtlasPacker isFullyOpaque:] ().
The crash does not happen every time and only happens for some images. The crash never happens on the simulator.
Did I make a mistake inside createSpriteTextureAtlas or is this a SpriteKit bug?
P.S. I already know that I can let Xcode make the texture atlas for me by using a folder with a .atlas extension but this is not what i want to do.
I have implemented auto renewable subscriptions in my app, as well as promo codes. Purchase of subscriptions both monthly and annual; work correctly. What I don't know is what to "listen for" instead of product, when the user uses a promo code to purchase the product. am I looking for a different product code? or product identifier when the offer code is used to subscribe?
Hello everyone
I am porting my existing 2d game writing by spritekit to visionOS
and I am creating a SpriteView in WindowGroup
let currentScene = BattleScene.newGameScene(gameMode: "endless", dataContext: dataController.container.viewContext)
SpriteView(scene: currentScene)
.ignoresSafeArea(.all)
.frame(width: currentScene.frame.width, height: currentScene.frame.height, alignment: .center)
.onReceive(NotificationCenter.default.publisher(for: GameoverNotification)) { _ in
stopAllAudio()
}
.onTapGesture { location in
let viewPosition = location
let touchLocation = CGPoint(x: viewPosition.x, y: viewPosition.y)
print("touch on vision window: ", touchLocation.x, touchLocation.y)
}
.glassBackgroundEffect()
//WindowGameView()
// .environment(\.managedObjectContext, dataController.container.viewContext)
// .environment(model)
// .environment(pressedKeys)
}
.windowStyle(.automatic)
.defaultSize(width: 0.5, height: 1.0, depth: 0.0, in: .meters)
run it and it turns out the scene can't receive tap event.
but it works normal if I run it with my ios target (vision Os designd for ipad)
is there anything I missed?