Game Development Reference
In-Depth Information
This looks sufficient for our character sprite. What can we do with backgrounds?
Obviously, we need the player to collide with the top and bottom of the screen.
This is when other methods come in handy. Add this code right before the return
statement in the generateNewBackground method in ERGBackground.m :
background.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPo
intMake(0, 30) toPoint:CGPointMake(background.size.width, 30)];
background.physicsBody.collisionBitMask = playerCollisionBitmask;
We set the bitmask to be the same as we want the player to collide with the
background. But here comes the problem. Since all physics bodies need to be
convex, we can't create the physics body that accommodates both the top and
bottom collision surfaces. We might think of adding a second collision body
to the same node, but it is not supported.
There is a small hack around this—we will just create a new node, attach it as a child
to the background node, and attach the top collision body to it. Add the following
code after the previous code and right before the return statement:
SKNode *topCollider = [SKNode node];
topCollider.position = CGPointMake(0, 0);
topCollider.physicsBody = [SKPhysicsBody bodyWithEd
geFromPoint:CGPointMake(0, background.size.height - 30)
toPoint:CGPointMake(background.size.width, background.size.height -
30)];
topCollider.physicsBody.collisionBitMask = 1;
[background addChild:topCollider];
Here, we create a new physics body with EdgeFromPoint , from the left edge to the
right edge. This way, we effectively have two colliding bodies on one background.
As we now want to have physics-based controls, you may remove all the code that
handles movement. These tapping and actions methods are as follows:
touchesBegan:
touchesMoved:
touchesEnded:
swipeLeft
swipeRight
 
Search WWH ::




Custom Search