0

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

Paul
  • 6,108
  • 14
  • 72
  • 128
  • Couple of points to consider... Is the texture you provided a SKTexture or just an image? Have you "cleaned" your project? If the image is not a PNG image, have you tried another file format? – sangony Apr 22 '15 at 16:43
  • @sangony Thanks for the comment, I cleaned the project, it is an image, a png. I am using SceneKit, with a spriteKit class which is linked with sceneKit via `overlaySKScene`. The path for the image is correct though, because the particle system shows the correct image. Then, I don't know why it does not read it? – Paul Apr 22 '15 at 17:20
  • I don't know your code so perhaps you have already tried this... You have to store your image as a SKTexture. If you are using an image directly it might be the root of your issue. – sangony Apr 22 '15 at 17:39
  • @sangony thanks, how do you link the two : SKEmitterNode and the SKTexture? I have edited my post with some code. – Paul Apr 22 '15 at 18:46
  • I assume you are setting your particle emitter's texture property with you own image. If that's the case you will have to use something like this: SKTexture *myTexture = [SKTexture textureWithImageNamed:@"myPicture.png"]; – sangony Apr 22 '15 at 19:02
  • @sangony yes but then, how to tell the emitter I want this texture? I added the texture from the particle system window, not by code, and I don't find a way (like a variable `emitter.image = ...`, `emitter.texture = ...`, in the documentation? – Paul Apr 22 '15 at 19:39
  • My bad, I thought you were doing all this from code. Not sure how to fix this issue now... – sangony Apr 22 '15 at 20:41
  • @sangony Actually you were right, I can just add : `myEmitter.particleTexture = mySkTexture` and the texture would be set in `Images.xcassets` and it works! If you want to post it as an answer, I will mark it. Otherwise, thanks a lot for the support. – Paul Apr 23 '15 at 10:02
  • Happy to hear you got it working! – sangony Apr 23 '15 at 12:33

1 Answers1

1

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.

sangony
  • 11,636
  • 4
  • 39
  • 55