bodyWithEdgeLoopFromRect not working in landscape

半城伤御伤魂 提交于 2020-01-24 06:03:32

问题


I'm just trying to set up a basic scene in landscape, with gravity, and having the scene in an edge loop.

I set up the scene's physics body and the mainCharacter sprite physics body, here is my code:

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.backgroundColor = [SKColor redColor];
        [self setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromRect:[self frame]]];
    }
    return self;
}


-(void)setupMain
{
    if (!self.mainCharacter)
    {
        self.mainCharacter = [[SKSpriteNode alloc] initWithImageNamed:@"spriteDefault"];

       [self.mainCharacter setPosition:CGPointMake(CGRectGetMidX([self frame]), CGRectGetMidY([self frame]))];

       [self addChild:self.mainCharacter];

       self.mainCharacter.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.mainCharacter.frame.size];
       self.mainCharacter.physicsBody.dynamic = YES;
       self.mainCharacter.physicsBody.affectedByGravity = YES;
       self.mainCharacter.physicsBody.mass = 0.02;
    }
 }

So, in portrait mode, everything works perfectly, however, in landscape, things get really screwy.

I figured it has something to do with

  [self setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromRect:[self frame]]];

Oddly enough, the edge loop for the x axis for landscape (the y axis in portrait mode) works fine, but I just fall through the y axis (x for portrait).

My guess is that the frame is returning the position on the y axis somewhere not within the bounds of the screen in landscape mode.... meaning its somewhere above or below the screen. ...Maybe... Not really sure.

However, I have tried several different options, including manually setting the rectangle myself by using CGRectMake() I wasn't able to get anything to work properly.

Any advice would be greatly appreciated!!!


回答1:


Okay, this is a super Janky fix...

 self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0, 200, 320, 200)];

I'm not sure why this works... and it's obviously going to have some problems on smaller screen sizes... better fixes and explanations would be much appreciated!! Thanks :D




回答2:


You will need to setup the edge loop in the viewWillLayoutSubviews, since the scene size is only known at then:

-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];

// Configure the view.
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    [skView presentScene:scene];
}

}




回答3:


You need to set the collisionBitMask of your mainCharacter equals to the sceneCategory (need to create). Your nodes will only be affected if you tell them.



来源:https://stackoverflow.com/questions/19580706/bodywithedgeloopfromrect-not-working-in-landscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!