PDA

View Full Version : What is the best way to create my own animated canvas item ?



yellowmat
14th July 2006, 19:43
Hi everybody,

I need to play animation, just as QCanvasSprite does, but the way this class works does not feet to my need.

I need to play the animation foreward and backward and the animation must stop once the last item (if playing foreward) or the first item (if playing backward) has been drawn.

So here are my questions :
* should I subclass the QCanvasSprite to respond to my need ?
* is it possible to subclass the QCanvasSprite class ?if it is, how must I do ?
* if it is not possible to subclass does someone could tell me about its experience ?

Thanks in advance for your help.

wysota
16th July 2006, 09:46
I think you should subclass QCanvasSprite and reimplement advance(). In your reimplementation you should check if you are currently on the last/first frame and if not, increase/decrease the frame number. Later you must call the base implementation from QCanvasItem to do the rest of the job (don't call the implementation from QCanvasSprite!).

I don't think default canvas sprites provide backward animation, so you have to implement that yourself, for example by reimplementing setFrameAnimation() or by defining your own method.

yellowmat
17th July 2006, 17:43
ok, I tryed with the following code



void CMySprite::advance(int phase)
{
if( frame() != 0 )
setFrame(frame() - 1);

QCanvasItem::advance(phase);
}


... it works backward and stops when the first image is reached.

So I would like to reimplement setFrameAnimation() ... but how is it possible to modify the enum FrameAnimationType in order to add my own animation types ? Should I declare my own enum with cycle, oscillation and my own values or is there another way to do it ?

About advance(), I also tryed without calling the base class method and it works as well, is it really necessary to call the base class method ?

thanks in advance

wysota
17th July 2006, 17:57
So I would like to reimplement setFrameAnimation() ... but how is it possible to modify the enum FrameAnimationType in order to add my own animation types ? Should I declare my own enum with cycle, oscillation and my own values or is there another way to do it ?
Maybe you could use one of existing types? If not, you can always add a new method to the class.


About advance(), I also tryed without calling the base class method and it works as well, is it really necessary to call the base class method ?
The base class implementation moves the object. If your object is not to be moved automatically, you can skip the base call, but there is nothing wrong in making it, so I'd leave it as it is.