How to give screen edge a physics body

…衆ロ難τιáo~ 提交于 2019-12-11 03:44:08

问题


I have a background sprite that I would like to give physics body.

background.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(screenWidth , screenHeight)];

The problem is that the sprites are within the screen bounds and Im trying to detected collision with those sprites when they hit the edge of the screen. Instead its automatically detecting collision because my sprites are already within the physics body of the background. What do I do?

Update:Above question is answered for me now I have another issue. Its detecting contact right way and it has to do with my size and position of background can someone please help me.

//init several sizes used in all scene
    screenRect = [[UIScreen mainScreen] bounds];
    screenHeight = screenRect.size.height;
    screenWidth = screenRect.size.width;

    //set background
    self.background = [SKSpriteNode spriteNodeWithImageNamed:@"GameBG3.5inchN.png"];
    self.background.size = CGSizeMake(screenHeight, screenWidth);
    self.background.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    //add pphysics body
    self.background.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:screenRect]; // 1
    self.background.physicsBody.dynamic = YES; // 2
    self.background.physicsBody.categoryBitMask = screenEdge; // 3
    self.background.physicsBody.contactTestBitMask = cd | floppy; // 4
    self.background.physicsBody.collisionBitMask = 0; // 5

    [self addChild:self.background];

Answer: The problem was where it init my sizes the game is landscape but values were portrait


回答1:


You want an edge-based body, not a volume-based body. Use bodyWithEdgeLoopFromRect: and take a look at the physics chapter in the programming guide.



来源:https://stackoverflow.com/questions/21948028/how-to-give-screen-edge-a-physics-body

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