如何给屏幕边缘一个物理体 [英] How to give screen edge a physics body

查看:139
本文介绍了如何给屏幕边缘一个物理体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个背景精灵,我想给物理身体。

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

推荐答案

您需要基于边的主体,而不是基于卷的主体。使用 bodyWithEdgeLoopF​​romRect: 并查看编程指南中的物理章节

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.

这篇关于如何给屏幕边缘一个物理体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆