简体   繁体   中英

Physics Body edges IOS8, Sprite Kit

SO following an IOS game tutorial, its IO7 as not many for 8 are out yet.

The following code is supposed to be a ball bouncing around but only bounces on the top and bottom of the screen, if it hits the side edges the ball goes of screen

#import "GameScene.h"

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */

self.backgroundColor =[SKColor  whiteColor];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.gravity = CGVectorMake(0, 0);

SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

ball.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));

ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];

//ball.physicsBody.friction = 0;
ball.physicsBody.linearDamping = 0;
ball.physicsBody.restitution = 1;


[self addChild:ball];

CGVector myVector = CGVectorMake(0, 5);
[ball.physicsBody applyImpulse:myVector];
}



-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

There has to be something simple here but i can't seem to find an answer on stack overflow

Use NSLog to check the size of your view: NSLog(@"%f,%f",self.size.width,self.size.height);

There is a problem with SpriteKit that causes the view dimensions to be off of what you expect. If this is what is happening in your case, then make sure to change the size of your view to the correct one:

-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */

self.size = self.view.frame.size;
self.backgroundColor =[SKColor  whiteColor];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.gravity = CGVectorMake(0, 0);

SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

ball.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));

ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];

//ball.physicsBody.friction = 0;
ball.physicsBody.linearDamping = 0;
ball.physicsBody.restitution = 1;


[self addChild:ball];

CGVector myVector = CGVectorMake(50, 20);
[ball.physicsBody applyImpulse:myVector];
}



-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM