FlyDoodle
4th January 2020, 00:29
Hello!
So the game works just fine but when i add the code which creates hp, hunger and thirst bar for my game, the game doesn't load on normal run (and it crashes), but it does fully load when i run the debugger and it works just fine.
this is the code that i run in game.cpp:
void Game::drawBars()
{
bars = new Bars();
scene->addItem(bars->hp);
scene->addItem(bars->hunger);
scene->addItem(bars->thirst);
}
This is Bars.h file
#ifndef BARS_H
#define BARS_H
#include <QGraphicsRectItem>
#include <QGraphicsItem>
#include <QObject>
class Bars: public QObject{
Q_OBJECT
public:
Bars();
QGraphicsPolygonItem *hp;
QGraphicsPolygonItem *hunger;
QGraphicsPolygonItem *thirst;
QLineF line;
QLineF dist;
void setPosition();
private:
int hp_amount;
int hunger_amount;
int thirst_amount;
float size;
float hp_height;
float hunger_height;
float thirst_height;
};
#endif // BARS_H
This is Bars.cpp file
Bars::Bars()
{
hp_amount = 1400;
hunger_amount = 1400;
thirst_amount = 1400;
QVector<QPointF> hp_points,hunger_points,thirst_points;
hp_points << QPointF(1,0) << QPointF(2,0) << QPointF(2.33,1) << QPointF(2.66,2) << QPointF(3,3) << QPointF(3,4) << QPointF(2.66,5) << QPointF(2.33,6) << QPointF(2,7) << QPointF(1,7) << QPointF(0.66,6) << QPointF(0.33,5) << QPointF(0,4) << QPointF(0,3) << QPointF(0.33,2) << QPointF(0.66,1);
hunger_points << QPointF(-1,0) << QPointF(0,0) << QPointF(-0.33,1) << QPointF(-0.66,2) << QPoint(-1,3) << QPoint(-1,4) << QPointF(-0.66,5) << QPointF(-0.33,6) << QPointF(0,7) << QPoint(-1,7) << QPointF(-1.33,6) << QPointF(-1.66,5) << QPoint(-2,4) << QPointF(-2,3) << QPointF(-1.66,2) << QPointF(-1.33,1);
thirst_points << QPoint(3,0) << QPoint(4,0) << QPointF(4.33,1) << QPointF(4.66,2) << QPoint(5,3) << QPoint(5,4) << QPointF(4.66,5) << QPointF(4.33,6) << QPointF(4,7) << QPointF(3,7) << QPointF(3.33,6) << QPointF(3.66,5) << QPointF(4,4) << QPointF(4,3) << QPointF(3.66,2) << QPointF(3.33,1);
line.setPoints(hunger_points[0], hp_points[0]);
dist.setPoints(hp_points[0], hp_points[1]);
size = game->getViewHeight()/57;
for(int i=0;i<hp_points.size();i++){
hp_points[i] *= size;
hunger_points[i] *= size;
thirst_points[i] *= size;
}
hp = new QGraphicsPolygonItem(QPolygonF(hp_points));
hunger = new QGraphicsPolygonItem(QPolygonF(hunger_points));
thirst = new QGraphicsPolygonItem(QPolygonF(thirst_points));
hp_height = hp->boundingRect().height();
hunger_height = hunger->boundingRect().height();
thirst_height = thirst->boundingRect().height();
setPosition();
hp->setBrush(QBrush("#f44047"));
hunger->setBrush(QBrush("#efbc12"));
thirst->setBrush(QBrush("#00a8f3"));
//hp->setPen(Qt::NoPen);
//hunger->setPen(Qt::NoPen);
//thirst->setPen(Qt::NoPen);
hp->setZValue(16);
}
void Bars::setPosition()
{
game->scene->removeItem(hp);
game->scene->addItem(hp);
game->scene->removeItem(hunger);
game->scene->addItem(hunger);
game->scene->removeItem(thirst);
game->scene->addItem(thirst);
hp->setPos(game->hotbarSlots->slot[8]->x()+game->hotbarSlots->slot[8]->boundingRect().width()+(game->getViewWidth()*0.175)/2-dist.length()/2, game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-hp_height);
hunger->setPos(hp->x()-line.length()/2,game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-hunger_height);
thirst->setPos(hp->x()+line.length()/2,game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-thirst_height);
}
Any help or advice is appreciated !!!
So the game works just fine but when i add the code which creates hp, hunger and thirst bar for my game, the game doesn't load on normal run (and it crashes), but it does fully load when i run the debugger and it works just fine.
this is the code that i run in game.cpp:
void Game::drawBars()
{
bars = new Bars();
scene->addItem(bars->hp);
scene->addItem(bars->hunger);
scene->addItem(bars->thirst);
}
This is Bars.h file
#ifndef BARS_H
#define BARS_H
#include <QGraphicsRectItem>
#include <QGraphicsItem>
#include <QObject>
class Bars: public QObject{
Q_OBJECT
public:
Bars();
QGraphicsPolygonItem *hp;
QGraphicsPolygonItem *hunger;
QGraphicsPolygonItem *thirst;
QLineF line;
QLineF dist;
void setPosition();
private:
int hp_amount;
int hunger_amount;
int thirst_amount;
float size;
float hp_height;
float hunger_height;
float thirst_height;
};
#endif // BARS_H
This is Bars.cpp file
Bars::Bars()
{
hp_amount = 1400;
hunger_amount = 1400;
thirst_amount = 1400;
QVector<QPointF> hp_points,hunger_points,thirst_points;
hp_points << QPointF(1,0) << QPointF(2,0) << QPointF(2.33,1) << QPointF(2.66,2) << QPointF(3,3) << QPointF(3,4) << QPointF(2.66,5) << QPointF(2.33,6) << QPointF(2,7) << QPointF(1,7) << QPointF(0.66,6) << QPointF(0.33,5) << QPointF(0,4) << QPointF(0,3) << QPointF(0.33,2) << QPointF(0.66,1);
hunger_points << QPointF(-1,0) << QPointF(0,0) << QPointF(-0.33,1) << QPointF(-0.66,2) << QPoint(-1,3) << QPoint(-1,4) << QPointF(-0.66,5) << QPointF(-0.33,6) << QPointF(0,7) << QPoint(-1,7) << QPointF(-1.33,6) << QPointF(-1.66,5) << QPoint(-2,4) << QPointF(-2,3) << QPointF(-1.66,2) << QPointF(-1.33,1);
thirst_points << QPoint(3,0) << QPoint(4,0) << QPointF(4.33,1) << QPointF(4.66,2) << QPoint(5,3) << QPoint(5,4) << QPointF(4.66,5) << QPointF(4.33,6) << QPointF(4,7) << QPointF(3,7) << QPointF(3.33,6) << QPointF(3.66,5) << QPointF(4,4) << QPointF(4,3) << QPointF(3.66,2) << QPointF(3.33,1);
line.setPoints(hunger_points[0], hp_points[0]);
dist.setPoints(hp_points[0], hp_points[1]);
size = game->getViewHeight()/57;
for(int i=0;i<hp_points.size();i++){
hp_points[i] *= size;
hunger_points[i] *= size;
thirst_points[i] *= size;
}
hp = new QGraphicsPolygonItem(QPolygonF(hp_points));
hunger = new QGraphicsPolygonItem(QPolygonF(hunger_points));
thirst = new QGraphicsPolygonItem(QPolygonF(thirst_points));
hp_height = hp->boundingRect().height();
hunger_height = hunger->boundingRect().height();
thirst_height = thirst->boundingRect().height();
setPosition();
hp->setBrush(QBrush("#f44047"));
hunger->setBrush(QBrush("#efbc12"));
thirst->setBrush(QBrush("#00a8f3"));
//hp->setPen(Qt::NoPen);
//hunger->setPen(Qt::NoPen);
//thirst->setPen(Qt::NoPen);
hp->setZValue(16);
}
void Bars::setPosition()
{
game->scene->removeItem(hp);
game->scene->addItem(hp);
game->scene->removeItem(hunger);
game->scene->addItem(hunger);
game->scene->removeItem(thirst);
game->scene->addItem(thirst);
hp->setPos(game->hotbarSlots->slot[8]->x()+game->hotbarSlots->slot[8]->boundingRect().width()+(game->getViewWidth()*0.175)/2-dist.length()/2, game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-hp_height);
hunger->setPos(hp->x()-line.length()/2,game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-hunger_height);
thirst->setPos(hp->x()+line.length()/2,game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-thirst_height);
}
Any help or advice is appreciated !!!