Next we will use an example to describe how to access the accelerometer through layer-based accelerometer events. As shown in this example, there is a ball in the scene. When we place the mobile device horizontally, the screen is up, and then shake the mobile device left and right to change the position of the ball.
Next let's take a look at the specific program code. First, let's take a look at the helloworldscene. h file. Its code is as follows:
[HTML]View plaincopy
- # Ifndef _ helloworld_scene_h __
- # DEFINE _ helloworld_scene_h __
- # Include "cocos2d. H"
- # Define kball_tag 100 ①
- # Define speed 30.0 ②
- Class helloworld: Public cocos2d: Layer
- {
- Public:
- Static cocos2d: Scene * createscene ();
- Virtual bool Init ();
- Virtual void onenter ();
- Virtual void onexit ();
- Virtual voidonacceleration (cocos2d: acceleration * ACC, cocos2d: Event * unused_event); ③
- Create_func (helloworld );
- };
- # Endif/_ helloworld_scene_h __
The first line of the above Code defines the macro kball_tag, which is the tag value of the ball genie. The second line defines the macro speed, which indicates the speed of the ball movement. The Code in line ③ declares the onacceleration function ,.
Helloworldscene. cpp file. Its main code is as follows:
[HTML]View plaincopy
- Bool helloworld: Init ()
- {
- If (! Layer: Init ())
- {
- Returnfalse;
- }
- Sizevisiblesize = Director: getinstance ()-> getvisiblesize ();
- Pointorigin = Director: getinstance ()-> getvisibleorigin ();
- // The texture image width and height must be 2 to N power, 128x128
- Autobg = sprite: Create ("backgroundtile.png ",
- Rect (0, 0, visiblesize. Width, visiblesize. Height ));
- // Texture parameters of the texture, horizontal and vertical
- Texture2d: texparamstp = {gl_linear, gl_linear, gl_repeat, gl_repeat };
- BG-> gettexture ()-> settexparameters (TP );
- BG-> setposition (origin + point (visiblesize. width/2, visiblesize. Height/2 ));
- Addchild (BG, 0 );
- Autoball = sprite: Create ("ball.png ");
- Ball-> setposition (origin + point (visiblesize. width/2, visiblesize. Height/2 ));
- Addchild (ball, 10, kball_tag );
- Returntrue;
- }
- Void helloworld: onenter ()
- {
- Layer: onenter ();
- Log ("helloworldonenter ");
- Setaccelerometerenabled (true); ①
- }
- Void helloworld: onacceleration (acceleration * ACC, event * unused_event)
- {
- Log ("{x = % F, y = % f}", ACC-> X, ACC-> Y );
- Size visiblesize = Director: getinstance ()-> getvisiblesize ();
- Sprite * ball = (sprite *) This-> getchildbytag (kball_tag); ②
- Size S = ball-> getcontentsize (); ③
- Point p0 = ball-> getposition (); ④
- Float p1x = running X + acc-> X * speed; ⑤
- If (p1x-S. width/2) <0) {6
- P1x = S. width/2; 7
- }
- If (p1x + S. width/2)> visiblesize. width) {Signature
- P1x = visiblesize. Width-S. width/2; rows
- }
- Float p1y = running y + acc-> y * speed;
- P1y = p1y <0? -P1y: p1y;
- If (p1y-S. Height/2) <0 ){
- P1y = S. Height/2;
- }
- If (p1y + S. Height/2)> visiblesize. Height ){
- P1y = visiblesize. Height-S. Height/2;
- }
- Ball-> runaction (Place: Create (point (p1x, p1y); then
- }
- Void helloworld: onexit ()
- {
- Layer: onexit ();
- Log ("helloworldonexit ");
- }
Line 1 of the above Code enables the accelerator device. This code is in the helloworld: onenter () function, which means that the accelerator device is enabled when you enter the layer.
In line ②, the Code obtains the small ball genie object through tag attributes. Line ③ code ball-> getcontentsize () to get the ball size. Line ④ code ball-> getposition () is the position of the ball. The fifth line of code is running X + acc-> X * speed, which is the position for moving the X axis of the ball. However, when the left and right sides of the ball exceed the screen, line 6 of code is (p1x-s. width/2) <0 indicates that the screen is beyond the left. In this case, we need to use the code p1x = s in line 7. width/2 reset its X axis coordinates. The first line of code (p1x + S. width/2)> visiblesize. width is used to determine whether the screen is exceeded. In this case, we need to use the nth line of code p1x = visiblesize. width-s. width/2 reset its X axis coordinates. Similar Y axis judgment is also required, and the code will not be explained.
After obtaining the coordinates of the ball, run the code ball-> runaction (Place: Create (point (p1x, p1y) in the nth line ))) is to execute an action to move the ball to a new position.
More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): c ++ development" book exchange discussion site: http://www.cocoagame.net welcome to join the cocos2d-x Technology Discussion Group: 257760386, 327403678