Using SpriteKit for an escape game (2)

Source: Internet
Author: User

Add the following code at the end of initWithSize:

// 1 create a physical shape (boundary with screen)

SKPhysicsBody * borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect: self. frame];

// 2 set the physical shape to the scene body

Self. physicsBody = borderBody;

// 3 set the resistance coefficient

Self. physicsBody. friction = 0.0f;

    Create a SKPhysicsBody. SKPhysicsBody is used to simulate the shape of a node. Here, a shape with only four sides is created, which has no volume or volume and is not affected by external force and impulse. You can set a physical shape for each node. We have set the shape of the scenario here. Note: The Coordinate System of SKPhysicsBody is relative to the node position.

    Set the damping factor to 0.0f so that the ball will not slow down when it hits the boundary of the cage. What you need is a full reflection. After the sphere hits the wall, it bounce away from the same angle.

    Full reflection

    Although you have a magic cage, you cannot see the effect. Only after the sphere is added can you see the existence of the cage. Add the following code at the end of initWithSize: method:

    // 1

    SKSpriteNode * ball = [SKSpriteNode spriteNodeWithImageNamed:@"Ball.png"];

    Ball. name = ballCategoryName;

    Ball. position = CGPointMake (self. frame. size. width/3, self. frame. size. height/3 );

    [Self addChild: ball];

    // 2

    Ball. physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: ball. frame. size. width/2];

    // 3

    Ball. physicsBody. friction = 0.0f;

    // 4

    Ball. physicsBody. restitution = 1.0f;

    // 5

    Ball. physicsBody. linearDamping = 0.0f;

    // 6

    Ball. physicsBody. allowsRotation = NO;

      As expected, we created a role to reference it in the name of ball, set its location (relative to the scenario), and then add it to the scene. We create a three-dimensional ball shape. Compared with the line shape created based on the screen boundary, this shape is subject to the external force and impulse, and will collide with other shapes. We create a sphere physical shape in the same size as the "ball" role. Next, set the properties of the physical shape. You have already used the friction coefficient. This line of code should be clear, and it will make the friction coefficient of the ball 0. Restitution attributes refer to the elasticity of objects. If it is set to 1.0, it means that the sphere will rebound percent when it collide with another object. That is to say, the sphere will bounce back with the same force in the collision. LinearDamping indicates the deceleration effect (linear damping) of gases or liquids on objects ). In an escape game, the ball will not be slowed down. Therefore, linear damping is set to 0. The AllowsRotation attribute is literal. Indicates whether to allow rotation of objects. Here, we set it as not allowed.

      Note:Generally, the closer the shape is to the player, the better. It is easy to simulate the behavior of a sphere. But you have to spend a lot of time thinking about more complex shapes. Here, we need to remind you that an object that is too complex may have a high performance cost.

      It's time for the ball to get up (in fact it's a beat ). Add this line after the above Code:

      [Ball. physicsBody applyImpulse: CGVectorMake (10.0f,-10.0f)];

      This line of code imposes a thrust on the Ball (like installing a rocket booster) so that it moves in a certain direction (along the right lower diagonal ). Once the ball starts to exercise, it will rebound back and forth in the cage.

      Let's test it. Compile and run the program, and you will see the ball continuously rebound along the four sides of the screen.

      I keep jumping, so you are upset: D

      Join a racket

      What is next? This is an escape game, but it does not have a racket.

      After the initWithSize: Method ball code, add the following code to construct the racket (and its physical shape ):

      SKSpriteNode * paddle = [[SKSpriteNode alloc] initWithImageNamed:@"Paddle.png"];

      Paddle. name = paddleCategoryName;

      Paddle. position = CGPointMake (CGRectGetMidX (self. frame), paddle. frame. size. height * 0.6f );

      [Self addChild: paddle];

      Paddle. physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: paddle. frame. size];

      Paddle. physicsBody. restitution = 0.1f;

      Paddle. physicsBody. friction = 0.4f;

      // Set the object to a static object

      Paddle. physicsBody. dynamic = NO;

      Most of the Code is the same as the code of the ball. However, a cuboid is used as the shape of a racket, as you can see on the screen.

      Here the racket should be static. In this way, the racket will not be affected by gravity and external forces. Next, you will see why we need to explain this in particular.

      Compile and run the program. The racket will be displayed in the scene and the ball will be opened by the racket.

      The most boring game in history :/

      This is boring because you cannot move the racket at all.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.