iOS7 SKScene如何让精灵从屏幕边缘反弹?

时间:2013-11-09 22:26:51

标签: ios7 sprite-kit skscene skphysicsworld

我在iPad的屏幕上弹出一个带有球的游戏。类似于Pong游戏。我看到SKScene的SKPhysicsWorld具有重力属性,还可以控制物体相互碰撞的方式。

有没有什么方法可以自动检测精灵的边缘是否与屏幕的边缘发生碰撞,所以它可以从那里反弹?或者我是否需要写我自己的碰撞代码?

4 个答案:

答案 0 :(得分:10)

您无需编写太多代码即可让球从屏幕边缘反弹。物理环境可以处理上述所有内容,你只需要以正确的方式实例化sprite。

首先,您必须设置场景的physicsBody:

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一个[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

答案 1 :(得分:7)

您是否尝试过设置SKScene的物理主体

[self setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]];  //Physics body of Scene

这应该让你的节点保持在框架内。

w ^

答案 2 :(得分:2)

这来自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)];

答案 3 :(得分:2)

这将使你的节点进入Swift中的框架:

self.physicsBody = SKPhysicsBody (edgeLoopFromRect: self.frame)