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.
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:
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.