Ale*_*one 4 ios7 sprite-kit skscene skphysicsworld
我正在建造一个带有球在iPad屏幕内弹跳的游戏.类似于Pong游戏.我看到SKScene的SKPhysicsWorld具有重力属性,还可以控制物体相互碰撞的方式.
有没有什么方法可以自动检测精灵的边缘是否与屏幕边缘相撞,所以它可以反弹掉?或者我是否需要编写自己的碰撞代码?
ZeM*_*oon 10
您不需要编写太多代码就可以使球从屏幕边缘反弹.物理环境可以处理上述所有内容,您只需要以正确的方式实例化sprite.
首先,您必须设置场景的physicsBody:
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
Run Code Online (Sandbox Code Playgroud)
在场景的.h文件中创建两个位掩码类别:
static const uint8_t ballCategory = 1;
static const uint8_t wallCategory = 2;
Run Code Online (Sandbox Code Playgroud)
并给场景的物理主体一个类别:
self.physicsBody.categoryBitMask = wallCategory;
Run Code Online (Sandbox Code Playgroud)
然后创建你的精灵:
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];
Run Code Online (Sandbox Code Playgroud)
而不是给spriteNode一个[SKAction moveTo: duration:],给它的物理体施加一个冲动.
CGVector impulse = CGVectorMake(1.0,1.0);
[spriteNode.physicsBody applyImpulse:impulse];
Run Code Online (Sandbox Code Playgroud)
瞧!球将从墙上弹起,没有任何阻止它.
这就是.h文件应该是这样的:
#import <SpriteKit/SpriteKit.h>
static const uint8_t ballCategory = 1;
static const uint8_t wallCategory = 2;
@interface BallBounce : SKScene
@end
Run Code Online (Sandbox Code Playgroud)
这就是.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
Run Code Online (Sandbox Code Playgroud)
您是否尝试过设置SKScene的物理主体
[self setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]]; //Physics body of Scene
Run Code Online (Sandbox Code Playgroud)
这应该保持你的节点在框架内.
w ^
| 归档时间: |
|
| 查看次数: |
8469 次 |
| 最近记录: |