(NO.00003) iOS games simple robot projection game forming notes (6)
Why do I need to put the code for arm movement in a single method?
In fact, this is the version after multiple reconstruction. The original mobile code is placed in the touchMoved method. Later, we found that in addition to the mobile method of the touch arm, we also needed to implement the second method of moving the arm on the touch screen.
So we extract it and put it in a method, and we will make a declaration in the Arm class interface later. Now we don't care about it, just look at this method:
-(void)moveArm:(MoveDirection)direction{ CCPhysicsBody *physicsBody = self.physicsBody; CGPoint velocity; switch (direction) { case armMoveDirectionUp: velocity = ccp(0, 10); break; case armMoveDirectionDown: velocity = ccp(0, -10); break; default: break; } [physicsBody applyForce:velocity atLocalPoint:ccp(20, 5)];}
The code is clear: first obtain the physical object of the Arm, and then set the corresponding torque based on the different rotation directions. If you rotate upwards, a positive value is given to the torque Y axis, and vice versa, a negative value is given to the Y axis. the value I set here is +/-10, which is the best value obtained from multiple tests. finally, the torque is applied to the arm.
Note that I didn't use the previous applyForce method here: instead, I used another method similar to this, which is a heavy load method. this method has an additional parameter, which is used to set the point on the arm when the torque is applied. if you do not do this, the torque will be applied to the average point of the arm by default (it is not clear where the cat;), it will be difficult to rotate the arm.
Note that this parameter is ccp (20, 5), and the arm is about 20 long and 10 tall. This is exactly on the midline of the arm and the focus of the hand. this point is also used as a reference point for subsequent bullets, which will be detailed later. in addition, it may be better to calculate this point dynamically based on the length and width of the arm.
Compile and run the App. We found that when the touch arm does not move, the arm will slowly rotate as it moves. here is an effect of acceleration and deceleration. It is difficult for the arm to move from the rest. When it is turned, it becomes faster because of the kinetic energy. this is also consistent with the actual motion of physical objects.
What is a small problem discovered by rotating your arm without stopping? The next article will be announced later ;)