Sprite Node leaving frame - sprite-kit

I've set the physics body for both the background and the other sprite node, yet the node can still leave the the border. It doesn't fall through the bottom, it can leave on the sides though.
SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:#"bg"];
bg.position = CGPointMake(self.scene.size.width/2, self.scene.size.height/2);
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.contactDelegate = self;
[self addChild:bg];
SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:#"ball"];
player.position = CGPointMake(self.frame.size.width*.5, 200);
player.name = #"player";
player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.contactTestBitMask = barCategory;
[self addChild:player];
What i think is happening is the body of the the background node is wider than what the view is showing. How can i make it so that the body dimension is what ever the view is.

Change the initialisation line of GameScene in GameViewController to
GameScene *scene = [[GameScene alloc]
initWithSize:CGSizeMake(self.view.frame.size.width,
self.view.frame.size.height)];

Related

Prevent SKSpriteNode from bouncing off other SKSpriteNode

I have a SKSpriteNode that collides and bounces off another SKSpriteNode. What I want is to be able to detect the collision, but not have it bounce off. I want it to bounce off other nodes, but not this one. Is that possible?
self.physicsWorld.contactDelegate = self;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
self.zone = [SKSpriteNode spriteNodeWithImageNamed:#"Oval.png"];
self.zone.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame)); self.zone.xScale = 0.3;
self.zone.yScale = 0.3;
self.zone.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.zone.frame.size.width/2];
self.zone.physicsBody.restitution = 0.0f;
self.zone.physicsBody.density = 0;
self.zone.physicsBody.friction = 0.4f;
self.zone.physicsBody.categoryBitMask = zoneCategory;
self.zone.physicsBody.dynamic = NO;
self.ball = [SKSpriteNode spriteNodeWithImageNamed:#"Ball.png"];
self.ball.position = CGPointMake(80,0);
self.ball.name = #"BallNode";//how the node is identified later
self.ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.ball.frame.size.width/2];
self.ball.physicsBody.restitution = 0.1f;
self.ball.physicsBody.friction = 0.4f;
self.ball.physicsBody.categoryBitMask = ballCategory;
self.ball.physicsBody.dynamic = NO;
I don't want ball and zone to bounce. Any ideas?
set collisionBitMask and contactTestBitMask of those two bodies
like:
self.zone.collisionBitMask = 0xFFFFFFFF & (~ballCategory);
self.ball.collisionBitMask = 0xFFFFFFFF & (~zoneCategory);
self.zone.contactTestBitMask = ballCategory;
then when contact occurs expect call of didBeginContact: in your SKPhysicsContactDelegate.
There will be no collsion.

Impulse doesn't apply in Sprite Kit

I'm attempting to apply an impulse to a sprite node in SK every time I touch, but nothing is happening. The touch is registering, but there is no impulse.
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
SKSpriteNode *torso = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(40, 60)];
torso.position = CGPointMake (250,250);
torso.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 60)];
torso.physicsBody.dynamic = YES;
[self addChild: torso];
SKSpriteNode *arm = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(15, 50)];
arm.position = CGPointMake (235,235);
arm.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 60)];
arm.physicsBody.dynamic = YES;
[self addChild: arm];
SKPhysicsJointPin *leftArm = [SKPhysicsJointPin jointWithBodyA:torso.physicsBody bodyB:arm.physicsBody anchor:torso.position];
[self.physicsWorld addJoint:leftArm];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[_torso.physicsBody applyImpulse:CGVectorMake(12, 10)];
NSLog(#"touch");
}
My sprite node does have another node linked to it, but I tried the impulse on it without the additional node and it still did not work.
For some reason, in order to move my sprite node I had to create it like this
_torso = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(40, 60)];
rather than
SKSpriteNode *torso = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(40, 60)];
Once I set a breakpoint and saw that _torso was nil, I referred to some previous work and saw my mistake.

How to synchronize circle (with SKAction by SKShapeNode) to ball (by SKShapeNode)?

I try to synchronize circle (create with SKAction by SKShapeNode) to ball (create by SKShapeNode).
Here is this code. But it couldn't work well.
What should I do?
-(void)makeContents{
//make ground
UIBezierPath *groundPath = [UIBezierPath bezierPath];
[groundPath moveToPoint:CGPointMake(0,250)];
[groundPath addLineToPoint:CGPointMake(568,100)];
SKShapeNode *ground = [[SKShapeNode alloc] init];
ground.path = groundPath.CGPath;
[self addChild:ground];
//make ball
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:#"ball.jpeg"];
ball.position = CGPointMake(10, 320);
ball.size = CGSizeMake(20, 20);
ball.physicsBody.dynamic = YES;
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:3];
[self addChild:ball];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//make circle
UIBezierPath *circlePath = [UIBezierPath bezierPath];
[circlePath addArcWithCenter:ball.position radius:10 startAngle:0 endAngle:2*M_PI clockwise:NO];
circle = [[SKShapeNode alloc] init];
circle.path = circlePath.CGPath;
circle.strokeColor = [SKColor blackColor];
//this line added after first post.
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10 center:ball.postion];
[self addChild:circle];
//make action
SKAction *scale = [SKAction sequence:#[[SKAction scaleBy:0.1 duration:2],
[SKAction scaleBy:0 duration:0.01]]];
[circle runAction:[SKAction repeatActionForever:scale]];
}
Thank you in advance for your help.

Not able to apply impulse to SKSpriteNode physics body

I am new to xcode's sprite kit and I'm an trying to apply an impulse to a SKSpriteNode's physics body.
Here is how I create the scene:
self.backgroundColor = [SKColor colorWithRed:0 green:0 blue:0 alpha:1.0];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.friction = 0.0f;
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
ballCategory = 1;
wallCategory = 2;
self.physicsBody.categoryBitMask = wallCategory;
Here is how I create the player AND give it its impulse:
player = [SKSpriteNode spriteNodeWithImageNamed:filePath];
player.size = CGSizeMake(100, 100);
player.position = CGPointMake(150, 250);
player.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:player.frame];
player.physicsBody.categoryBitMask = ballCategory;
player.physicsBody.collisionBitMask = wallCategory;
player.physicsBody.friction = 0.0f;
player.physicsBody.restitution = 1.0f;
player.physicsBody.linearDamping = 0.0f;
player.physicsBody.allowsRotation = NO;
[self addChild:player];
CGVector impulse = CGVectorMake(100,100);
[player.physicsBody applyImpulse:impulse];
Is there anything obvious that I am missing because I've been following the iOS Developer Library Sprite Kit Programming Guide perfectly? Thanks in advance for the help!
You are creating the player's physicsBody as an edge body - these are always static (immovable by forces/impulses). You should change that line to:
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.frame.size];
This example makes it a dynamic volume based on a rectangle which will be affected by physics forces. You can read up on the types of physics bodies here.

cocos2d CCScene background orientation

I'm trying to add a background image for my scene using the following code:
CCSprite* background = [CCSprite spriteWithFile:#"background-hd.png"];
background.tag = 1;
background.anchorPoint = CGPointMake(0,0);
[self addChild:background];
My apps orientation is landscape and the image's orientation is landscape as well, however I get the image orientated in portrait:
I think this is the solution with Layer which set the scene with orientation...
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
StartLayer *layer = [StartLayer node];
CCSprite *background = [CCSprite spriteWithFile:#"background-hd.png"];
background.anchorPoint = ccp(0,0);
[layer addChild:background z:-1];
[scene addChild: layer];
return scene;
}
See this link for More information Cocos2d-Scenes-and-Layers....
Hope this helpful to you...