所以我用SpriteKit创建了一个小游戏。每当人类击中BlockCategory时,他应该能够跳一次。使用我当前的代码,Human在开始后跳转1次,然后在Contact和Contact之间每0.1秒切换一次。所以他再次击中地面后再也无法跳起来了。我的代码:
-(void)Mensch{
Human = [SKSpriteNode spriteNodeWithTexture:HumanTexture1];
[Human setScale:1.0];
Human.position = CGPointMake(50 , 225);
[Human runAction:Run];
SKSpriteNode * HumanBody = [SKSpriteNode spriteNodeWithTexture:HumanTexture1];
HumanBody.size = CGSizeMake(30, 50);
Human.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:MenschKörper.size];
Human.physicsBody.dynamic = NO;
Human.physicsBody.allowsRotation = NO;
Human.physicsBody.usesPreciseCollisionDetection = YES;
Human.physicsBody.categoryBitMask = HumanCategory;
Human.physicsBody.collisionBitMask = BlockCategory | StoneCategory;
Human.physicsBody.contactTestBitMask = StoneCategory | BlockCategory;
[self addChild:Human];
-(void)spawnBlocks {
Block1 = [SKSpriteNode spriteNodeWithTexture:BlockTexture1];
[Block1 setScale:1];
Block1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Block1.size];
Block1.physicsBody.dynamic = NO;
Block1.physicsBody.usesPreciseCollisionDetection = YES;
Block1.name = @"Blocks";
[self addChild:Block1];
Block1.physicsBody.categoryBitMask = BlockCategory;
Block1.physicsBody.collisionBitMask = HumanCategory;
Block1.physicsBody.contactTestBitMask = HumanCategory;
// and for the following 3 Blocks the same code
- (void)didBeginContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (HumanCategory | StoneCategory))
{
[self GameOver];
[self runAction:[SKAction playSoundFileNamed:@"Collision.mp3" waitForCompletion:NO]];
}
if (collision == (HumanCategory | BlockCategory))
{
PlayerBlockContact = YES;
NSLog(@"Contact");
}
}
-(void)didEndContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (HumanCategory | BlockCategory))
{
PlayerBlockContact = NO;
NSLog(@"Contact Lost");
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if(PlayerBlockContact){
Human.physicsBody.velocity = CGVectorMake(0, 0);
[Human.physicsBody applyImpulse:CGVectorMake(0, 35)];
[self runAction:[SKAction playSoundFileNamed:@"Jump.mp3" waitForCompletion:NO]];
}
答案 0 :(得分:0)
快速连续丢失的联系人和联系人听起来像是对象的归还财产的问题。这有时会导致无休止的一系列反弹。
如果对象在很快的连续中成为和失去联系,有时SpriteKit会忽略正确的条件(有联系的对象显示为没有联系)。
尝试将两个对象的恢复原状设置为零。
(yourObjectName).physicsBody.restitution = 0;