example.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 button 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. //register click handler to button
  24. EventCallback cb = CLOSURE(this, &MainActor::buttonClicked);
  25. button->addEventListener(TouchEvent::CLICK, cb);
  26. //animate mouse over and mouse out events
  27. cb = CLOSURE(this, &MainActor::buttonOverOut);
  28. button->addEventListener(TouchEvent::OVER, cb);
  29. button->addEventListener(TouchEvent::OUT, cb);
  30. #ifdef CLOSURE_FUNCTION //if your compiler supports lambda
  31. button->addEventListener(TouchEvent::CLICK, [](Event * e)->void
  32. {
  33. log::messageln("button clicked");
  34. });
  35. #endif
  36. //attach button as child to current actor
  37. addChild(button);
  38. _button = button;
  39. //create TextField Actor
  40. spTextField text = new TextField();
  41. //attach it as child to button
  42. text->attachTo(button);
  43. //centered in button
  44. text->setPosition(button->getSize() / 2);
  45. //initialize text style
  46. TextStyle style = TextStyle(gameResources.getResFont("main")).withColor(Color::White).alignMiddle();
  47. text->setStyle(style);
  48. text->setText("Click\nMe!");
  49. _text = text;
  50. }
  51. void buttonClicked(Event* event)
  52. {
  53. //user clicked to button
  54. //animate button by chaning color
  55. _button->setColor(Color::White);
  56. _button->addTween(Sprite::TweenColor(Color::Green), 500, 1, true);
  57. //animate text by scaling
  58. _text->setScale(1.0f);
  59. _text->addTween(Actor::TweenScale(1.1f), 500, 1, true);
  60. //and change text
  61. _text->setText("Clicked!");
  62. //lets create and run sprite with simple animation
  63. runSprite();
  64. }
  65. void buttonOverOut(Event* e)
  66. {
  67. if (e->type == TouchEvent::OVER)
  68. {
  69. _button->addTween(Sprite::TweenAddColor(Color(64, 64, 64, 0)), 300);
  70. }
  71. if (e->type == TouchEvent::OUT)
  72. {
  73. _button->addTween(Sprite::TweenAddColor(Color(0, 0, 0, 0)), 300);
  74. }
  75. }
  76. void runSprite()
  77. {
  78. spSprite sprite = new Sprite();
  79. addChild(sprite);
  80. int duration = 600;//ms
  81. int loops = -1;//infinity loops
  82. //animation has 8 columns - frames, check 'res.xml'
  83. ResAnim* animation = gameResources.getResAnim("anim");
  84. //add animation tween to sprite
  85. //TweenAnim would change animation frames
  86. sprite->addTween(Sprite::TweenAnim(animation), duration, loops);
  87. Vector2 destPos = getStage()->getSize() - sprite->getSize();
  88. Vector2 srcPos = Vector2(0, destPos.y);
  89. //set sprite initial position
  90. sprite->setPosition(srcPos);
  91. //add another tween: TweenQueue
  92. //TweenQueue is a collection of tweens
  93. spTweenQueue tweenQueue = new TweenQueue();
  94. tweenQueue->setDelay(1500);
  95. //first, move sprite to dest position
  96. tweenQueue->add(Sprite::TweenPosition(destPos), 2500, 1);
  97. //then fade it out smoothly
  98. tweenQueue->add(Sprite::TweenAlpha(0), 500, 1);
  99. sprite->addTween(tweenQueue);
  100. //and remove sprite from tree when tweenQueue is empty
  101. //if you don't hold any references to sprite it would be deleted automatically
  102. tweenQueue->detachWhenDone();
  103. }
  104. };
  105. //declare spMainActor as intrusive_ptr holder of MainActor
  106. typedef oxygine::intrusive_ptr<MainActor> spMainActor;
  107. //you could use DECLARE_SMART preprocessor definition it does the same:
  108. //DECLARE_SMART(MainActor, spMainActor)
  109. void example_preinit() {}
  110. //called from main.cpp
  111. void example_init()
  112. {
  113. //load xml file with resources definition
  114. gameResources.loadXML("res.xml");
  115. //lets create our client code simple actor
  116. //spMainActor was defined above as smart intrusive pointer (read more: http://www.boost.org/doc/libs/1_60_0/libs/smart_ptr/intrusive_ptr.html)
  117. spMainActor actor = new MainActor;
  118. //and add it to Stage as child
  119. getStage()->addChild(actor);
  120. }
  121. //called each frame from main.cpp
  122. void example_update()
  123. {
  124. }
  125. //called each frame from main.cpp
  126. void example_destroy()
  127. {
  128. //free previously loaded resources
  129. gameResources.free();
  130. }