c++ - Qt5: How to solve this trouble. Custom QWidget with custom paintEvent() vs. QWidget move() function -
first post here, need others.
i'm new @ qt/c++, years of hobby making programming things, i'm progressing relatively fast.
i'm trying learn making game, on, made, besides other things, custom sprite class inherits qwidget. reimplemented, common practice, paintevent function, using tricks, qpixmap/qpainter, json files, spritesheets, codehelpers made self, succefully play animation stances idle/walking/attacking.
all went perfect until tried move while walking function move(int x, int y) qwidget.
the problem, think, every time call "move" function updates/refresh/repaint qwidget calling paintevent function again, chopping animation, generating several issues, etc. etc.
i can't figure out how move without repainting or waiting animation finish, or i'm missing. seems there no other ways move qwidget. belive problem in event function that's being called emitted signal, moveevent, reimplemented keeping empty , result same.
do have idea? should remake in way skip trouble? or simple can't see coin opposite me?
ask code if needed. thank much, luna.
edit 1:
customsprite::customsprite(qwidget *parent, qstring spritename) : qwidget(parent) { // qpm qpixmap qpm_idle.load(":/assets/sprites/" + spritename + ".png"); qpm_walk.load(":/assets/sprites/" + spritename + "_walk.png"); ... // here goes custom class load json files each sprite sheet // json contains x,y,h,w info draw animation ... //some int,bools,qstrings ... // fps qtimer fps.setinterval(33); connect(&fps,signal(timeout()),this,slot(update())); fps.start(); } //the paintevent: void customsprite::paintevent(qpaintevent *){ // qpainter qp(this); qrect targetr(xmove,0,spritew,spriteh); qrect sourcer(jhv[status].getframex(jhv[status].namesv[spritefg]),0,spritew*sizedivisor,spriteh*sizedivisor); // things abouts consulting jsonhelper info switch (status){ // status know if should draw idle or walk animation case 0: qp.drawpixmap(targetr,qpm_idle,sourcer); break; case 1: xmove = xmove + 1; targetr = qrect(xmove,0,spritew,spriteh); qp.drawpixmap(targetr,qpm_walk,sourcer); break; default: break; } // works fine if(!reversinganimation && shouldanimate){ // simple bool options spritefg++; // int counter refering frame should drawed if(spritefg == jhv[status].getsssize()){ // consults json files custom jsonhelper, works fine if(reversible){ reversinganimation = true; spritefg = jhv[status].getsssize() - 1; }else if(!reversible){ spritefg = 0; emitsignals(); if(animateonce == true){ shouldanimate = false; } } } } //here goes similar above code to handle animation playing in reverse, nothing important }
then in generic qmainwindow add instance of customsprite class, set parent qmainwindow, appears, play animation. problem when try use:
//cs customsprite cs->move(cs->x()+1,cs->y());
it moves, interrumping animation several times. have tried putting "iswalking" bool filter inside paintevent, it's same.
edit 2: "xmove" thing there, trick used succefully , smoothly move animation, need increase width of qwidget being seen. not solution.
Comments
Post a Comment