本文介绍了检测sprite工具包中的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试用精灵套件制作一个简单的游戏。 基本的想法是,有一个玩家可以跳跃以避免阻挡。 但我有一个问题我不知道怎么说当玩家击中阻挡时玩家消失并且血液动画开始。 首先,我不明白我在苹果网站上找到的代码是什么。I'm trying to make a simple game with sprite kit.The basic idea is that there is one player who can jump to avoid blocks.But i have a problem I don't know how to make it that when the player hits the block the player disappears and the blood animation starts.First of all i don't understand what this code does that I found on apples website.static const uint32_t blockCategory = 0x1 <<0;static const uint32_t playerCategory = 0x1 <<1;比我调用didBeginContact函数并在其中放入NSLOG(确实调用函数)。 但我从未在调试器中收到输出。Than i am calling the didBeginContact function and put a NSLOG("did call function") in it.But i never receive the output in my debugger.这是我的_player和_block代码: - (SKSpriteNode *)character {Here is my _player and _block code: -(SKSpriteNode *)character {_player = [SKSpriteNode spriteNodeWithImageNamed:@"soldier_run1"];_player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:_player.size.width /2 -5];_player.physicsBody.dynamic = YES;_player.physicsBody.usesPreciseCollisionDetection = YES;_player.physicsBody.friction = 0;_player.physicsBody.categoryBitMask = playerCategory;_player.physicsBody.collisionBitMask = blokCategory;_player.name = @"player";SKAction *animAction = [SKAction animateWithTextures:playerTextures timePerFrame:0.1 resize:YES restore:YES];我的_player代码:My _player code:[_player runAction:[SKAction repeatActionForever:animAction]];return _player;}-(SKSpriteNode *)block {_blok = [[SKSpriteNode alloc] initWithColor:[SKColor blackColor] size:CGSizeMake(15, 40)];//physics_blok.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_blok.size];_blok.physicsBody.dynamic = NO;_blok.name = @"block";_blok.physicsBody.categoryBitMask = blokCategory;_blok.physicsBody.collisionBitMask = playerCategory;SKAction *moveBlock = [SKAction sequence:@[ [SKAction moveToX:-20 duration:2] ]];[_blok runAction:moveBlock ];return _blok;}另外,我真的不明白categoryBitMask和collisionBitMask的作用。 在我完成这项工作之后,我想让角色从屏幕和血液动画中消失,但我不知道如何让这种情况发生。我认为你必须做类似的事情: if(_player&& _block didcollide){} 但我不知道该怎么做。Also i don't really understand what the categoryBitMask and collisionBitMask do.After i have that working i would like to make the character disappear from the screen and the blood animation to start, but I have no idea how to let that happen. I think you have to do something like: if(_player && _block didcollide) { }But I don't know how to do it exactly.推荐答案 categoryBitMask设置sprite所属的类别,而collisionBitMask设置sprite可以与之碰撞的类别,而不是通过它们。The categoryBitMask sets the category that the sprite belongs to, whereas the collisionBitMask sets the category with which the sprite can collide with and not pass-through them.对于碰撞检测,您需要设置contactTestBitMask。在这里,您可以设置联系人代表在联系时调用的精灵类别。For collision detection, you need to set the contactTestBitMask. Here, you set the categories of sprites with which you want the contact delegates to be called upon contact.您已经完成的工作是正确的。以下是您需要做的一些补充:What you have already done is correct. Here are a few additions you need to do:_player.physicsBody.contactTestBitMask = blockCategory;_blok.physicsBody.contactTestBitMask = playerCategory;之后,按如下方式实施联系代表:Afterwards, implement the contact delegate as follows:-(void)didBeginContact:(SKPhysicsContact *)contact`{NSLog(@"contact detected");SKPhysicsBody *firstBody;SKPhysicsBody *secondBody;if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask){ firstBody = contact.bodyA; secondBody = contact.bodyB;}else{ firstBody = contact.bodyB; secondBody = contact.bodyA;}//Your first body is the block, secondbody is the player.//Implement relevant code here.}有关实施碰撞检测的详细说明,请查看这个教程。For a good explanation on implementing collision detection, look at this tutorial. 这篇关于检测sprite工具包中的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 02:47