PDA

View Full Version : I need help with QPropertyAnimation



Davidf7823
16th June 2013, 21:59
Hello! :D
I am working on a game, but I can't get the player to move when the WASD keys are pressed. I even tried making a new project and simply animating a square, but it wouldn't even work for me when I did that!
NOTE: In the following code, I removed all my failed attempts at animating, so the code does run and there are no fatal errors.

My code:

dialog.h


#include "player.h"
#include "zombie.h"

#include <QDialog>
#include <QGraphicsScene>
#include <QPropertyAnimation>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
QGraphicsView *view;
QGraphicsScene *scene;
player *p;
zombie *z;
void keyPressEvent(QKeyEvent *event);
private:
Ui::Dialog *ui;
};


player.h


#include <QGraphicsItem>
#include <QPropertyAnimation>

class player : public QGraphicsItem
{
public:
player();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget);
void moveUp();
void moveLeft();
void moveDown();
void moveRight();
private slots:
};


dialog.cpp


#include "dialog.h"
#include "ui_dialog.h"
#include "player.h"
#include "zombie.h"

#include <QHBoxLayout>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QSizePolicy>
#include <QPropertyAnimation>
#include <QKeyEvent>
#include <iostream>
#include <QPushButton>

using namespace std;

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setMinimumSize(1000, 700);
this->setMaximumSize(size());
this->setWindowTitle("Game");

QHBoxLayout *layout = new QHBoxLayout();
this->setLayout(layout);

QGraphicsView *view = new QGraphicsView();
view->setRenderHints(QPainter::Antialiasing);

QGraphicsScene *scene = new QGraphicsScene();
view->setScene(scene);

QBrush backgroundBrush(Qt::white);
scene->setBackgroundBrush(backgroundBrush);

player *p = new player();
scene->addItem(p);

zombie *z = new zombie();
scene->addItem(z);

layout->addWidget(view);
view->show();
}

Dialog::~Dialog()
{
delete ui;
qDebug("Dialog Deleted");
}

void Dialog::keyPressEvent(QKeyEvent *event){
switch(event->key()){
case Qt::Key_W:
qDebug("Key Pressed: W");
p->moveUp();
break;
case Qt::Key_A:
qDebug("Key Pressed: A");
p->moveLeft();
break;
case Qt::Key_S:
qDebug("Key Pressed: S");
p->moveDown();
break;
case Qt::Key_D:
qDebug("Key Pressed: D");
p->moveRight();
break;
}
}


main.cpp


#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();

return a.exec();
}


player.cpp


#include "player.h"

#include <QGraphicsItem>
#include <QPainter>
#include <QPropertyAnimation>
#include <QObject>

player::player()
{
setFlag(QGraphicsItem::ItemIsMovable);
qDebug("Player Created");
}

QRectF player::boundingRect() const
{
return QRectF(0, 0, 25, 25);
}

void player::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
{
QRectF rec = boundingRect();

painter->setBrush(Qt::red);
painter->setPen(Qt::red);

painter->drawEllipse(rec);
}

void player::moveUp(){

}

void player::moveLeft(){

}

void player::moveDown(){

}

void player::moveRight(){

}


I really wanted to put the respective animations in the functions moveUp(), moveLeft(), moveDown, moveRight() in the class player. Could you please help me by showing my how to do the animations in these locations.
Also, while I'm here, is there anything I should change to my program while I am not to far into the game?

Thanks :D
-David

ChrisW67
19th June 2013, 10:08
I don't see how you can use QPropertyAnimation on your player class because it has no properties exposed. You would need to derive from QGraphicsObject which would give you a pos property to work with.