Premium Features
Let's transform our red square into an animated character using sprite sheets.
A sprite sheet is a single image containing multiple frames of animation laid out in a grid. Each frame shows the character in a slightly different pose.
Download the warrior assets from the course resources. Each animation state (idle, run, jump, fall) has its own sprite sheet:
Idle — Character standing still, breathing animation
Run — Running cycle
Jump — Ascending pose
Fall — Descending pose
Use c.drawImage() with crop parameters to display one frame at a time:
c.drawImage( this.image, this.currentFrame * this.width, // Crop X 0, // Crop Y this.width, // Crop width this.height, // Crop height this.position.x, this.position.y, this.width, this.height )Increment currentFrame over time to cycle through the animation.
Control animation speed with a frame delay counter — don't advance every single frame or it'll play too fast.
Comments
Want to participate?
Create a free Chris Courses account to begin
No comments yet, be the first to add one