IOS Sprite 套件didBeginContact未调用
<p><p>请帮助我,我已经尝试解决这个问题 24 小时,我快要疯了:) 我试图通过 <code>didBeginContact</code> 委托(delegate)方法检测 Sprite Kit 中的碰撞,但是这个方法没有触发,网上的答案我都试过了</p>
<pre><code>#import "MyScene.h"
#import "GameOverScene.h"
static const uint32_t basketCategory =1 << 0;
static const uint32_t ballCategory =1 << 1;
// 1
@interface MyScene () <SKPhysicsContactDelegate>
//@interface MyScene ()
@property BOOL contentCreated;
@property (nonatomic) SKSpriteNode * basket;
@property (nonatomic) SKSpriteNode * ball;
@property (nonatomic) NSTimeInterval lastSpawnTimeInterval;
@property (nonatomic) NSTimeInterval lastUpdateTimeInterval;
@property (nonatomic) int ballDestroyed;
@property (nonatomic) int score;
@end
static inline CGPoint rwAdd(CGPoint a, CGPoint b) {
return CGPointMake(a.x + b.x, a.y + b.y);
}
static inline CGPoint rwSub(CGPoint a, CGPoint b) {
return CGPointMake(a.x - b.x, a.y - b.y);
}
static inline CGPoint rwMult(CGPoint a, float b) {
return CGPointMake(a.x * b, a.y * b);
}
static inline float rwLength(CGPoint a) {
return sqrtf(a.x * a.x + a.y * a.y);
}
// Makes a vector have a length of 1
static inline CGPoint rwNormalize(CGPoint a) {
float length = rwLength(a);
return CGPointMake(a.x / length, a.y / length);
}
@implementation MyScene
float balltime;
-(id)initWithSize:(CGSize)size {
if (self = ) {
/* Setup your scene here */
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
self.backgroundColor = ;
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
self.basket = ;
self.basket.size = CGSizeMake(100, 100);
self.basket.position = CGPointMake(self.frame.size.width/2, self.basket.size.height/2);
self.basket.physicsBody.dynamic = YES; // 2
self.basket.physicsBody.categoryBitMask = basketCategory; // 3
self.basket.physicsBody.contactTestBitMask = ballCategory; // 4
self.basket.physicsBody.collisionBitMask = 0; // 5
self.basket.physicsBody.usesPreciseCollisionDetection = YES;
;
// self.physicsWorld.contactDelegate = self;
}
}
- (void)addball {
balltime=1;
// Create sprite
self.ball = ;
self.ball.size = CGSizeMake(100, 100);
self.ball.physicsBody = ; // 1
self.ball.physicsBody.dynamic = YES; // 2
self.ball.physicsBody.categoryBitMask = ballCategory; // 3
self.ball.physicsBody.contactTestBitMask = basketCategory; // 4
self.ball.physicsBody.collisionBitMask = 0; // 5
self.ball.physicsBody.usesPreciseCollisionDetection = YES;
//self.physicsWorld.contactDelegate = self;
// Determine where to spawn the ball along the x axis
int minX = self.ball.size.width / 2;
int maxX = self.frame.size.width - self.ball.size.width / 2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
// Create the monster slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
// ball.position = CGPointMake(actualX,self.frame.size.height + ball.size.height/2);
self.ball.position = CGPointMake(actualX,self.frame.size.height + 300);
;
// Determine speed of the ball
int minDuration = 3.99;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
SKAction * actionMove = ;
SKAction * actionMoveDone = ;
SKAction * loseAction = [SKAction runBlock:^{
SKTransition *reveal = ;
SKScene * gameOverScene = [ initWithSize:self.size won:NO];
;
}];
]];
}
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval > balltime) {
self.lastSpawnTimeInterval = 0;
;
}
}
- (void)update:(NSTimeInterval)currentTime {
// Handle time delta.
// If we drop below 60fps, we still want everything to move the same distance.
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}
;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = ;
CGPoint positionInScene = ;
self.basket.position=positionInScene;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = ;
CGPoint positionInScene = ;
self.basket.position=positionInScene;
}
- (void)ball:(SKSpriteNode *)ball didCollideWithbasket:(SKSpriteNode *)basket {
NSLog(@"Hit");
;
self.ballDestroyed++;
if (self.ballDestroyed > 20) {
SKTransition *reveal = ;
SKScene * gameOverScene = [ initWithSize:self.size won:YES];
;
}
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
NSLog(@"contact");
// 1
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
// 2
if ((firstBody.categoryBitMask & basketCategory) != 0 &&
(secondBody.categoryBitMask & ballCategory) != 0)
{
;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>尝试将 <code><SKPhysicsContactDelegate></code> 移动到您的 MyScene.h 文件中,并将您的类别位掩码更改为如下所示。 <code>static const uint32_t basketCategory = 0x1 << 0;</code> 并确保设置了碰撞位掩码。我发现如果没有设置碰撞位掩码,是不会触发联系方式的。</p></p>
<p style="font-size: 20px;">关于IOS Sprite 套件didBeginContact未调用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21954037/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21954037/
</a>
</p>
页:
[1]