example.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "oxygine-framework.h"
  2. #include <functional>
  3. using namespace oxygine;
  4. //it is our resources
  5. //in real project you would have more than one Resources declarations.
  6. //It is important on mobile devices with limited memory and you would load/unload them
  7. Resources gameResources;
  8. class MainActor: public Actor
  9. {
  10. public:
  11. spTextField _text;
  12. spSprite _button;
  13. MainActor()
  14. {
  15. //create simple Sprite
  16. spSprite button = new Sprite();
  17. //setup it:
  18. //set button.png image. Resource 'button' defined in 'res.xml'
  19. button->setResAnim(gameResources.getResAnim("button"));
  20. //centered button at screen
  21. Vector2 pos = getStage()->getSize() / 2 - button->getSize() / 2;
  22. button->setPosition(pos);
  23. //handle click to button
  24. EventCallback cb = CLOSURE(this, &MainActor::buttonClicked);
  25. button->addEventListener(TouchEvent::CLICK, cb);
  26. #ifdef CLOSURE_FUNCTION //if your compiler supports lambda
  27. button->addEventListener(TouchEvent::CLICK, [](Event * e)->void
  28. {
  29. log::messageln("button clicked");
  30. });
  31. #endif
  32. //attach button as child to current actor
  33. addChild(button);
  34. _button = button;
  35. //second part
  36. //create TextField Actor
  37. spTextField text = new TextField();
  38. //attach it as child to button
  39. text->attachTo(button);
  40. //centered in button
  41. text->setPosition(button->getSize() / 2);
  42. //initialize text style
  43. TextStyle style;
  44. style.font = gameResources.getResFont("main")->getFont();
  45. style.color = Color::White;
  46. style.vAlign = TextStyle::VALIGN_MIDDLE;
  47. style.hAlign = TextStyle::HALIGN_CENTER;
  48. text->setStyle(style);
  49. text->setText("Click\nMe!");
  50. _text = text;
  51. }
  52. void buttonClicked(Event* event)
  53. {
  54. //user clicked to button
  55. //animate button by chaning color
  56. _button->setColor(Color::White);
  57. _button->addTween(Sprite::TweenColor(Color::Green), 500, 1, true);
  58. //animate text by scaling
  59. _text->setScale(1.0f);
  60. _text->addTween(Actor::TweenScale(1.1f), 500, 1, true);
  61. //and change text
  62. _text->setText("Clicked!");
  63. //lets create and run sprite with simple animation
  64. runSprite();
  65. }
  66. void runSprite()
  67. {
  68. spSprite sprite = new Sprite();
  69. addChild(sprite);
  70. int duration = 500;//500 ms
  71. int loops = -1;//infinity loops
  72. //animation has 7 columns, check 'res.xml'
  73. ResAnim* animation = gameResources.getResAnim("anim");
  74. //add animation tween to sprite
  75. //TweenAnim would change animation frames
  76. sprite->addTween(Sprite::TweenAnim(animation), duration, loops);
  77. Vector2 destPos = getStage()->getSize() - sprite->getSize();
  78. Vector2 srcPos = Vector2(0, destPos.y);
  79. //set sprite initial position
  80. sprite->setPosition(srcPos);
  81. //add another tween: TweenQueue
  82. //TweenQueue is a collection of tweens
  83. spTweenQueue tweenQueue = new TweenQueue();
  84. tweenQueue->setDelay(1500);
  85. //first, move sprite to dest position
  86. tweenQueue->add(Sprite::TweenPosition(destPos), 1500, 1);
  87. //then fade it out smoothly
  88. tweenQueue->add(Sprite::TweenAlpha(0), 500, 1);
  89. sprite->addTween(tweenQueue);
  90. //and remove sprite from tree when tweenQueue is empty
  91. //if you don't hold any references to sprite it would be deleted automatically
  92. tweenQueue->setDetachActor(true);
  93. }
  94. };
  95. //declare spMainActor as intrusive_ptr holder of MainActor
  96. typedef oxygine::intrusive_ptr<MainActor> spMainActor;
  97. void example_preinit() {}
  98. //called from entry_point.cpp
  99. void example_init()
  100. {
  101. //load xml file with resources definition
  102. gameResources.loadXML("res.xml");
  103. //lets create our client code simple actor
  104. //spMainActor was defined above as smart intrusive pointer (read more: http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/intrusive_ptr.html)
  105. spMainActor actor = new MainActor;
  106. //and add it to Stage as child
  107. getStage()->addChild(actor);
  108. }
  109. //called each frame from entry_point.cpp
  110. void example_update()
  111. {
  112. }
  113. //called each frame from entry_point.cpp
  114. void example_destroy()
  115. {
  116. //free previously loaded resources
  117. gameResources.free();
  118. }