Skip to content
Browse files

- Updated example: added Play/Pause button and frame counter.

- Added AnimatedActor::resume.
  • Loading branch information...
1 parent 399f42d commit c3f15e9bef2f7bec2f99019d49a15833dda4053e @Yukigaru committed
View
2 examples/example1/src/entry_point.cpp
@@ -78,6 +78,8 @@ void run()
//all input events would be passed to RootActor::instance.handleEvent
//if done is true then User requests quit from app.
done = update();
+
+ example_update();
}
//so user closed application
View
82 examples/example1/src/example.cpp
@@ -1,12 +1,15 @@
#include "Actor.h"
#include "Sprite.h"
#include "Button.h"
+#include "example.h"
#include "TextActor.h"
#include "ResFont.h"
+#include "closure.h"
#include "RootActor.h"
#include "Resources.h"
#include "AnimatedActor.h"
#include "ResKeyframeAnimation.h"
+#include <sstream>
using namespace oxygine;
@@ -15,6 +18,33 @@ using namespace oxygine;
Resources gameResources;
Resources animResources;
+bool anim_stopped = false;
+
+
+struct ab
+{
+ void clicked(oxygine::Event *)
+ {
+ spAnimatedActor anim = RootActor::instance->getChildT<AnimatedActor>("anim");
+ spActor button = RootActor::instance->getChild("btn");
+ spTextActor caption = button->getChildT<TextActor>("caption");
+
+ if (!anim_stopped)
+ {
+ anim->stop();
+ caption->setText(L"Play");
+ }
+ else
+ {
+ anim->resume();
+ caption->setText(L"Pause");
+ }
+
+ anim_stopped = !anim_stopped;
+ }
+} a;
+
+
void example_init()
{
//load xml file with resources definition
@@ -24,6 +54,7 @@ void example_init()
animResources.loadXML("godscores_res.xml");
// temporary solution
+ // here we are connecting string resources names to real ResAnims
int res_count = animResources.getCount();
for (int ind = 0; ind < res_count; ++ind)
{
@@ -31,17 +62,64 @@ void example_init()
ResKeyframeAnimation * res_ka = dynamic_cast<ResKeyframeAnimation *>(res);
if (res_ka)
- {
res_ka->linkWithResAnims(&animResources);
- }
}
spAnimatedActor anim = new AnimatedActor();
+ anim->setName("anim");
anim->setPosition(250, 80);
anim->setResAnim(animResources.getT<ResKeyframeAnimation>("godscores"));
anim->play();
RootActor::instance->addChild(anim);
+
+ //create Play/Stop button
+ spButton button = new Button();
+ button->setName("btn");
+ button->setResAnim(gameResources.getResAnim("button"));
+ button->setPosition(50, VirtualHeight - 10);
+ button->setAnchor(Vector2(0.0, 1.0));
+ button->setInputChildrenEnabled(false);
+ EventCallback cb = CLOSURE(&a, &ab::clicked);
+ button->addEventListener(TouchEvent::CLICK, cb);
+ RootActor::instance->addChild(button);
+
+ //create Actor with Text and it to button as child
+ spTextActor caption = new TextActor();
+ caption->setName("caption");
+ TextStyle style;
+ style.font = gameResources.getResFont("main")->getFont();
+ style.color = Color(255, 255, 139, 255);
+ style.vAlign = TextStyle::VALIGN_MIDDLE;
+ style.hAlign = TextStyle::HALIGN_CENTER;
+ caption->setStyle(style);
+ caption->setSize(button->getSize());
+ caption->setText(L"Pause");
+ button->setScale(0.8f);
+ button->addChild(caption);
+
+ // create frame counter
+ spTextActor frame_text = new TextActor();
+ frame_text->setName("frame");
+ TextStyle style2;
+ style2.font = gameResources.getResFont("main")->getFont();
+ style2.color = Color(255, 255, 139, 255);
+ style2.vAlign = TextStyle::VALIGN_TOP;
+ frame_text->setStyle(style2);
+ frame_text->setPosition(10, 10);
+ frame_text->setScale(0.6f);
+ frame_text->setSize(200, 30);
+ RootActor::instance->addChild(frame_text);
+}
+
+void example_update()
+{
+ spAnimatedActor anim = RootActor::instance->getChildT<AnimatedActor>("anim");
+ spTextActor frame_text = RootActor::instance->getChildT<TextActor>("frame");
+
+ wostringstream ws;
+ ws << L"Current frame: " << anim->getCurrentFrame();
+ frame_text->setText(ws.str());
}
void example_destroy()
View
1 examples/example1/src/example.h
@@ -1,5 +1,6 @@
void example_init();
void example_destroy();
+void example_update();
const int VirtualWidth = 480;
const int VirtualHeight = 320;
View
6 src/AnimatedActor.cpp
@@ -71,6 +71,11 @@ void AnimatedActor::stop()
_playing = false;
}
+void AnimatedActor::resume()
+{
+ _playing = true;
+}
+
void AnimatedActor::gotoAndStop(unsigned int frame)
{
_currentFrame = frame;
@@ -86,6 +91,7 @@ void AnimatedActor::gotoAndPlay(unsigned int frame, bool looped)
_looped = looped;
_currentFrame = frame;
+ _t = 0.0f;
}
void AnimatedActor::doUpdate(const oxygine::UpdateState &us)
View
3 src/AnimatedActor.h
@@ -22,7 +22,8 @@ class AnimatedActor : public oxygine::Actor
void stop();
void gotoAndStop(uint frame);
virtual void gotoAndPlay(uint frame, bool looped = true);
-
+ void resume();
+
// accessors
bool isPlaying() const { return _playing; }

0 comments on commit c3f15e9

Please sign in to comment.
Something went wrong with that request. Please try again.