Search code examples
iossprite-kitparticle-systemsktexture

Error loading particle texture in Spritekit?


My particle texture is correctly showing in the particle system, in SpriteKit. But once the app is started, the image is not recognized :

SKTexture: Error loading image resource: "XP.png"

I tried to add the image in Images.xcassets, but it is not seen by the particle system. Or just directly in the same folder as the particle effect, but then the image is not recognized once the app is started.

Would you know how to fix this?

Edit:
Here is the code :

//2D particle
    func createSKSParticle(str:String)->SKEmitterNode{
        let path : String = NSBundle.mainBundle().pathForResource(str, ofType: "sks") as String!
        let particle : SKEmitterNode = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as SKEmitterNode
        return particle
    }

    func addParticle(nameParticle: String){
        let p : SKEmitterNode = createSKSParticle(nameParticle)
        let texture = SKTexture(imageNamed: "xpPart") //xpPart is in Images.xcassets .... 
        p.particleTexture = texture //HERE is the solution
        self.addChild(p)
    }

Thanks


Solution

  • Manually assigning a texture to the SKEmitterNode should resolve the issue:

    SKTexture *myTexture = [SKTexture textureWithImageNamed:@"myPicture.png"];
    

    More information can be found in the SKEmitterNode docs.