我正在制作一个在iPad屏幕上弹跳球的游戏。类似于乒乓游戏。我看到SKScene的SKPhysicsWorld具有重力特性,并且控制着物体如何相互碰撞。
,有什么方法可以自动检测精灵的边缘是否与屏幕的边缘发生碰撞,所以它可以弹出?,还是我需要编写自己的碰撞代码?
发布于 2013-12-05 10:57:05
你不需要写太多的代码来使球从屏幕边缘弹出。物理环境可以处理以上所有的事情,您只需要以正确的方式实例化这些精灵。
首先,您必须将场景的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发布于 2013-11-10 22:37:25
你试过设置SKScene的物理体吗?
[self setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]]; //Physics body of Scene这应该使您的节点处于帧内。
W
发布于 2014-03-07 05:47:48
这是来自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)];https://stackoverflow.com/questions/19883865
复制相似问题