TimerControlledGraphicsPixmapObject::TimerControlledGraphicsPixmapObject(QPixmap pixmap, int speed, QGraphicsItem* parent) :
QObject(), QGraphicsPixmapItem(pixmap, parent)
{
+ pixelsPerMove_ = 2;
setSpeed(speed);
- direction_ = S;
+ changeDirection();
connect(&timer_,SIGNAL(timeout()),this,SLOT(move()));
}
void TimerControlledGraphicsPixmapObject::startMoving()
{
- timer_.start();
+ if (!stoppedBecauseInvalidTime_)
+ timer_.start();
}
void TimerControlledGraphicsPixmapObject::stopMoving()
void TimerControlledGraphicsPixmapObject::setSpeed(int speed)
{
- timer_.setInterval(1000/speed); //converts from pixels in second to milliseconds per pixel
+ if (speed >0)
+ {
+ timer_.setInterval(1000/(speed/pixelsPerMove_)); //converts from pixels in second to milliseconds per pixels moved at once
+ stoppedBecauseInvalidTime_ = false;
+ }
+ else
+ stoppedBecauseInvalidTime_ = true;
+ timer_.stop();
}
+
void TimerControlledGraphicsPixmapObject::move()
{
if (direction_ == E || direction_ == SE || direction_ == NE)
{
- newx++;
+ newx+= pixelsPerMove_;
}
if (direction_ == W || direction_ == SW || direction_ == NW)
{
- newx--;
+ newx-=pixelsPerMove_;
}
if (direction_ == S || direction_ == SE || direction_ == SW)
{
- newy++;
+ newy+=pixelsPerMove_;
}
if (direction_ == N || direction_ == NE || direction_ == NW)
{
- newy--;
+ newy-=pixelsPerMove_;
}