Physics Body 边缘 IOS8,Sprite Kit
Posted
技术标签:
【中文标题】Physics Body 边缘 IOS8,Sprite Kit【英文标题】:Physics Body edges IOS8, Sprite Kit 【发布时间】:2014-10-17 10:46:30 【问题描述】:所以按照 ios 游戏教程,它的 IO7 为 8 还不是很多。
下面的代码应该是一个弹跳的球,但只在屏幕的顶部和底部弹跳,如果它碰到侧边,球就会离开屏幕
#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
这里必须有一些简单的东西,但我似乎无法找到堆栈溢出的答案
【问题讨论】:
【参考方案1】:使用 NSLog 检查视图的大小:NSLog(@"%f,%f",self.size.width,self.size.height);
SpriteKit 存在一个问题,导致视图尺寸与您的预期不一致。如果这是您的情况,请确保将视图的大小更改为正确的大小:
-(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 */
【讨论】:
以上是关于Physics Body 边缘 IOS8,Sprite Kit的主要内容,如果未能解决你的问题,请参考以下文章
边缘检测 --> CGPath --> SpriteKite 碰撞
unity Physics/Physics2DProjectSettings中LayerCollisionMatrix的存储方式