So i made a simple game where i protect my island from incoming enemies and at the start u control ship and when you press space you shoot a canon ball and all goes well until my canon ball reaches the point where i delete it.
I get a segmentation error which i believe means that im trying to reach memory that no longer exists, but still have no idea what is wrong...
and these are the errors i get:
error.jpg
link: https://ddgobkiprc33d.cloudfront.net...b6759bc53e.png
QGraphicsItem::x
graphiitem.jpg
link: https://ddgobkiprc33d.cloudfront.net...5b5438eff2.png
function inside Canon Class moveCanonBall()
mvoecanoin.jpg
link: https://ddgobkiprc33d.cloudfront.net...92fc602e76.png
and this
this.jpg
link: https://ddgobkiprc33d.cloudfront.net...e9062b1a01.png
This is Canon's header file:
#ifndef CANON_H
#define CANON_H
#include <QGraphicsItem>
#include <QObject>
#include <QGraphicsPathItem>
#include <QTimer>
Q_OBJECT
public:
~Canon();
static bool getCanonBallGone();
static bool setCanonBallGoneToFalse();
static bool setCanonBallGoneToTrue();
private:
int direction;
double xPl;
double yPl;
double newX;
double moveByX;
double moveByY;
bool moveDown;
double help;
bool ballHitEnd;
static bool CanonBallGone;
int splashLasting;
public slots:
void moveCanonBall();
};
#endif // CANON_H
#ifndef CANON_H
#define CANON_H
#include <QGraphicsItem>
#include <QObject>
#include <QGraphicsPathItem>
#include <QTimer>
class Canon: public QObject, public QGraphicsPixmapItem{
Q_OBJECT
public:
Canon(QGraphicsItem *parent=0);
~Canon();
QTimer *time;
static bool getCanonBallGone();
static bool setCanonBallGoneToFalse();
static bool setCanonBallGoneToTrue();
private:
int direction;
double xPl;
double yPl;
double newX;
double moveByX;
double moveByY;
bool moveDown;
double help;
bool ballHitEnd;
static bool CanonBallGone;
int splashLasting;
public slots:
void moveCanonBall();
};
#endif // CANON_H
To copy to clipboard, switch view to plain text mode
And this i canon's .cpp file:
#include "canon.h"
#include "game.h"
#include "math.h"
#include "soundeffects.h"
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QObject>
#include <QTimer>
#include <QDebug>
#include <QMediaPlayer>
#include "soundeffects.h"
extern Game *game;
bool Canon::CanonBallGone=false;
ballHitEnd = false;
setPixmap
(QPixmap(":/Images/Canon_1.png"));
// 30x25 SoundEffects::canonBallShot(1);
game->scene->addItem(this);
// bullet cords
moveByY = 1.0;
moveDown = false;
splashLasting = 0;
// shrani direction katero player trenutno is facing // saves the direction which the player is currently facing
// (left==3, right ==1)
direction = game->player->getLeftOrRightDirection();
if(direction == 1){
xPl = game->player->x()+83;
yPl = game->player->y()+42;
newX = game->player->x()+83;
}else if(direction == 3){
xPl = game->player->x()-18;
yPl = game->player->y()+42;
newX = game->player->x()-18;
}
connect(time,SIGNAL(timeout()),this,SLOT(moveCanonBall()));
time->start(5);
}
Canon::~Canon(){
delete time;
}
bool Canon::getCanonBallGone(){
return CanonBallGone;
}
bool Canon::setCanonBallGoneToFalse(){
CanonBallGone = false;
}
bool Canon::setCanonBallGoneToTrue(){
CanonBallGone = true;
}
void Canon::moveCanonBall(){
if(direction==1 && ballHitEnd==false){
// s pomocjo balisticne enacbe zracunamo y // with a help of this function we calculate y ( parabolic movement of the canon ball )
help = (0.5*(x()-xPl)*tan(0.6)-(0.5 *pow((x()-xPl),2))
/ (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
moveByY = yPl - help;
setY(moveByY);
setPos(x(),y());
// povecujemo x za 1.5 // incrementing x by 1.5
setX(x()+1.5);
// ce je canon ball v vodi spremenimo sliko v splash sliko // if canon ball hits its final destination we change picture to a splash
if(y() >= yPl+32){
ballHitEnd = true;
setPixmap
(QPixmap(":/Images/Splash_1.png"));
}
}
else if(direction==3 && ballHitEnd == false){
help = (0.5*(newX-xPl)*tan(0.6)-(0.5 *pow((newX-xPl),2))
/ (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
moveByY = yPl - help;
setY(moveByY);
setPos(x(),y());
// povecujemo x za 1.5 // incrementing x by 1.5
newX = newX + 1.5;
setX(x()-1.5);
// ce je canon ball v vodi spremenimo sliko v splash sliko // if canon ball hits its final destination we change picture to a splash
if(y() >= yPl+32){
ballHitEnd = true;
setPixmap
(QPixmap(":/Images/Splash_2.png"));
}
}
// slika ostane nekaj sekund nato se canon sporsti s pomnilnika // picture stays as a splash for couple of microseconds then we delete the canon ball from memory
if(ballHitEnd == true){
splashLasting++;
if(splashLasting == 15){
game->scene->removeItem(this);
CanonBallGone=true;
delete this;
}
}
// ce je canon ball sou izven scene ga sporstimo iz pomnilnika // if canon ball is out of the scene we remove it from the scene and delete it
if(x()<(0-30) || x()>game->getW()+30){
qDebug() << "deleted canonball";
game->scene->removeItem(this);
delete time;
CanonBallGone=true;
delete this;
}
}
#include "canon.h"
#include "game.h"
#include "math.h"
#include "soundeffects.h"
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QObject>
#include <QTimer>
#include <QDebug>
#include <QMediaPlayer>
#include "soundeffects.h"
extern Game *game;
bool Canon::CanonBallGone=false;
Canon::Canon(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
ballHitEnd = false;
setPixmap(QPixmap(":/Images/Canon_1.png")); // 30x25
SoundEffects::canonBallShot(1);
game->scene->addItem(this);
// bullet cords
moveByY = 1.0;
moveDown = false;
splashLasting = 0;
// shrani direction katero player trenutno is facing // saves the direction which the player is currently facing
// (left==3, right ==1)
direction = game->player->getLeftOrRightDirection();
if(direction == 1){
xPl = game->player->x()+83;
yPl = game->player->y()+42;
newX = game->player->x()+83;
}else if(direction == 3){
xPl = game->player->x()-18;
yPl = game->player->y()+42;
newX = game->player->x()-18;
}
time = new QTimer();
connect(time,SIGNAL(timeout()),this,SLOT(moveCanonBall()));
time->start(5);
}
Canon::~Canon(){
delete time;
}
bool Canon::getCanonBallGone(){
return CanonBallGone;
}
bool Canon::setCanonBallGoneToFalse(){
CanonBallGone = false;
}
bool Canon::setCanonBallGoneToTrue(){
CanonBallGone = true;
}
void Canon::moveCanonBall(){
if(direction==1 && ballHitEnd==false){
// s pomocjo balisticne enacbe zracunamo y // with a help of this function we calculate y ( parabolic movement of the canon ball )
help = (0.5*(x()-xPl)*tan(0.6)-(0.5 *pow((x()-xPl),2))
/ (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
moveByY = yPl - help;
setY(moveByY);
setPos(x(),y());
// povecujemo x za 1.5 // incrementing x by 1.5
setX(x()+1.5);
// ce je canon ball v vodi spremenimo sliko v splash sliko // if canon ball hits its final destination we change picture to a splash
if(y() >= yPl+32){
ballHitEnd = true;
setPixmap(QPixmap(":/Images/Splash_1.png"));
}
}
else if(direction==3 && ballHitEnd == false){
help = (0.5*(newX-xPl)*tan(0.6)-(0.5 *pow((newX-xPl),2))
/ (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
moveByY = yPl - help;
setY(moveByY);
setPos(x(),y());
// povecujemo x za 1.5 // incrementing x by 1.5
newX = newX + 1.5;
setX(x()-1.5);
// ce je canon ball v vodi spremenimo sliko v splash sliko // if canon ball hits its final destination we change picture to a splash
if(y() >= yPl+32){
ballHitEnd = true;
setPixmap(QPixmap(":/Images/Splash_2.png"));
}
}
// slika ostane nekaj sekund nato se canon sporsti s pomnilnika // picture stays as a splash for couple of microseconds then we delete the canon ball from memory
if(ballHitEnd == true){
splashLasting++;
if(splashLasting == 15){
game->scene->removeItem(this);
CanonBallGone=true;
delete this;
}
}
// ce je canon ball sou izven scene ga sporstimo iz pomnilnika // if canon ball is out of the scene we remove it from the scene and delete it
if(x()<(0-30) || x()>game->getW()+30){
qDebug() << "deleted canonball";
game->scene->removeItem(this);
delete time;
CanonBallGone=true;
delete this;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks