I need help with QPropertyAnimation
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
Code:
#include "player.h"
#include "zombie.h"
#include <QDialog>
#include <QGraphicsScene>
#include <QPropertyAnimation>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
player *p;
zombie *z;
private:
Ui::Dialog *ui;
};
player.h
Code:
#include <QGraphicsItem>
#include <QPropertyAnimation>
{
public:
player();
void moveUp();
void moveLeft();
void moveDown();
void moveRight();
private slots:
};
dialog.cpp
Code:
#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;
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setMinimumSize(1000, 700);
this->setMaximumSize(size());
this->setWindowTitle("Game");
this->setLayout(layout);
view
->setRenderHints
(QPainter::Antialiasing);
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
Code:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Dialog w;
w.show();
return a.exec();
}
player.cpp
Code:
#include "player.h"
#include <QGraphicsItem>
#include <QPainter>
#include <QPropertyAnimation>
#include <QObject>
player::player()
{
qDebug("Player Created");
}
QRectF player
::boundingRect() const {
}
{
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
Re: I need help with QPropertyAnimation
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.