Homework 3: Set Your Sprite in Motion
Until now, our sprites haven't moved, but our animation will be more interesting if the sprite can move around the screen. In this step, we'll add the pieces needed to get things moving!
Sprite
s will get a flag saying whether they should scroll (move one column to the right on every “redraw”). On every “redraw”, the Asciimation
object will ask the Sprite
whether it should be scrolling and, if so, tell the Sprite
to move one column to the right.
If a sprite moves off the right edge of the screen, it should wrap back around to the other side of the screen. In particular, when its topLeftColumn
reaches the right-most side of the movie screen, it should reposition itself just off the left side of the asciimation frame, so that in the next update, its rightmost column will appear on the left side of the movie frame.
Your Task
Add scrolling functionality to the Sprite
and Asciimation
classes:
-
In the
Sprite
class, add- A
shouldScroll_
private data member (boolean) that stores whether the sprite should be animated. - A
setScrolling(bool flag)
member function that sets the value ofshouldScroll_
. - A
getScrolling()
member function that returns the current value ofshouldScroll_
. - A
moveRight(int maxWidth)
member function that changes theSprite
's location one spot to the right and handles wrapping.
- A
-
In the
Asciimation
class, modify- The
updateContents
member function to check if theSprite
is scrolling and, if so, move it one step to the right before copying its character array.
- The
-
In your constructor, turn on scrolling for the sprite.
Helpful Hints
Here are some tips for implementing sprite motion!
Wrapping Around the Screen
When working on the sprite wrapping, drawing a picture may help you sort out how to make that happen with maxWidth
(which should be passed as the MOVIE_WIDTH
value from the Asciimation
class) and SPRITE_WIDTH
.
The sprite should disappear completely off the right edge before reappearing on the left. When topLeftColumn_
equals maxWidth
, the sprite is completely off-screen to the right. At this point, you should reset it to -SPRITE_WIDTH
so it starts completely off-screen to the left.
Testing Motion
After implementing these changes,
- Compile and run your program.
- You should see the sprite scrolling smoothly from left to right.
- When it goes off the right edge, it should reappear on the left.
Smooth Animation
The play()
function includes a small delay between frames to create smooth animation. If the sprite seems to be moving too fast or too slow, you can adjust the delay value in the play()
function.
(When logged in, completion status appears here.)