Reputation: 312
this is a objective-c code that work exactly for physics body collision around the screen
self.physicsWorld.gravity = CGVectorMake(10.0f, -10.0f);
// Create a physics body borders the screen
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
// Set physicsBody of scene to borderBody
self.physicsBody = borderBody;
SKSpriteNode* aa = [SKSpriteNode spriteNodeWithImageNamed:@"ca1.png"];
aa.position = CGPointMake(20, 20);
aa.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:aa.frame.size.width/2];
aa.physicsBody.friction = 0.0f;
aa.physicsBody.restitution = 1.0f;
aa.physicsBody.linearDamping = 0.0f;
[self addChild:aa];
that should be the equivalent for swift
self.physicsWorld.gravity = CGVectorMake(CGFloat(10.0), CGFloat(-10.0));
let physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame)
self.physicsBody = physicsBody
var sp=SKSpriteNode()
var atexture=SKTexture(imageNamed: "candycane.png")
sp=SKSpriteNode(texture: atexture)
sp.position=CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
sp.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2)
sp.physicsBody?.friction = 0
sp.physicsBody?.restitution = 1
sp.physicsBody?.linearDamping = 0
self.addChild(sp)
but work only for top and bottom the spritenode can go out the left and right
what is the problem ?. how i can have a boundary for the scene in swift ?.
Upvotes: 2
Views: 4836
Reputation: 603
I hope this will helpfull for you in Swift for Side and top bottom sprite collision.
self.physicsBody = SKPhysicsBody ( edgeLoopFromRect: self.frame )
Upvotes: 1
Reputation: 1155
Have you tried going into your GameViewController
and setting the following:
scene.scaleMode = SKSceneScaleMode.ResizeFill
I had a similar issue and this fixed it for me.
Upvotes: 0
Reputation: 312
so this is the magic trick
lets add this in GameViewController
let scene = GameScene(size: skView.bounds.size)
end get the same result of the objective-c all work fine
Upvotes: 2
Reputation: 312
for the moment i solved with this
let physicsBody = SKPhysicsBody (edgeLoopFromRect: CGRect(x: 300, y: 0, width: self.size.width-600, height: self.size.height))
and get the same result of objective-c code in the iphone 6 simulator but this its very bad solution
i will do that whit the right way
Upvotes: 0
Reputation: 312
the problem is that this swift code can't do the same effect to te next objective-c code
let physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame)
self.physicsBody = physicsBody
objective-c code
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody = borderBody;
objective-c code work swift no
Upvotes: 2