我在一次执行多个函数时遇到了问题。具体来说,我有两个类(MyRect和Space类)。这个想法类似于太空入侵者,但我坚持从一开始。在类MyRect中,我定义了两个矩形::body (船体)和bullet (船体).In,主要的类空间是创建ship as MyRect带有body && bullet的新对象rectangles.Also,还有keyPress事件的定义。问题是,当我发射子弹时,所有的东西都停止了子弹的移动,直到MyRect::fireBullet(int x,int y)循环完成,我不能在这个event.Obviously期间移动船,我做了一些基本的错误,所以如果有人愿意澄清这一点。
以下是示例代码::
MyRect.h
#include <QWidget>
#include <QRect>
#include <QMainWindow>
#include <QPainter>
#include <QLabel>
#include <ctime>
#include <QtGui/QMainWindow>
class space;
class MyRect : public QObject {
Q_OBJECT
public:
MyRect(int in_x, int in_y, int in_w, int in_h, QWidget* parent)
{
itsx = in_x;
itsy = in_y;
itsw = in_w;
itsh = in_h;
body = new QRect(itsx, itsy, itsw, itsh);
bullet = new QRect(itsx+41, itsy-15, itsw/8, itsh/2);
itsParent = parent;
}
~MyRect() {}
void move(int x ,int y);
public slots:
void fireBullet(int x, int y);
private:
int itsx;
int itsy;
int itsw;
int itsh;
QWidget* itsParent;
QRect* body;
QRect* bullet;
friend class space;
};MyRect.cpp
#include "MyRect.h"
void wait( float seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
void MyRect::move(int x, int y)
{
body->moveTo(x,y);
bullet->moveTo(x+35, y-15);
}
void MyRect::fireBullet(int x, int y)
{
y = y-15;
for(int i=0 ; i<200 ; i++)
{
bullet->moveTo(x+41, y--);
itsParent->repaint();
wait(0.001);
}
}space.h
#include <QKeyEvent>
#include <QMouseEvent>
#include "MyRect.h"
class space : public QMainWindow
{
Q_OBJECT
public:
space(QWidget *parent = 0);
~space(){}
protected:
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent* event);
private:
private:
int x;
int y;
int w;
int h;
MyRect* ship;
signals:
void fireBullet(int x, int y);
};space.cpp
#include "space.h"
#include <QApplication>
space::space(QWidget *parent)
: QMainWindow(parent)
{
x = 170;
y = 250;
w = 90;
h = 25;
ship = new MyRect(x,y,w,h, this);
connect(this, SIGNAL(fireBullet(int,int)), ship, SLOT(fireBullet(int,int)) );
}
void space::paintEvent(QPaintEvent *event)
{
QPen pen(Qt::black, 2, Qt::SolidLine);
QColor hourColor(0, 255, 0);
QPainter painter(this);
painter.setBrush(hourColor);
painter.setPen(pen);
painter.drawRect( *(ship->body) );
painter.drawRect( *(ship->bullet) );
}
void space::keyPressEvent(QKeyEvent* event)
{
switch(event->key()) {
case Qt::Key_D :
{
x = x+10;
ship->move(x,y);
this->update();
break;
}
case Qt::Key_A :
{
x = x-10;
ship->move(x,y);
this->update();
break;
}
case Qt::Key_M :
{
emit fireBullet(x,y);
break;
}
} }
main.cpp
#include "space.h"
#include <QDesktopWidget>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
space window;
window.setWindowTitle("Lines");
window.resize(500,500);
window.show();
return app.exec();
}谢谢你的回答。
发布于 2010-12-16 00:11:24
你有一个架构问题。您正在做的是在其fireBullet方法中循环移动项目符号。当该循环运行时,程序的其余部分不会运行,因为单个线程一次只能做一件事。
解决方案是重构您的代码,以便每次调用某种更新方法时,屏幕上的所有内容都会通过一帧动画进行更新。基本上,你只需要保持足够的状态,你在哪里,你移动的速度有多快,在消失之前你可以走多远,这样你就可以在每一帧中按所需的量移动。
您要更改的另一件事是让keyPressEvent更新宇宙飞船的状态,以了解它应该移动的方向,以便它可以在其常规paintEvent上移动。为此,您可以使用QTimer
https://stackoverflow.com/questions/4450291
复制相似问题