我正在构建一个球在 iPad 屏幕内弹跳的游戏。类似于乒乓球游戏。我看到 SKScene 的 SKPhysicsWorld 具有重力属性,并且还控制对象如何相互碰撞。
有什么方法可以自动检测精灵的边缘是否与屏幕的边缘碰撞,所以它可以反弹?还是我需要编写自己的碰撞代码?
我正在构建一个球在 iPad 屏幕内弹跳的游戏。类似于乒乓球游戏。我看到 SKScene 的 SKPhysicsWorld 具有重力属性,并且还控制对象如何相互碰撞。
有什么方法可以自动检测精灵的边缘是否与屏幕的边缘碰撞,所以它可以反弹?还是我需要编写自己的碰撞代码?
您无需编写太多代码即可使球从屏幕边缘反弹。物理环境可以处理上述所有情况,您只需要以正确的方式实例化精灵。
首先,您必须将场景的物理主体设置为:
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
在场景的 .h 文件中创建两个位掩码类别:
static const uint8_t ballCategory = 1;
static const uint8_t wallCategory = 2;
并给场景的物理体一个类别:
self.physicsBody.categoryBitMask = wallCategory;
然后创建你的精灵:
SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"ball.png"];
spriteNode.name = @"ball";
spriteNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
spriteNode.position = CGPointMake(10.0,10.0); //or anything you want
spriteNode.physicsBody.dynamic = YES;
spriteNode.physicsBody.affectedByGravity = NO;
spriteNode.physicsBody.categoryBitMask = ballCategory;
spriteNode.physicsBody.collisionBitMask = wallCategory;
spriteNode.physicsBody.restitution = 1.0;
spriteNode.physicsBody.friction = 0.0;
spriteNode.physicsBody.linearDamping = 0.0;
spriteNode.physicsBody.angularDamping = 0.0;
[self addChild:spriteNode];
不要给 spriteNode a [SKAction moveTo: duration:]
,而是对它的物理体施加一个脉冲。
CGVector impulse = CGVectorMake(1.0,1.0);
[spriteNode.physicsBody applyImpulse:impulse];
瞧!球会从墙上弹起,没有任何东西可以阻止它。
这是 .h 文件的样子:
#import <SpriteKit/SpriteKit.h>
static const uint8_t ballCategory = 1;
static const uint8_t wallCategory = 2;
@interface BallBounce : SKScene
@end
这就是 .m 文件的样子:
#import "BallBounce.h"
@implementation BallBounce
-(instancetype)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = wallCategory;
SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(50, 50)];
spriteNode.name = @"ball";
spriteNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
spriteNode.position = CGPointMake(10.0,10.0); //or anything you want
spriteNode.physicsBody.dynamic = YES;
spriteNode.physicsBody.affectedByGravity = NO;
spriteNode.physicsBody.categoryBitMask = ballCategory;
spriteNode.physicsBody.collisionBitMask = wallCategory;
spriteNode.physicsBody.restitution = 1.0;
spriteNode.physicsBody.friction = 0.0;
spriteNode.physicsBody.linearDamping = 0.0;
spriteNode.physicsBody.angularDamping = 0.0;
[self addChild:spriteNode];
CGVector impulse = CGVectorMake(100.0,100.0);
[spriteNode.physicsBody applyImpulse:impulse];
}
return self;
}
@end
你试过设置SKScene的物理体吗
[self setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]]; //Physics body of Scene
这应该使您的节点保持在框架内。
W
这是来自http://www.raywenderlich.com/49721/how-to-create-a-breakout-game-using-spritekit
SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
// 2 Set physicsBody of scene to borderBody
self.physicsBody = borderBody;
// 3 Set the friction of that physicsBody to 0
self.physicsBody.friction = 0.0f;
// 1
SKSpriteNode* ball = [SKSpriteNode spriteNodeWithImageNamed: @"Sphere.png"];
ball.name = @"ball";
ball.position = CGPointMake(self.frame.size.width/3, self.frame.size.height/3);
[self addChild:ball];
// 2
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
// 3
ball.physicsBody.friction = 0.0f;
// 4
ball.physicsBody.restitution = 1.0f;
// 5
ball.physicsBody.linearDamping = 0.0f;
// 6
ball.physicsBody.allowsRotation = NO;
ball.physicsBody.affectedByGravity = NO;
[ball.physicsBody applyImpulse:CGVectorMake(10.0f, 10.0f)];
这会将您的节点带入 Swift 的框架中:
self.physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame)