Qt: how to make QGraphicsPixmapItem automatically move in a repeating route

180 views Asked by At

I am making a game now, and I want my QGraphicsPixmapItem automatically move from left to right and right to left between my view repeatedly in the same route.

To be more specific:

my enemy start at (0,0), and it start off by heading right (x()+3). When it hits the far right(800,0), it turns its way to the left and keep moving (x()-3) until it hits the far left(0,0). Likewise, it heads right and keep moving(x()+3) until it hits the far right(800,0), it turns left and so on and so on in a straight and same route.

So far, the outcome is that my item only stays in the far right and never move.

And my only clue is that it has this warning in my "application output", but I guess this is probably not the point to this error.

libpng warning: iCCP: known incorrect sRGB profile


I code this way and these are my thoughts:

  • use "while" in order to do this moving repeatedly.

  • use bool judge to turn the moving direction of my item when it hits the far right or far left of the view.

  • my pixmapitem's width is 100, so I write x()+100 in order to reach its right hand side when hitting the far right of the view.(my view's width is 800)

Is there anything I'm missing? Any thoughts sharing or logic debugging will be very very appreciated, thanks!

main window.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    scene(new QGraphicsScene(0,0,800,600)),
    enemyMoveTimer(new QTimer)
{
    ui->setupUi(this);
    ui->graphicsView->setScene(scene);

    //enemy
    Enemy * enemy = new Enemy();
    enemy->setPixmap(QPixmap(QPixmap(":/img/ghost.gif").scaled(100,100)));
    enemy->setPos(0,0);
    scene->addItem(static_cast<QGraphicsPixmapItem*>(enemy));

    enemyMoveTimer->start(100);
    enemy->connect(enemyMoveTimer, SIGNAL(timeout()), enemy, SLOT(enemyMove()));
}

main window.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QTimer>

#include "enemy.h"


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QGraphicsScene *scene;
    QTimer *enemyMoveTimer;
};

#endif // MAINWINDOW_H

enemy.cpp

#include "enemy.h"

Enemy::Enemy()
{

}

void Enemy::enemyMove()
{
     while(x()>=0 && x()+100<=800)
     {
         if(judge==0)
         {
             if (x()+100==800)
                 judge=1;

             else
                 setPos(x() +3,y());
          }

         else if(judge==1)
          {
             if (x()==0)
                 judge=0;

             else
                 setPos(x() -3,y());

          }
     }
}

enemy.h

#ifndef ENEMY_H
#define ENEMY_H

#include <QObject>
#include <QGraphicsPixmapItem>
#include <QTimer>

class Enemy: public QObject,public QGraphicsPixmapItem
{
    Q_OBJECT

public:
    Enemy();


public slots:
    void enemyMove();

private:
      bool judge = 0;
};

#endif // ENEMY_H
1

There are 1 answers

0
史努比 On

I just figure it out myself suddenly!!

-->take out the "while" in enemy.cpp then it can run.

we don't need while to do the repeat movement, because the timer has already called this function repeatedly.