gpt4 book ai didi

ios - 让碰撞在 Apple SpriteKit 中起作用

转载 作者:行者123 更新时间:2023-11-28 19:53:49 24 4
gpt4 key购买 nike

我试图在 Xcode 中编写一个简单的应用程序 - 坠落游戏 - 我需要让球与平台发生碰撞。

首先 - 我尝试使用动画师并添加碰撞器和重力等,但后来我无法将框架设为圆形(如果有人可以向我展示这将非常有帮助)。

其次 - 因为上面的方法不起作用(与 UIbezierpath 有关)我决定使用 SpriteKit。使用 spriteKit 你可以创建对象/节点。球和平台是 skshapenodes,我需要使用 ball.physicsBody.collisionBitMask 进行碰撞,但每次我跑球都会穿过 body

static const uint32_t ballCategory = 0x11 << 1;
static const uint32_t platformCategory = 0x11 << 3;

+ (SKNode *) spawnBall
{
SKShapeNode *node = [[SKShapeNode alloc] init];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, BALL_RADIUS, BALL_RADIUS)];

node.path = [path CGPath];
node.fillColor = [SKColor colorWithRed:0.7 green:0.0 blue:0.0 alpha:1.0];
node.strokeColor = [SKColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
//node.glowWidth = BALL_GLOW_RADIUS;

node.physicsBody.categoryBitMask = ballCategory;
node.physicsBody.collisionBitMask = platformCategory;
return node;
}
+ (NSMutableArray *) risingPlats
{
// Screen Size
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat width = screenBound.size.width;
CGFloat height = screenBound.size.height;

NSMutableArray *arrayOfShapes = [[NSMutableArray alloc] init];
int numberPieces = arc4random() % 2;
if (numberPieces) {
SKShapeNode * node = [[SKShapeNode alloc] init];

UIBezierPath *path = [UIBezierPath bezierPathWithRect:
CGRectMake((BALL_RADIUS*3-width)/2, -height, width-(BALL_RADIUS*3), BALL_RADIUS)];

node.path = [path CGPath];
node.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0];
node.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];

node.physicsBody.categoryBitMask = platformCategory;
node.physicsBody.collisionBitMask = ballCategory;

[arrayOfShapes addObject:node];
} else {
int locationOfHole = (arc4random() % (int)(width-(BALL_RADIUS*6)))+BALL_RADIUS*1.5;

// node1
SKShapeNode * node = [[SKShapeNode alloc] init];
UIBezierPath *path = [UIBezierPath bezierPathWithRect:
CGRectMake((-width/2), -height, locationOfHole, BALL_RADIUS)];

node.path = [path CGPath];
node.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0];
node.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];

node.physicsBody.categoryBitMask = platformCategory;
node.physicsBody.collisionBitMask = ballCategory;

[arrayOfShapes addObject:node];

// node2
SKShapeNode *node2 = [[SKShapeNode alloc] init];
UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:
CGRectMake(locationOfHole+BALL_RADIUS*3-(width/2), -height, width-(locationOfHole+BALL_RADIUS*3), BALL_RADIUS)];

node2.path = [path2 CGPath];
node2.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0];
node2.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];

node2.physicsBody.categoryBitMask = platformCategory;
node2.physicsBody.collisionBitMask = ballCategory;

[arrayOfShapes addObject:node2];
}
return arrayOfShapes;
}

我将上面的内容更改为 contactbitmask 并且当我实现两个 body 接触的方法时它从未打印到控制台。

最佳答案

您的节点没有分配给它们的物理体。

您正在为它们分配 physicsbody 类别,但它们一开始就没有 physicsbody。是零!

你需要做的

yournode.physicsBody = [SKPhysicsBody <some initializer>]

关于ios - 让碰撞在 Apple SpriteKit 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27700309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com