iOS7 SKScene如何让精灵从屏幕边缘反弹? [英] iOS7 SKScene how to make a sprite bounce off the edge of the screen?

查看:112
本文介绍了iOS7 SKScene如何让精灵从屏幕边缘反弹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建造一个带有球在iPad屏幕内弹跳的游戏。类似于Pong游戏。我看到SKScene的SKPhysicsWorld具有重力属性,并且还控制物体彼此碰撞的方式。

I'm building a game with balls bouncing within the iPad's screen. Similar to a Pong game. I see that SKScene's SKPhysicsWorld has gravity property, and also controls how objects collide with each other.

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

Is there some way I can automatically detect if a sprite's edge has collided with the screen's edge, so it can bounce off that? Or do I need to write my own collision code?

推荐答案

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

You don't need to write much code to make the ball bounce off the edge of the screen. the physics environment can handle all of the above, you just need to instantiate the sprites in the correct way.

首先,你必须将场景的physicsBody设置为这样:

First, you will have to set the physicsBody of the scene as such:

self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

在场景的.h文件中创建两个位掩码类别:

In your scene's .h file create two bitmask categories:

static const uint8_t ballCategory = 1;
static const uint8_t wallCategory = 2;

并为场景的物理主体分类:

And give the scene's physics body a category:

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:] ,对它的物理身体施加冲动。

Instead of giving the spriteNode a [SKAction moveTo: duration:], apply an impulse to it's physics body.

CGVector impulse = CGVectorMake(1.0,1.0);
[spriteNode.physicsBody applyImpulse:impulse];

瞧!球将从墙上反弹而没有任何阻止它。

And voila! The ball will be bouncing off the walls with nothing to stop it.

这就是.h文件的样子:

This is what the .h file should look like:

#import <SpriteKit/SpriteKit.h>

static const uint8_t ballCategory = 1;
static const uint8_t wallCategory = 2;

@interface BallBounce : SKScene

@end

这就是.m文件的样子:

And this is how the .m file should look like:

#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

这篇关于iOS7 SKScene如何让精灵从屏幕边缘反弹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆