Firing objects from the catapult

You will now learn how to launch an object from the catapult. This will be accomplished by combining techniques into one smooth action, which you have already learned. Following are the steps:

  1. The first step to firing an object from the catapult is to actually make an object.
  2. Open SpriteBuilder and add a new node named Brick. Set bricks size to be (25, 25) and its anchor point to be (0.5, 0.5). Enable physics and set its density to a high value such as 50.0, and its friction to be 1.0, and its elasticity to 0.
  3. Add a color node to the node and give it an orange color to make it look more like a brick.
  4. Publish and switch back to Xcode.
  5. In order to fire an object, you will use the same techniques that you have already used. When the user touches down on the catapult arm, you will add a new object to it. You will then connect it to the arm with a pivot joint. Once the catapult is released, you will invalidate this joint and let the brick fly.
  6. First modify the interface to add two new properties:
    @property (nonatomic, strong) CCNode *brick;
    @property (nonatomic, strong) CCPhysicsJoint *brickJoint;

    These will be used to keep track of your brick when it is being used.

  7. Next, modify the touchBegan method in the following code:
    - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint touchLocation = [touch locationInNode:self];
        
        //Is the touch location in the catapult arm?
        if (CGRectContainsPoint(self.catapultArm.boundingBox, touchLocation)) {
            //Move the touch node to the position of the touch.
            self.touchNode.position = touchLocation;
            
            //Attach a spring between the touch node and the catapult arm
            self.touchJoint = [CCPhysicsJoint connectedSpringJointWithBodyA:self.touchNode.physicsBody bodyB:self.catapultArm.physicsBody anchorA:ccp(0, 0) anchorB:ccp(15, 15) restLength:0.0f stiffness:3000.0f damping:150.0f];
            
            //Setup the brick
            CCNode *brick = [CCBReader load:@"Brick"];
            CGPoint brickPosition = [self.catapultArm convertToWorldSpace:ccp(35, 35)];
            brick.position = brickPosition;
            [self.physicsNode addChild:brick];
            self.brick.physicsBody.collisionMask = @[];
            self.brick.physicsBody.allowsRotation = NO;
            self.brick = brick;
            
            self.brickJoint = [CCPhysicsJoint connectedPivotJointWithBodyA:brick.physicsBody bodyB:self.catapultArm.physicsBody anchorA:ccp(15, 15)];
        }
    }

    This adds a new brick to the scene and sets up its position. You will notice that rotation and collision have been disabled. This is to avoid weird behavior when the brick is in the catapult. Once it is released, these will be enabled again.

  8. Now that you have added the brick to the catapult arm, you just need to release it.
  9. Modify your fire catapult method to the following code:
    - (void)fireCatapult {
        if (self.touchJoint) {
            [self.touchJoint invalidate];
            self.touchJoint = nil;
            
            [self.brickJoint invalidate];
            self.brickJoint = nil;
            
            self.brick.physicsBody.collisionMask = nil;
            self.brick.physicsBody.allowsRotation = YES;
        }
    }

    This should look very familiar by now. You invalidate the joint, and also the renewable collisions and rotation for the brick.

  10. Build and run your app. You should now be able to throw bricks from your catapult!
    Firing objects from the catapult
  11. Now all you need to do is build something to destroy.
  12. Open SpriteBuilder and drag out some color nodes to create a structure of your choice.
  13. Make sure they are all children of the physics node and then enable physics on all of them. Give them a solid weight so they don't go flying too far (unless that's what you want!).
    Firing objects from the catapult
  14. Build and run your game when your structure is complete. Enjoy knocking it down.
    Firing objects from the catapult
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.71.72