TQCanvasSprite Class

Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# TQCanvasSprite Class Reference
[canvas module]

The TQCanvasSprite class provides an animated canvas item on a TQCanvas. More...

`#include <tqcanvas.h>`

Inherits TQCanvasItem.

List of all member functions.

## Public Members

- TQCanvasSprite ( TQCanvasPixmapArray * a, TQCanvas * canvas )
- void setSequence ( TQCanvasPixmapArray * a )
- virtual ~TQCanvasSprite ()
- virtual void move ( double nx, double ny, int nf )
- void setFrame ( int f )
- enum FrameAnimationType { Cycle, Oscillate }
- virtual void setFrameAnimation ( FrameAnimationType type = Cycle, int step = 1, int state = 0 )
- int frame () const
- int frameCount () const
- virtual int rtti () const
- virtual TQRect boundingRect () const
- int width () const
- int height () const
- int leftEdge () const
- int topEdge () const
- int rightEdge () const
- int bottomEdge () const
- int leftEdge ( int nx ) const
- int topEdge ( int ny ) const
- int rightEdge ( int nx ) const
- int bottomEdge ( int ny ) const
- TQCanvasPixmap * image () const
- virtual TQCanvasPixmap * imageAdvanced () const
- TQCanvasPixmap * image ( int f ) const
- virtual void advance ( int phase )
- virtual void draw ( TQPainter & painter )

## Detailed Description

The TQCanvasSprite class provides an animated canvas item on a TQCanvas.

A canvas sprite is an object which can contain any number of images (referred to as frames), only one of which is current, i.e. displayed, at any one time. The images can be passed in the constructor or set or changed later with setSequence(). If you subclass TQCanvasSprite you can change the frame that is displayed periodically, e.g. whenever TQCanvasItem::advance(1) is called to create the effect of animation.

The current frame can be set with setFrame() or with move(). The number of frames available is given by frameCount(). The bounding rectangle of the current frame is returned by boundingRect().

The current frame's image can be retrieved with image(); use imageAdvanced() to retrieve the image for the frame that will be shown after advance(1) is called. Use the image() overload passing it an integer index to retrieve a particular image from the list of frames.

Use width() and height() to retrieve the dimensions of the current frame.

Use leftEdge() and rightEdge() to retrieve the current frame's left-hand and right-hand x-coordinates respectively. Use bottomEdge() and topEdge() to retrieve the current frame's bottom and top y-coordinates respectively. These functions have an overload which will accept an integer frame number to retrieve the coordinates of a particular frame.

TQCanvasSprite draws very quickly, at the expense of memory.

The current frame's image can be drawn on a painter with draw().

Like any other canvas item, canvas sprites can be moved with move() which sets the x and y coordinates and the frame number, as well as with TQCanvasItem::move() and TQCanvasItem::moveBy(), or by setting coordinates with TQCanvasItem::setX(), TQCanvasItem::setY() and TQCanvasItem::setZ().

See also Graphics Classes and Image Processing Classes.

## Member Type Documentation

### TQCanvasSprite::FrameAnimationType

This enum is used to identify the different types of frame animation offered by TQCanvasSprite.

- `TQCanvasSprite::Cycle` - at each advance the frame number will be incremented by 1 (modulo the frame count).
- `TQCanvasSprite::Oscillate` - at each advance the frame number will be incremented by 1 up to the frame count then decremented to by 1 to 0, repeating this sequence forever.

## Member Function Documentation

### TQCanvasSprite::TQCanvasSprite ( TQCanvasPixmapArray * a, TQCanvas * canvas )

Constructs a TQCanvasSprite which uses images from the TQCanvasPixmapArray a.

The sprite in initially positioned at (0, 0) on canvas, using frame 0.

### TQCanvasSprite::~TQCanvasSprite ()`[virtual]`

Destroys the sprite and removes it from the canvas. Does not delete the images.

### void TQCanvasSprite::advance ( int phase )`[virtual]`

Extends the default TQCanvasItem implementation to provide the functionality of setFrameAnimation().

The phase is 0 or 1: see TQCanvasItem::advance() for details.

See also TQCanvasItem::advance() and setVelocity().

Example: canvas/canvas.cpp.

Reimplemented from TQCanvasItem.

### int TQCanvasSprite::bottomEdge () const

Returns the y-coordinate of the current bottom edge of the sprite. (This may change as the sprite animates since different frames may have different bottom edges.)

See also leftEdge(), rightEdge(), and topEdge().

### int TQCanvasSprite::bottomEdge ( int ny ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns what the y-coordinate of the top edge of the sprite would be if the sprite (actually its hotspot) were moved to y-position ny.

See also leftEdge(), rightEdge(), and topEdge().

### TQRect TQCanvasSprite::boundingRect () const`[virtual]`

Returns the bounding rectangle for the image in the sprite's current frame. This assumes that the images are tightly cropped (i.e. do not have transparent pixels all along a side).

Reimplemented from TQCanvasItem.

### void TQCanvasSprite::draw ( TQPainter & painter )`[virtual]`

Draws the current frame's image at the sprite's current position on painter painter.

Reimplemented from TQCanvasItem.

### int TQCanvasSprite::frame () const

Returns the index of the current animation frame in the TQCanvasSprite's TQCanvasPixmapArray.

See also setFrame() and move().

### int TQCanvasSprite::frameCount () const

Returns the number of frames in the TQCanvasSprite's TQCanvasPixmapArray.

### int TQCanvasSprite::height () const

The height of the sprite for the current frame's image.

See also frame().

### TQCanvasPixmap * TQCanvasSprite::image () const

Returns the current frame's image.

See also frame() and setFrame().

### TQCanvasPixmap * TQCanvasSprite::image ( int f ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns the image for frame f. Does not do any bounds checking on f.

### TQCanvasPixmap * TQCanvasSprite::imageAdvanced () const`[virtual]`

Returns the image the sprite will have after advance(1) is called. By default this is the same as image().

### int TQCanvasSprite::leftEdge () const

Returns the x-coordinate of the current left edge of the sprite. (This may change as the sprite animates since different frames may have different left edges.)

See also rightEdge(), bottomEdge(), and topEdge().

### int TQCanvasSprite::leftEdge ( int nx ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns what the x-coordinate of the left edge of the sprite would be if the sprite (actually its hotspot) were moved to x-position nx.

See also rightEdge(), bottomEdge(), and topEdge().

### void TQCanvasSprite::move ( double nx, double ny, int nf )`[virtual]`

Set the position of the sprite to nx, ny and the current frame to nf. nf will be ignored if it is larger than frameCount() or smaller than 0.

### int TQCanvasSprite::rightEdge () const

Returns the x-coordinate of the current right edge of the sprite. (This may change as the sprite animates since different frames may have different right edges.)

See also leftEdge(), bottomEdge(), and topEdge().

### int TQCanvasSprite::rightEdge ( int nx ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns what the x-coordinate of the right edge of the sprite would be if the sprite (actually its hotspot) were moved to x-position nx.

See also leftEdge(), bottomEdge(), and topEdge().

### int TQCanvasSprite::rtti () const`[virtual]`

Returns 1 (TQCanvasItem::Rtti_Sprite).

See also TQCanvasItem::rtti().

Example: canvas/canvas.cpp.

Reimplemented from TQCanvasItem.

### void TQCanvasSprite::setFrame ( int f )

Sets the animation frame used for displaying the sprite to f, an index into the TQCanvasSprite's TQCanvasPixmapArray. The call will be ignored if f is larger than frameCount() or smaller than 0.

See also frame() and move().

### void TQCanvasSprite::setFrameAnimation ( FrameAnimationType type = Cycle, int step = 1, int state = 0 )`[virtual]`

Sets the animation characteristics for the sprite.

For type == Cycle, the frames will increase by step at each advance, modulo the frameCount().

For type == Oscillate, the frames will increase by step at each advance, up to the frameCount(), then decrease by step back to 0, repeating forever.

The state parameter is for internal use.

### void TQCanvasSprite::setSequence ( TQCanvasPixmapArray * a )

Set the array of images used for displaying the sprite to the TQCanvasPixmapArray a.

If the current frame() is larger than the number of images in a, the current frame will be reset to 0.

### int TQCanvasSprite::topEdge () const

Returns the y-coordinate of the top edge of the sprite. (This may change as the sprite animates since different frames may have different top edges.)

See also leftEdge(), rightEdge(), and bottomEdge().

### int TQCanvasSprite::topEdge ( int ny ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns what the y-coordinate of the top edge of the sprite would be if the sprite (actually its hotspot) were moved to y-position ny.

See also leftEdge(), rightEdge(), and bottomEdge().

### int TQCanvasSprite::width () const

The width of the sprite for the current frame's image.

See also frame().

This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.