TerranceW
TerranceW

Reputation: 286

iOS Texture rendering

I have a texture that I am trying to implement in a SpriteKit game:

https://i.sstatic.net/esQHe.png

As you can see, there are two horizontal bars. The SpriteNode is supposed to go through these two bars, and they are both supposed to be solid and make contact with the sprite.

However, the top bar, for some reason, does not provide any collision with the spriteNode, and the SpriteNode simply passes through it, the BOTTOM bar works exactly as it should, I.E. the spriteNode can make contact with it.

It is all one file though, so this makes absolutely no sense to me. Why is only the bottom bar rendering.

Here is my code for the texture:

let pointnodeTexture = SKTexture(imageNamed: "pointnode")

let pointnode = SKSpriteNode(texture: pointnodeTexture, size:pointnodeTexture.size())

pointnode.physicsBody = SKPhysicsBody(texture: runningbarTexture, size: runningbarTexture.size())
pointnode.physicsBody?.affectedByGravity = false
pointnode.physicsBody?.pinned = true
pointnode.physicsBody?.dynamic = false

pointnode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) / 1.5)

Are there any solutions to this? Thanks.

Upvotes: 0

Views: 85

Answers (1)

Skyler Lauren
Skyler Lauren

Reputation: 3812

I believe the issue is in how

pointnode.physicsBody = SKPhysicsBody(texture: runningbarTexture, size: runningbarTexture.size())

actually works. If I remember correctly making a physics body from a texture will try to create 1 shape and start from the lower left corner. Because you have transparent pixels it is only drawing around the bottom bar.

To get the results you want you will have to split it up into two separate sprites. Now if you still want to control the position together you could try making two bar sprites and adding them to a parent node and move the parent around.

Hopefully that helps.

Edit

It would looks something like this...

    let pointnodeTexture = SKTexture(imageNamed: "pointnode") //single bar texture

    let pointnode = SKNode()

    let distanceApart : CGFloat = 20.0

    let bar1 = SKSpriteNode(texture: pointnodeTexture, size:pointnodeTexture.size())

    bar1.physicsBody = SKPhysicsBody(texture: bar1.texture, size: bar1.size)
    bar1.physicsBody?.affectedByGravity = false
    bar1.physicsBody?.pinned = true
    bar1.physicsBody?.dynamic = false
    bar1.position = CGPointMake(0.0, distanceApart)

    pointnode.addChild(bar1)

    let bar2 = SKSpriteNode(texture: pointnodeTexture, size:pointnodeTexture.size())

    bar2.physicsBody = SKPhysicsBody(texture: bar2.texture, size: bar2.size)
    bar2.physicsBody?.affectedByGravity = false
    bar2.physicsBody?.pinned = true
    bar2.physicsBody?.dynamic = false
    bar2.position = CGPointMake(0.0, -distanceApart)

    pointnode.addChild(bar2)

    pointnode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) / 1.5)

I also noticed in your original code you are using a different texture for your physics body than what you used to create your sprite. pointnodeTexture vs runningbarTexture not sure which texture you wanted but you should use the same textures for both creating the sprite and the physics body.

Hopefully this will get you pointed in the right direction.

Upvotes: 1

Related Questions