PDA

View Full Version : Advance function doesnt get triggerd on timeout qt timer



nelisdnurste
24th April 2011, 16:10
Dear forum, Im writing a pong game that uses a timer to call advance on the graphicsscene to show the animation on the screen, somehow it doesnt work. I would really appreciate your help. here the code...

main.CPP :
QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
timer.start(1000 / 33);
return app.exec();

gameworld.H :

protected:
void drawBackground(QPainter *painter, const QRectF &rect);
virtual void keyPressEvent(QKeyEvent *event);
virtual void keyReleaseEvent(QKeyEvent *event);
void advance(int phase);

GameWorld.CPP :
void GameWorld::advance(int phase) {
printf("hello");
QPointF direction = ball.getDirection();
if (phase == 0) {
QRectF rect = sceneRect();
QPointF point = ball.pos();


//if (!collidingItems(Qt::IntersectsItemBoundingRect).i sEmpty())
// ball.setDirection(-direction.x(), direction.y());
if (point.y() <= rect.top() + 20) {
ball.setDirection(direction.x(), -direction.y());
}
if (point.y() >= rect.bottom() - 20) {
ball.setDirection(direction.x(), -direction.y());
}

if (point.x() <= rect.left() + 20) {
ball.setDirection(-direction.x(), direction.y());
}

if (point.x() >= rect.right() - 20) {
ball.setDirection(-direction.x(), direction.y());

}

} else
ball.moveBy(direction.x(), direction.y());

}

stampede
24th April 2011, 16:25
advance must be declared as a slot:


protected:
void drawBackground(QPainter *painter, const QRectF &rect);
virtual void keyPressEvent(QKeyEvent *event);
virtual void keyReleaseEvent(QKeyEvent *event);

protected slots:
void advance();

edit: and should have no parameters :)
If you want to have timeout with parameter, take a look at QTimeLine.

Zlatomir
24th April 2011, 16:26
At a first look i noticed two problems:
1) void advance(int phase); is not declared as a slot
2) and it has an int parameter (without a default value), so it can't be called without parameters.

You can read more about signals and slots here (http://doc.qt.nokia.com/4.7/signalsandslots.html).

nelisdnurste
24th April 2011, 16:35
Hey guys thanks for the fast reply,

with your help I totally figured it out:D

really thankfull,

Greetz

Niels