gpt4 book ai didi

ios - didBeginContact :(SKPhysicsContact *)contact not invoked

转载 作者:技术小花猫 更新时间:2023-10-29 11:17:58 25 4
gpt4 key购买 nike

我创建了 SKScene 继承类。问题是关于物理体方法的接触

- (void)didBeginContact:(SKPhysicsContact *)contact 

未被调用解决方案可能很简单,但作为 sprite kit 的初学者,我坚持这个。

下面是代码

#import "MyScene.h"
@interface MyScene ()
@property BOOL contentCreated;
@end
@implementation MyScene
- (id)initWithSize:(CGSize)size {
self = [super initWithSize:size];
if (self) {
self.physicsWorld.contactDelegate = self;
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self buildWorld];
self.physicsWorld.contactDelegate = self;
}
}

#pragma mark - World Building
- (void)buildWorld {
NSLog(@"Building the world");
SKSpriteNode * sprite1 = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(100,100)];
sprite1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100,100)];
sprite1.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) +100);

SKSpriteNode * sprite2 = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(100,100)];
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100,100)];
sprite2.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 100);


[self addChild:sprite1];
[self addChild:sprite2];
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"contact");
}

@end

提前致谢。

最佳答案

来自<a href="https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsWorld_Ref/Introduction/Introduction.html#//apple_ref/occ/instp/SKPhysicsWorld/contactDelegate" rel="noreferrer noopener nofollow">SKPhysicsWorld</a>文档:

A contact is created when two physics bodies overlap and one of the physics bodies has a contactTestBitMask property that overlaps with the other body’s categoryBitMask property.

你必须给物理体分配一个 categoryBitMask和一个 contactTestBitMask .你想先创建你的类别:

static const uint32_t sprite1Category = 0x1 << 0;
static const uint32_t sprite2Category = 0x1 << 1;

接下来,分配类别和接触测试位掩码:

sprite1.physicsBody.categoryBitMask = sprite1Category;
sprite1.physicsBody.contactTestBitMask = sprite2Category;

sprite2.physicsBody.categoryBitMask = sprite2Category;
sprite2.physicsBody.contactTestBitMask = sprite1Category;

来自 <a href="https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/Reference/Reference.html#//apple_ref/occ/instp/SKPhysicsBody/collisionBitMask" rel="noreferrer noopener nofollow">SKPhysicsBody</a> 的注释文档:

For best performance, only set bits in the contacts mask for interactions you are interested in.

关于ios - didBeginContact :(SKPhysicsContact *)contact not invoked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19675967/

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