我必须在这里遗漏一些非常明显的东西。我的目标:在屏幕中央创建一个盒子,弹跳球留在盒子里面。我正在尝试为边界框创建基于边缘的SKSpriteNode,并为内部球创建基于体积的SKShapeNode。球在盒子的中心产生,从盒子里掉下来,从屏幕上掉下来,显然会碰到屏幕底部以下的东西,然后在底部边缘上方反弹几次,最后消失。一直以来,didBeginContact永远不会发射。
问题1:为什么我的球落在可见的盒子里,但接触到屏幕边缘以下的东西?我的物理实体是否与实际精灵错位? 问题2:为什么didBeginContact没有解雇? [编辑,好吧,这很简单。忘了设置physicsworld.delegate]谢谢!
Scene.h
#define MAX_WORLD_CELLS_X 10
#define MAX_WORLD_CELLS_Y 10
#define BGTILE_SIZE 100
typedef enum : uint32_t {
TYPE_SUBSPACE = 1,
TYPE_WORLD = 2,
TYPE_CHILD = 4
} ColliderTypes;
@interface helloScene : SKScene
<SKPhysicsContactDelegate>
{
SKSpriteNode *layerSubSpace,*layerWorld;
}
@end
Scene.m,didBeginContact
-(void) didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"Function: GameScene.m:didBeginContact");
}
Scene.m,initWithSize
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
CGSize layerSize = CGSizeMake(MAX_WORLD_CELLS_X * BGTILE_SIZE *2, MAX_WORLD_CELLS_Y * BGTILE_SIZE *2);
layerSubSpace = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:layerSize];
//center subSpace on the screen's center
layerSubSpace.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
CGRect subspaceRect = CGRectMake(-layerSubSpace.size.width/2, -layerSubSpace.size.height/2, layerSubSpace.size.width, layerSubSpace.size.height);
layerSubSpace.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:subspaceRect];
layerSubSpace.physicsBody.affectedByGravity = NO;
layerSubSpace.physicsBody.categoryBitMask = TYPE_SUBSPACE;
layerSubSpace.physicsBody.contactTestBitMask = TYPE_WORLD;
layerSubSpace.physicsBody.collisionBitMask = TYPE_WORLD;
[self addChild:layerSubSpace];
SKShapeNode *ball = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 60, 0, M_PI*2, YES);
ball.path = myPath;
ball.lineWidth = 1.0;
ball.fillColor = [SKColor blueColor];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:60];
ball.physicsBody.restitution = 1;
ball.physicsBody.categoryBitMask = TYPE_WORLD;
ball.physicsBody.collisionBitMask = TYPE_SUBSPACE;
ball.physicsBody.contactTestBitMask = TYPE_SUBSPACE;
[layerSubSpace addChild:ball];
}
layerSubSpace.xScale = 0.25;
layerSubSpace.yScale = 0.25;
return self;
}
答案 0 :(得分:1)
1)您的物理模拟没有与您的视觉表示对齐的原因是您已将x/yScale
的{{1}}属性设置为0.25。这些属性会影响精灵的视觉表示,但不影响基础物理模型。基本问题是你的球和盒子的尺寸在每个尺寸上减少了四倍,但是球的下落距离并没有减少。
我怀疑解决方案只是简单地将所有内容的大小减少四倍,而不是依赖于layerSubSpace
属性。当我将x/yScale
减少到25并将圆圈的半径(BGTILE_SIZE
和CGPathAddArc
)减少到15时,球会停留在iPad模拟器上框的底部。
2)如你所说,你没有设置bodyWithCircleOfRadius: