2
votes

Okay, so I'm writing code for a space blaster game, and all seems to be going well, minus these crucial issues.

First, I added a spaceship (with PhysicsBody) as a child of self (GameScene).

Then I added a background layer as a node also as a child of self, behind the spaceship, and simulated movement by moving the background around behind the spaceship via joystick control.

I added a node with a boundary physics Edge Loop body so the background wouldn't move beyond the ship, and added that node as a child of the bgLayer.

I have objects for the spaceship to shoot at, but I want them to move around as the background does, so I added them as children of the bgLayer.

Of course, I needed a laser when the spaceship fires, so I added this as well as a child of the bgLayer.


_spaceship.physicsBody = (custom physicsBody code);
[self addChild:_spaceship];

_bgLayer = [SKNode node];
[self addChild:_bgLayer];

_bounds = [SKNode node]; 
_bounds.physicsBody = (physicsBody edgeLoop code);
[_bgLayer addChild:_bounds];

_otherObjects.physicsBody = (custom physicsBody code);
[_bgLayer addChild:_otherObjects];

_laser.physicsBody = (custom physicsBody code);
[_bgLayer addChild:_laser];

All is well, the background doesn't move beyond the spaceship,the other objects move as the background moves, and the laser fires when called upon.

My first big dilemma is when my laser fires, it does not move as the background moves, but the background moves behind it. I could probably make do but it looks funny if the laser always moves in parallel with the ship. This seems odd to me since I added the laser as a child of _bgLayer.

Second, my laser's physicsBody doesn't seem to recognize the EdgeLoop body, and sails right on through it. Also, my spaceship's physicsBody seems to recognize the EdgeLoop body, but it does not recognize the other objects that are children of _bgLayer.

Do Physics Bodies that are not children of the same layer recognize each other? And why doesn't my laser behave similarly to other children of the same layer?

2
There are no layers in the physics engine. There is one physics world per scene. You just need to set the collision categories and that sort of thing and make sure to set up all the things that define if and how an object participates in physics.uchuugaka
I apologize for not adding this in my coding example, but I did set category and collision bitMasks for them, yet the laser seems to ignore these settings.wtivie05

2 Answers

0
votes
-(void)fireLaser
{
    _laser = [SKSpriteNode spriteNodeWithImageNamed: [NSString stringWithFormat:@"blueLaser"]];;
    _laser.zRotation = _spaceShip.zRotation;
    CGVector rotationVector = RadiansToVector(_spaceShip.zRotation);
    _laser.position = (Custom code for positioning the laser just in front of the spaceship);

    _laser.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_laser.size];
    [_bgLayer addChild:_laser];

    _laser.physicsBody.velocity = CGVectorMake(rotationVector.dx * LASER_SPEED, rotationVector.dy * LASER_SPEED);  
}

[self fireLaser] is called in touchesBegan when a particular SpriteNode is touched. The laser fires beautifully, but does not scroll with the background, but rather moves in relation to the spaceship. Background scrolls with a PhysicsBody and a setVelocity method is called when the joystick moves, thus simulating spaceship motion, when in reality the spaceship never leaves the center of the screen. Physics categories prevent the background physics body from colliding with anything. LASER_SPEED is simply a static const float.

0
votes

Moving the world by changing its position will affect

  1. Children with physics bodies
  2. Children without physics bodies

Moving the world by applying a force/impulse or by settings its velocity will affect

  1. Children without physics bodies

Instead of moving the world by setting its velocity, you can add a camera (an SKNode with a physics body) to the world, move the camera by setting its velocity, and then update the position of the world to center the camera. You can center the camera in the didFinishUpdate method.