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. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Program crashes when Canon ball reaches "water"

    that means player can only shoot one canon ball at once, he can make the next shot after the first canon ball has been deleted
    There are better ways to do this. For example, all classes based on QObject have a QObject::destroyed() signal that is emitted in the QObject destructor. You can use this signal to detect when your cannonball is being destroyed instead of relying of a static (global) variable:

    Qt Code:
    1. void Ship::fireCannonball()
    2. {
    3. if ( canFireCannon() )
    4. {
    5. Canon * cannonball = new Canon( this );
    6. connect( canonball, &QObject::destroyed, this, &Ship::onCannonballDestroyed );
    7. cannonballInFlight = true;
    8. }
    9. }
    10.  
    11. void Ship::onCannonballDestroyed( QObject * ) // ignore "obj" argument since we know it is a cannonball
    12. {
    13. cannonballInFlight = false; // allows a new ball to be fired now
    14. }
    15.  
    16. bool Ship::canFireCannon()
    17. {
    18. return ( cannonballInFlight == false ); // cannonballInFlight is a member variable of Ship, initialized to false
    19. }
    To copy to clipboard, switch view to plain text mode 

    By making cannonballInFlight a member of Ship (instead of a global variable in Canon), you can have any number of ships firing cannonballs at the same time. Each Ship only has to check that it is allowed to fire another cannonball before it fires again. The last cannonball that it fired tells it when it is gone using the destroyed() signal, so the ship doesn't really have to do anything except wait for that after the cannonball is fired.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    MongKong (28th March 2019)

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
  •  
Qt is a trademark of The Qt Company.