无法指定类型为Player的值来键入SKSpriteNode

时间:2023-01-22 23:55:22

I am working on my game everything seems to be going great, but I keep getting this error message This is the piece of code the error is on. Player is a class

我正在研究我的游戏一切似乎都很好,但我不断收到此错误消息这是错误所在的代码片段。玩家是一个班级

player = Player(node: player, particles: playerParticles!)

player = Player(节点:玩家,粒子:playerParticles!)

 func addPlayer() {
    var player = SKSpriteNode(imageNamed: "player.png")
    player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
    player.physicsBody!.affectedByGravity = false
    player.physicsBody!.allowsRotation = false
    player.physicsBody!.categoryBitMask = ColliderType.Plyer.rawValue //sets value to 1
    player.physicsBody!.contactTestBitMask = ColliderType.Enemy.rawValue //will make contact with any value 2
    player.physicsBody!.collisionBitMask = ColliderType.Enemy.rawValue //will collide with any value 2
    let playerParticles = SKEmitterNode(fileNamed: "")
    playerParticles?.isHidden = true
    player = Player(node: player, particles: playerParticles!)
    player.addChild(playerParticles!)
    addChild(player)

Here is the player class:

这是玩家类:

import Foundation
import SpriteKit

class Player {
    var playerr:SKSpriteNode
    var speed = 0.1
    var showParticles = false
    var particlesFrameCount = 0
    var maxParticleFrames = 20
    var particles:SKEmitterNode
    init(node:SKSpriteNode, particles:SKEmitterNode) {
        self.playerr = node
        self.particles = particles
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

1 个解决方案

#1


2  

Try something like this

尝试这样的事情

func addPlayer() {
var player = SKSpriteNode(imageNamed: "player.png")
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
player.physicsBody!.affectedByGravity = false
player.physicsBody!.allowsRotation = false
player.physicsBody!.categoryBitMask = ColliderType.Plyer.rawValue //sets value to 1
player.physicsBody!.contactTestBitMask = ColliderType.Enemy.rawValue //will make contact with any value 2
player.physicsBody!.collisionBitMask = ColliderType.Enemy.rawValue //will collide with any value 2
  self.addChild(player)

let playerParticles = SKEmitterNode(fileNamed: "")
playerParticles?.isHidden = true
self.addChild(playerParticles)

#1


2  

Try something like this

尝试这样的事情

func addPlayer() {
var player = SKSpriteNode(imageNamed: "player.png")
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
player.physicsBody!.affectedByGravity = false
player.physicsBody!.allowsRotation = false
player.physicsBody!.categoryBitMask = ColliderType.Plyer.rawValue //sets value to 1
player.physicsBody!.contactTestBitMask = ColliderType.Enemy.rawValue //will make contact with any value 2
player.physicsBody!.collisionBitMask = ColliderType.Enemy.rawValue //will collide with any value 2
  self.addChild(player)

let playerParticles = SKEmitterNode(fileNamed: "")
playerParticles?.isHidden = true
self.addChild(playerParticles)