Getting Sprite to follow mouse, and other minor issues?
in
Programming Questions
•
2 months ago
Hi, I am making a Mario 64 sound board and would like your help.
I have a functioning display where you click one of the four buttons and a song plays. However, is it possible to have the background change to a picture with each change in song? I thought it would cool to have the Jolly Roger Bay background display when the Jolly Roger Bay song is selected, etc.
Would I have to make a new boolean for each picture, or would my previous booleans for the sound files also be able to put in the background pictures easily?
I want to have the text display longer when the correct keypressed() function is initiated. Right now it shows what letter is clicked, but it goes away too fast to read!
I thought maybe of making a little mario sprite, and I want to get him to follow my mouse, but maybe that is difficult?
Also, any ideas would be cool.. other ideas? Thanks!! I want you guys to listen to my sound files so I've attached my files in a zip!
(if it doesn't work or expires let me know!)
Or, just look at my sprite code..
AnimatedSprite mario;
class AnimatedSprite {
ArrayList<PImage> images;
int currentImage; // index of the image that will be displayed
int playRate; // speed at which to change currentImage
int imageSwitchTimer; // get a new image whenever this is 0
float x, y;
float dx, dy;
float angle;
float spinRate;
boolean visible;
boolean bouncing;
boolean boundingBoxVisible;
AnimatedSprite(int init_playRate) { // constructor
images = new ArrayList<PImage>();
playRate = init_playRate;
imageSwitchTimer = playRate;
currentImage = 0;
visible = false;
bouncing = false;
boundingBoxVisible = false;
}
void addImage(String fname) {
PImage img = loadImage(fname);
images.add(img);
}
void render() {
PImage img = images.get(currentImage);
if (visible) {
pushMatrix();
x = mouseX;
y = mouseY;
// move and rotate the coordinate system
// translate(x + img.height / 2, y + img.height / 2);
// rotate(angle);
// draw the image
image(img, -img.width / 2, -img.height / 2);
popMatrix();
}
// if (boundingBoxVisible) {
// pushStyle();
// noFill();
// stroke(255, 0, 0);
// rect(x, y, img.width, img.height);
// popStyle();
// }
}
void update() {
x = mouseX;
y = mouseY;
//x += dx;
//y += dy;
angle += spinRate;
if (bouncing) checkEdgeCollisions();
// change the image if necessary
--imageSwitchTimer;
if (imageSwitchTimer <= 0) {
imageSwitchTimer = playRate;
currentImage += 1;
if (currentImage >= images.size()) { // wrap-around to first image if necessary
currentImage = 0;
}
}
}
void checkEdgeCollisions() {
if (y < 0) dy *= -1; // hit the top edge?
if (y >= height) dy *= -1; // hit the bottom edge?
if (x < 0) dx *= -1; // hit the left edge?
if (x >= width) dx *= -1; // hit the right edge?
}
// Returns true when point (a, b) is inside this sprite's
// bounding box, and false otherwise.
boolean pointInBoundingBox(float a, float b) {
PImage img = images.get(currentImage);
if (a > x && a < x + img.width && b > y && b < y + img.height)
return true;
else
return false;
}
} // class AnimatedSprite
void setup() {
size(500,500);
background(0);
// initialize the mario sprite
mario = new AnimatedSprite(10);
mario.x = mouseX;
mario.y = mouseY;
mario.dx = 2;
mario.visible = true;
mario.bouncing = true;
mario.addImage("marioWalk1.png");
mario.addImage("marioWalk2.png");
mario.addImage("marioWalk3.png");
mario.addImage("marioWalk4.png");
mario.addImage("marioWalk5.png");
mario.addImage("marioWalk6.png");
mario.addImage("marioWalk7.png");
mario.addImage("marioWalk8.png");
}
void draw() {
background(0);
mario.render();
mario.update();
}
1