Sprite sheet link https://i.pinimg.com/736x/9c/1b/ce/9c1bce8dbd9a3f8b8a6d09aa7d7
ID: 3591180 • Letter: S
Question
Sprite sheet link
https://i.pinimg.com/736x/9c/1b/ce/9c1bce8dbd9a3f8b8a6d09aa7d79e3ae--game-creator-game-resources.jpg sprite link
--Spriteanimation.pde---
PImage spritesheet = loadImage("http://www.nordenfelt-thegame.com/blog/wp-content/uploads/2011/11/explosion_transparent.png");
int DIM = 5;
int W = spritesheet.width/DIM;
int H = spritesheet.height/DIM;
void setup() {
size(300, 300);
imageMode(CENTER); //use the center of the image to represent the image location
noCursor(); // hide the mouse cursor
frameRate(24);
}
void draw() {
background(0);
int x = frameCount%DIM * W;
int y = frameCount/DIM%DIM * H;
PImage sprite = spritesheet.get(x, y, W, H);
image(sprite, mouseX, mouseY);
}
--Movingdog--
int x=0, y = 0; //the initial position to display the dog
PImage img, dog;
void setup()
{
size (500, 350);
img = loadImage ("bricks.jpg"); //load the background image
dog = loadImage ("dog.png"); //load the dog image
}
void draw()
{
int dogW = dog.width/5; //the rescaled dog width
int dogH = dog.height/5; //the rescaled dog height
background(img); //set the background
if (key == CODED)
{
if (keyCode == UP && keyPressed == true)
{
y -= 1;
if( y < 0 )
y = 0;
}
else if (keyCode == DOWN && keyPressed == true)
{
y += 1;
if(y > height - dogH)
y = height - dogH;
}
else if (keyCode == LEFT && keyPressed == true)
{
x -= 1;
if(x < 0)
x = 0;
}
else if (keyCode == RIGHT && keyPressed == true)
{
x += 1;
if(x > width - dogW)
x = width - dogW;
}
}
image(dog, x, y, dogW, dogH); //display the dog image at the position (x,y); x and y are controlled by pressed keys
}
----
How would you kind of combine these two to create what #2 or #3 is asking for. Thanks! (Using processing)
Explanation / Answer
the code so far is correct
package com.akrillix.client;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheetLoader {
BufferedImage spriteSheet = ImageIO.read(new File("src/spriteSheet.png"));
int width;
int height;
int rows;
int columns;
BufferedImage[] sprites = new BufferedImage[rows * columns];
public SpriteSheetLoader(int width, int height, int rows, int columns) throws IOException {
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width, j * height, width, height);
}
}
}
public void paint(Graphics g) {
//g.drawImage(sprites[1], 100, 100, null);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.