Sprite kit SpriteKit bodyWithTexture:离开场景边界的精灵

Sprite kit SpriteKit bodyWithTexture:离开场景边界的精灵,sprite-kit,Sprite Kit,我最近发现了一种新方法,可以给精灵一个物理体,而不仅仅是使用一个圆,但是当我使用这种新方法时,精灵会撞到屏幕的边缘,然后穿过屏幕,然后离开 我有场景边框设置: self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]; 这就是我正在尝试的方法: zombie.physicsBody = [SKPhysicsBody bodyWithTexture:zombie.texture size:zombie.size]

我最近发现了一种新方法,可以给精灵一个物理体,而不仅仅是使用一个圆,但是当我使用这种新方法时,精灵会撞到屏幕的边缘,然后穿过屏幕,然后离开

我有场景边框设置:

self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
这就是我正在尝试的方法:

zombie.physicsBody = [SKPhysicsBody bodyWithTexture:zombie.texture size:zombie.size];
我的精灵是如何从场景中挤出来的

位掩码:

self.physicsWorld.contactDelegate = self;

zombie.physicsBody.categoryBitMask = playerCategory;
zombie.physicsBody.contactTestBitMask = gearCategory;
zombie.physicsBody.collisionBitMask = 1;

我真的不知道你为什么会有这个问题。我也不知道你为什么需要使用更新方法。如果设置速度,则精灵将保持该速度,直到有东西改变它。。我将共享我的viewcontroller和场景。我没有和你一样的问题。这和你现在做的有什么区别

您是否尝试过在没有
bodyWithTexture
的情况下执行此操作?我知道这是spritekit最近增加的一款。也许你的纹理的形状很奇怪,导致了这个问题。试着用
bodyWithRectangleOfSize

视图控制器

@implementation GameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    /* Sprite Kit applies additional optimizations to improve rendering performance */
    skView.ignoresSiblingOrder = YES;

    // Create and configure the scene.
    GameScene *scene = [[GameScene alloc]initWithSize:skView.frame.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}


@end
游戏场景

@implementation GameScene

SKSpriteNode *_test;

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        self.physicsWorld.gravity = CGVectorMake(0, 0);
        _test = [[SKSpriteNode alloc]initWithColor:[SKColor redColor] size:CGSizeMake(10, 10)];
        _test.position = CGPointMake(self.size.width/2, self.size.height/2);
        _test.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_test.size];

        [self addChild:_test];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint touchPos = [touch locationInNode:self];

        CGPoint newPosition = CGPointMake(touchPos.x - _test.position.x, touchPos.y - _test.position.y);
        CGFloat angle = atan2(newPosition.y, newPosition.x);
        CGPoint normalized = CGPointMake(cos(angle), sin(angle));

        _test.physicsBody.velocity = CGVectorMake(normalized.x * 200, normalized.y * 200);

    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

你能再多说一些细节吗?你是在对你的僵尸采取行动吗?它是如何“推动”通过的?请在您为场景和僵尸物理体设置接触、碰撞和类别位掩码值的地方发布代码。无需操作。触摸屏幕会更改更新:(NSInterval)currentTime方法中使用的速度变量,该方法会更改精灵的位置。如果触摸屏幕,精灵将继续以该速度运行,直到您点击其他位置。当我说推动时,我的意思是当它撞到墙上时,它会撞击它,稍微振动,然后穿过它。