Learn Courses My Dashboard

Not sure why the playground isn't working, but it works in the tutorial


Hey y’all. Apple has just announced the Swift Student Challenge 2021, and I wanted to enter but for some reason, my playground is giving me bugs. I am using version 3.4.1 Swift Playgrounds for Mac, and I also tried this on iPad. Both are giving me the same error. I am following a tutorial, and for her, there is no error. Please help me!

PS: I tagged everyone who helped me last time, so sorry if this is an inconvenience.

My main view:

import SpriteKit
import PlaygroundSupport

let skView = SKView(frame: .zero)

let GameScene = gameScene(size: UIScreen.main.bounds.size)
GameScene.scaleMode = .aspectFit

PlaygroundPage.current.liveView = skView
PlaygroundPage.current.wantsFullScreenLiveView = true

import SpriteKit

public class gameScene: SKScene, SKPhysicsContactDelegate {
    var player: SKSpriteNode!
    public override func didMove(to view: SKView) {
        physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
        physicsBody?.friction = 0.0
        physicsWorld.contactDelegate = self
        let bg = SKSpriteNode(texture: SKTexture(image: #imageLiteral(resourceName: "Dangerous Duos தமிழ்.png")))
        bg.zPosition = -10
        bg.position = CGPoint(x: frame.midX, y: frame.midY)
        player = SKSpriteNode(texture: SKTexture(image: #imageLiteral(resourceName: "chkbiriyani.png") ), color: .clear, size: CGSize(width: size.width = 0.05, height: size.height = 0.05))
//The error arises in the line "CGSize(width: size.width = 0.05, height: size.height = 0.05)"
        player.position = CGPoint(x: frame.midX, y: frame.midY)

Where is says #imageLiteral, that is just coming up when I copy and paste my code.

I can’t really help cause I haven’t done too much with SpriteKit

But also no need to tag people in the post, this forum is open for anyone to answer and by tagging certain people, others think they can’t chime in.

Have you tried quitting Xcode, opening it, rebooting your Mac, cleaning the derived data folder, etc?

What are you trying to do here? You need to supply a CGSize to the width and height parameters. So either:

size.width = 0.05
size.height = 0.05
CGSize(width: size.width, height: size.height)


CGSize(width: 0.05, height: 0.05)

But not both. You can’t assign values to size.width and size.height at the same time you are using them as parameters to another method. And is 0.05 really the value you want? That’s really tiny.

Also, you should rename both your gameScene class and the GameScene variable; they are reversed from convention. The convention in Swift is for types (like class names) to be named starting with a capital letter and variables to start with a lowercase letter. Following those conventions will make your code easier for others to read and understand and, presumably, will matter when Apple is judging the Student Challenge.

1 Like