From a35824a3319ae9c304263185c4efc449e2c2cf73 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 4 Apr 2011 11:27:54 +0200 Subject: added simple pacman animation --- pacman-c++/actor.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'pacman-c++/actor.cpp') diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 860f490..5080497 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -1,7 +1,50 @@ #include "actor.h" +#include "animationmanager.h" +#include +#include +#include + +#define SPRITE_MARGIN 2 +#define SPRITE_PLAYER_WIDTH 16 +#define SPRITE_PLAYER_HEIGHT 16 + +static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) +{ + return (progress == 1.0) ? end : start; +} Actor::Actor(Type type) - : PixmapItem("google-pacman-sprite"), m_type(type), m_direction(Actor::None) + : m_type(type), m_direction(Actor::None) { - setSprite(2, 2, 16, 16); + m_pix = ":/" + QString("actor%1").arg(type); + setPixmap(m_pix); + setSprite(82, 82, 16, 16); + + m_direction = Actor::Left; + + qRegisterAnimationInterpolator(myBooleanInterpolator); + QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this); + group->setLoopCount(-1); + for (int i = 0; i < 4; i++) + { + PixmapItem *step = new PixmapItem(m_pix, this); + int x = i * 20 + SPRITE_MARGIN; + int y = m_direction * 20 + SPRITE_MARGIN; + step->setSprite(x, y, SPRITE_PLAYER_WIDTH, SPRITE_PLAYER_HEIGHT); + step->setVisible(false); + + + QPropertyAnimation *anim = new QPropertyAnimation(step, "visible", group); + anim->setDuration(0); + anim->setEndValue(true); + + group->addPause(100); + + QPropertyAnimation *anim2 = new QPropertyAnimation(step, "visible", group); + anim2->setDuration(0); + anim2->setEndValue(false); + } + + AnimationManager::self()->registerAnimation(group); + group->start(); } -- cgit v1.2.3