Cocos2d-x accelerometer instance: moving ball

Source: Internet
Author: User
Tags addchild
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
  1. # Ifndef _ helloworld_scene_h __
  2. # DEFINE _ helloworld_scene_h __
  3. # Include "cocos2d. H"
  4. # Define kball_tag 100 ①
  5. # Define speed 30.0 ②
  6. Class helloworld: Public cocos2d: Layer
  7. {
  8. Public:
  9. Static cocos2d: Scene * createscene ();
  10. Virtual bool Init ();
  11. Virtual void onenter ();
  12. Virtual void onexit ();
  13. Virtual voidonacceleration (cocos2d: acceleration * ACC, cocos2d: Event * unused_event); ③
  14. Create_func (helloworld );
  15. };
  16. # 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
  1. Bool helloworld: Init ()
  2. {
  3. If (! Layer: Init ())
  4. {
  5. Returnfalse;
  6. }
  7. Sizevisiblesize = Director: getinstance ()-> getvisiblesize ();
  8. Pointorigin = Director: getinstance ()-> getvisibleorigin ();
  9. // The texture image width and height must be 2 to N power, 128x128
  10. Autobg = sprite: Create ("backgroundtile.png ",
  11. Rect (0, 0, visiblesize. Width, visiblesize. Height ));
  12. // Texture parameters of the texture, horizontal and vertical
  13. Texture2d: texparamstp = {gl_linear, gl_linear, gl_repeat, gl_repeat };
  14. BG-> gettexture ()-> settexparameters (TP );
  15. BG-> setposition (origin + point (visiblesize. width/2, visiblesize. Height/2 ));
  16. Addchild (BG, 0 );
  17. Autoball = sprite: Create ("ball.png ");
  18. Ball-> setposition (origin + point (visiblesize. width/2, visiblesize. Height/2 ));
  19. Addchild (ball, 10, kball_tag );
  20. Returntrue;
  21. }
  22. Void helloworld: onenter ()
  23. {
  24. Layer: onenter ();
  25. Log ("helloworldonenter ");
  26. Setaccelerometerenabled (true); ①
  27. }
  28. Void helloworld: onacceleration (acceleration * ACC, event * unused_event)
  29. {
  30. Log ("{x = % F, y = % f}", ACC-> X, ACC-> Y );
  31. Size visiblesize = Director: getinstance ()-> getvisiblesize ();
  32. Sprite * ball = (sprite *) This-> getchildbytag (kball_tag); ②
  33. Size S = ball-> getcontentsize (); ③
  34. Point p0 = ball-> getposition (); ④
  35. Float p1x = running X + acc-> X * speed; ⑤
  36. If (p1x-S. width/2) <0) {6
  37. P1x = S. width/2; 7
  38. }
  39. If (p1x + S. width/2)> visiblesize. width) {Signature
  40. P1x = visiblesize. Width-S. width/2; rows
  41. }
  42. Float p1y = running y + acc-> y * speed;
  43. P1y = p1y <0? -P1y: p1y;
  44. If (p1y-S. Height/2) <0 ){
  45. P1y = S. Height/2;
  46. }
  47. If (p1y + S. Height/2)> visiblesize. Height ){
  48. P1y = visiblesize. Height-S. Height/2;
  49. }
  50. Ball-> runaction (Place: Create (point (p1x, p1y); then
  51. }
  52. Void helloworld: onexit ()
  53. {
  54. Layer: onexit ();
  55. Log ("helloworldonexit ");
  56. }

 

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.