Why would you want to put the code that moves the arm in a single method?
In fact, this is the version after several refactoring. The original mobile code is placed in the Touchmoved method. It was later found that the second way to touch the screen was to move the arm, in addition to touching the arm movement.
Therefore, it is extracted and placed in a method, and later in the interface of the ARM class declaration. Now let's just look at this method:
-(void) Movearm: (movedirection) direction{ccphysicsbody *physicsbody = Self. Physicsbody;CgpointVelocitySwitch(direction) { CaseArmmovedirectionup:velocity = CCP (0,Ten); Break; CaseArmmovedirectiondown:velocity = CCP (0, -Ten); Break;default: Break; } [Physicsbody applyforce:velocity ATLOCALPOINT:CCP ( -,5)];}
The code is clear: first get the physical object of arm, and then according to the different rotation direction of the corresponding torque; rotate upward gives a positive amount to the y-axis of the moment, whereas a negative amount is given to the y-axis. I set the value here for +/-10, this is nothing to say, is the best value from many experiments. Finally, the torque is applied to the arm.
Note that I did not use the previous Applyforce: method, but using a similar method, is the overloaded method. This method has a parameter that sets the torque to the point on the arm. If you do not, the torque is applied to the average point of the arm by default (where the cat is not clear ;), it makes it difficult to rotate the arm.
Note that the parameter is CCP (20,5), which is approximately 20 and 10 high, which is located just above the center of the arm and the focus of the hand. The rear firing bullet is also based on this point as a reference point, which is detailed later. If you want to refactor, it might be better to calculate this point dynamically based on the arm's length and width.
Compiled to run the app, we found that when the touch arm does not put and move, the arm will slowly move in the direction of rotation. There is an acceleration and deceleration effect, the arm moves from the rest of the more laborious, when the turn up because of the kinetic energy, so the faster. This is also in line with the actual physical object movement.
By rotating your arms constantly, you find a small problem, what is it? Next announcement;)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(no.00003) iOS games simple robot projection game forming (vi)