Tracking the ball's movements
NoCodeConnect to the chipmunk simulator. Only after the corresponding method is extended can the position of the image be synchronously updated through chipmunk. This process can be implemented in multiple ways, such as storing a set of vertices to be checked. Fortunately, chipmunk has designed a very simple and abstract process for easy implementation. The following code is written once and rarely needs to be updated, unless one or more details are removed from the Code.
Therefore, the last step of creating a shape will be connected to a special body. Therefore, we also need to store a reference to a uiimageview in the same shape. This means that all the required data is in the middle. Remember, chipmunk always provides us with a convenient function:
View plaincopy to clipboardprint?
- cpspacehasheach (cpspacehash * hash, cpspacehashiterator func, void * Data)
This function takes a hash (an array), a function to be called for each element in the array and Optionally a custom data parameter.
This function requires a hash array as a parameter. The parameters in each array are
So, go to the tick method and add this code after the cpspacestep call already there:
View plaincopy to clipboardprint?
- // Call our function for each shape
-
- Cpspacehasheach (space-> activeshapes, & updateshape, nil );
As we mentioned earlier, the activeshape array needs to call the updateshape function, which will be implemented in the next step. When we implement the floor, we will discuss the active shape, so don't worry about the details.
At present, we need to implement the updateshape function, which is defined at the beginning, preferably after other function declarations:
View plaincopy to clipboardprint?
-
- VoidUpdateshape (Void* PTR,Void* Unused );// Updates a shape's visual representation (I. e. sprite)
-
- And then,ThisCode on the implementation file:
-
- // Updates a shape's visual representation (I. e. sprite)
-
- VoidUpdateshape (Void* PTR,Void* Unused ){
-
- // Get our shape
-
-
- Cpshape * shape = (cpshape *) PTR;
-
- // Make sure everything is as expected or tip & Exit
-
- If(Shape = nil | shape-> body = nil | shape-> DATA = nil ){
-
- Nslog (@"Unexpected shape please debug here ...");
-
- Return;
-
-
- }
-
- // Lastly checks if the object is an uiview of any kind
-
- // And update its position accordingly
-
- If([Shape-> data iskindofclass: [uiviewClass]) {
-
- [(Uiview *) Shape-> data setcenter: cgpointmake (shape-> body-> P. X, 480-shape-> body-> P. y)];
-
-
- }
-
- Else
-
- Nslog (@"The Shape Data wasn' t updateable using this code .");
-
- }
Although the last code segment is followed by 1 or 2 lines, many things are still implemented. I add a lot of tests and warnings to let you know what will happen, but it is important to update the position of the view. Note that we have not directly assigned the value of the Y axis, because the coordinate system of chipmunk is different from that of cocoa. The initial value of the cocoa system is 00 in the upper left corner and chipmunk in the lower left corner. Because different frameworks have different coordinate system systems, we need to pay more attention to these issues during implementation. Cocos2d and chipmunk have the same coordinate system.
Since we finally need this sphere for motion, it is time to compile and run it. If you follow this process, the result of the current operation should be that the sphere falls down from the top and then accelerates the movement. After not much time, the Sphere passes through the right side of the ground, this is what we need to do next.