Results 1 to 4 of 4

Thread: Play a simple .gif-animation and KeyEvent-question :)

  1. #1
    Join Date
    Sep 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Talking Play a simple .gif-animation and KeyEvent-question :)

    Hello! I am trying to learn how to create a simple game following some nice tutorials on Youtube. I have two questions:

    1. There is nothing wrong with the "moving code" I use. It does exacly what I tell it to do. I want to learn how to NOT make the player stop moving to the left or right when I click the spacebutton to shoot

    Qt Code:
    1. void Player::keyPressEvent(QKeyEvent *event){
    2. // move the player left and right
    3. if (event->key() == Qt::Key_Left){
    4. if (pos().x() > 0)
    5. setPos(x()-10,y());
    6. }
    7. else if (event->key() == Qt::Key_Right){
    8. if (pos().x() + 100 < 800)
    9. setPos(x()+10,y());
    10. }
    11. // shoot with the spacebar
    12. else if (event->key() == Qt::Key_Space){
    13. // create a bullet
    14. Projectile * projectile = new Projectile();
    15. projectile->setPos(x()+35,y());
    16. scene()->addItem(projectile);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    2. How to play add and play a simple .gif-animation. I use the following code (a sample) with no errors, but nothing appears in my window:
    Qt Code:
    1. QLabel *gif_anim = new QLabel();
    2. QMovie *movie = new QMovie("C:/Users/Technopia/Desktop/banana.gif"); //Everybody love bananas :cool:
    3. gif_anim->setMovie(movie);
    4. movie->start();
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Play a simple .gif-animation and KeyEvent-question :)

    Quote Originally Posted by Technopia View Post
    1. There is nothing wrong with the "moving code" I use. It does exacly what I tell it to do. I want to learn how to NOT make the player stop moving to the left or right when I click the spacebutton to shoot
    Then you probably don't want to move on key press, but as long as they is pressed.
    E.g. by starting a timer in key press, moving in the slot connected to the timer's timeout() signal, and stopping the timer in key released.

    Quote Originally Posted by Technopia View Post
    2. How to play add and play a simple .gif-animation. I use the following code (a sample) with no errors, but nothing appears in my window:
    Qt Code:
    1. QLabel *gif_anim = new QLabel();
    2. QMovie *movie = new QMovie("C:/Users/Technopia/Desktop/banana.gif"); //Everybody love bananas :cool:
    3. gif_anim->setMovie(movie);
    4. movie->start();
    To copy to clipboard, switch view to plain text mode 
    Do you show() the label?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Lightbulb Re: Play a simple .gif-animation and KeyEvent-question :)

    Thanks for reply I got the .gif-animation to show up. However in a new window. I am a beginner at this so I have difficult how the structures should be built. I am trying to show an animation if player reach a certain score, and I tried to implent the scoreAnimation function in my main score-function. However it gives me alot of errors. I do think im on right track tho and need a bit of push. Check #code comments for my thought.

    Score.h
    Qt Code:
    1. #ifndef SCORE_H
    2. #define SCORE_H
    3.  
    4. #include <QGraphicsTextItem>
    5. #include <QLabel> // I guess QLabel must be included here
    6. #include <QMovie> // I guess QMovie must be included here
    7.  
    8. class Score: public QGraphicsTextItem{ <-- should I add "public: QLabel" and "public: QMovie" here?
    9. public:
    10. Score(QGraphicsItem * parent=0);
    11. //Do I need to add -> Score(QLabel * label) here?
    12. //Do I need to add -> Score(QMovie * movie) here?
    13. void increase();
    14. int getScore();
    15. //int scoreAnimation(); <- This needs to be here I guess
    16. private:
    17. int score;
    18. };
    19.  
    20. #endif // SCORE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "score.h"
    2. #include <QFont>
    3. #include <QDebug>
    4. #include <QLabel>
    5. #include <QMovie>
    6. #include <QtGui>
    7.  
    8. Score::Score(QGraphicsItem *parent): QGraphicsTextItem(parent){
    9. score = 0;
    10.  
    11. setPlainText(QString("Score: ") + QString::number(score));
    12. setDefaultTextColor(Qt::yellow);
    13. setFont(QFont("times",16));
    14. }
    15.  
    16. void Score::increase(){
    17. score++;
    18. scoreAnimation(); // <--- Here I call another function
    19. setPlainText(QString("Score: ") + QString::number(score));
    20. }
    21.  
    22. int Score::getScore(){
    23. return score;
    24. }
    25.  
    26. int Score::scoreAnimation(){
    27. switch(score){
    28. case 1:
    29. /* This is the part I try to add an animation. I guess i need to set loopcount to one also. This code gives me alot of errors and im not sure how to structure it up :D
    30.   qDebug() << "10 Score to Gryffindor!";
    31.   QLabel* label = new QLabel();
    32.   QMovie *movie = new QMovie(":/images/C:/Users/Damien/Desktop/test.gif");
    33.   label->setMovie(movie);
    34.   movie->start();
    35.   label->show();
    36.   */
    37. break;
    38. default:
    39. break;
    40. }
    41. return 0;
    42. }
    To copy to clipboard, switch view to plain text mode 

    Thanks. An explanation would give me a great amount of understanding about how the structure works.
    Last edited by Technopia; 11th September 2015 at 15:01.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Play a simple .gif-animation and KeyEvent-question :)

    Ah, I see.

    So, the animation showed up in a separate window because the label you are using has no parent.
    A QWidget without a parent becomes a window by itself.

    From your new code I take it you want the animation inside a QGraphicsScene.
    For that you need a slightly different approach.

    Create your own animation item, by deriving from QObject and QGraphicsPixmapItem.
    Let it have a QMovie as a member or provide the QMovie via a setter and store the pointer to it as a member.

    In either case make the item show the movie's currentPixmap().

    Also connect to the movie's frameChanged() signal to a slot that calls update() on the item.

    Cheers,
    _

Similar Threads

  1. Replies: 11
    Last Post: 30th June 2015, 15:19
  2. Question about animation
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2010, 00:56
  3. A question about animation...
    By lar0che in forum Newbie
    Replies: 4
    Last Post: 8th August 2010, 06:22
  4. Simple Animation Help (Nevermind)
    By Pembar in forum Qt Programming
    Replies: 1
    Last Post: 19th May 2009, 13:33
  5. QT Animation Simple Pushbutton Help
    By Pembar in forum Qt Programming
    Replies: 4
    Last Post: 6th May 2009, 14:38

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.