Results 1 to 11 of 11

Thread: Program crashes when Canon ball reaches "water"

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Unhappy Program crashes when Canon ball reaches "water"

    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:
    Qt Code:
    1. #ifndef CANON_H
    2. #define CANON_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QObject>
    6. #include <QGraphicsPathItem>
    7. #include <QTimer>
    8.  
    9. class Canon: public QObject, public QGraphicsPixmapItem{
    10. Q_OBJECT
    11. public:
    12. Canon(QGraphicsItem *parent=0);
    13. ~Canon();
    14. QTimer *time;
    15. static bool getCanonBallGone();
    16. static bool setCanonBallGoneToFalse();
    17. static bool setCanonBallGoneToTrue();
    18. private:
    19. int direction;
    20. double xPl;
    21. double yPl;
    22. double newX;
    23. double moveByX;
    24. double moveByY;
    25. bool moveDown;
    26. double help;
    27. bool ballHitEnd;
    28. static bool CanonBallGone;
    29. int splashLasting;
    30.  
    31. public slots:
    32. void moveCanonBall();
    33. };
    34.  
    35. #endif // CANON_H
    To copy to clipboard, switch view to plain text mode 

    And this i canon's .cpp file:

    Qt Code:
    1. #include "canon.h"
    2. #include "game.h"
    3. #include "math.h"
    4. #include "soundeffects.h"
    5. #include <QGraphicsPixmapItem>
    6. #include <QPixmap>
    7. #include <QObject>
    8. #include <QTimer>
    9. #include <QDebug>
    10. #include <QMediaPlayer>
    11.  
    12. #include "soundeffects.h"
    13.  
    14. extern Game *game;
    15.  
    16. bool Canon::CanonBallGone=false;
    17.  
    18. Canon::Canon(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
    19.  
    20. ballHitEnd = false;
    21.  
    22. setPixmap(QPixmap(":/Images/Canon_1.png")); // 30x25
    23. SoundEffects::canonBallShot(1);
    24. game->scene->addItem(this);
    25.  
    26. // bullet cords
    27. moveByY = 1.0;
    28. moveDown = false;
    29. splashLasting = 0;
    30.  
    31. // shrani direction katero player trenutno is facing // saves the direction which the player is currently facing
    32. // (left==3, right ==1)
    33. direction = game->player->getLeftOrRightDirection();
    34. if(direction == 1){
    35. xPl = game->player->x()+83;
    36. yPl = game->player->y()+42;
    37. newX = game->player->x()+83;
    38. }else if(direction == 3){
    39. xPl = game->player->x()-18;
    40. yPl = game->player->y()+42;
    41. newX = game->player->x()-18;
    42. }
    43.  
    44. time = new QTimer();
    45. connect(time,SIGNAL(timeout()),this,SLOT(moveCanonBall()));
    46. time->start(5);
    47. }
    48.  
    49. Canon::~Canon(){
    50. delete time;
    51. }
    52.  
    53. bool Canon::getCanonBallGone(){
    54. return CanonBallGone;
    55. }
    56.  
    57. bool Canon::setCanonBallGoneToFalse(){
    58. CanonBallGone = false;
    59. }
    60.  
    61. bool Canon::setCanonBallGoneToTrue(){
    62. CanonBallGone = true;
    63. }
    64.  
    65.  
    66. void Canon::moveCanonBall(){
    67.  
    68. if(direction==1 && ballHitEnd==false){
    69. // s pomocjo balisticne enacbe zracunamo y // with a help of this function we calculate y ( parabolic movement of the canon ball )
    70. help = (0.5*(x()-xPl)*tan(0.6)-(0.5 *pow((x()-xPl),2))
    71. / (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
    72. moveByY = yPl - help;
    73. setY(moveByY);
    74. setPos(x(),y());
    75.  
    76. // povecujemo x za 1.5 // incrementing x by 1.5
    77. setX(x()+1.5);
    78.  
    79. // ce je canon ball v vodi spremenimo sliko v splash sliko // if canon ball hits its final destination we change picture to a splash
    80. if(y() >= yPl+32){
    81.  
    82. ballHitEnd = true;
    83. setPixmap(QPixmap(":/Images/Splash_1.png"));
    84. }
    85.  
    86. }
    87.  
    88.  
    89. else if(direction==3 && ballHitEnd == false){
    90.  
    91. help = (0.5*(newX-xPl)*tan(0.6)-(0.5 *pow((newX-xPl),2))
    92. / (2*pow(20,2)*pow(cos(0.6),2))+yPl)-yPl;
    93. moveByY = yPl - help;
    94. setY(moveByY);
    95. setPos(x(),y());
    96.  
    97. // povecujemo x za 1.5 // incrementing x by 1.5
    98. newX = newX + 1.5;
    99. setX(x()-1.5);
    100.  
    101. // ce je canon ball v vodi spremenimo sliko v splash sliko // if canon ball hits its final destination we change picture to a splash
    102. if(y() >= yPl+32){
    103.  
    104. ballHitEnd = true;
    105. setPixmap(QPixmap(":/Images/Splash_2.png"));
    106. }
    107. }
    108.  
    109.  
    110.  
    111. // 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
    112. if(ballHitEnd == true){
    113.  
    114. splashLasting++;
    115.  
    116. if(splashLasting == 15){
    117. game->scene->removeItem(this);
    118. CanonBallGone=true;
    119. delete this;
    120. }
    121. }
    122. // 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
    123. if(x()<(0-30) || x()>game->getW()+30){
    124. qDebug() << "deleted canonball";
    125. game->scene->removeItem(this);
    126. delete time;
    127. CanonBallGone=true;
    128. delete this;
    129. }
    130. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by MongKong; 27th March 2019 at 16:23.

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. Replies: 3
    Last Post: 16th March 2015, 07:31
  3. Replies: 1
    Last Post: 5th February 2011, 21:14
  4. "Cannot run program "C:\Qt\4.3.3\bin\qmake": file not found
    By PeteH in forum Installation and Deployment
    Replies: 1
    Last Post: 7th February 2009, 00:48
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.