We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Everyone, I am Mel! I have very little experience with processing, but I have an art project that I've created. My goal is to have 4 shapes bouncing around the screen, and when you click one, a sound will play. So far, I have figured out how to create the shapes, make them move around. However, my first problem is that only the flower image I have stays mostly within the screen. All other shapes are far off and barely show up in one rotation. I understand that this is similar to "bouncing ball" exercises, yet I'm still lost on how to keep the four of my shapes with the boundary.
This below is what I have so far. Sorry that you wont be able to play the code, but hopefully I was detailed enough for you to get the picture.Thank you for reading this and any help is much appreciated!!!
import ddf.minim.*;
Minim minim;
AudioPlayer winter;
AudioPlayer summer;
AudioPlayer spring;
AudioPlayer autumn;
PImage flower;
PImage fire;
PImage sun;
PImage snowman;
float xpos, ypos; // Starting position of shape
float xspeed = 5; // Speed of the shape
float yspeed = 10; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
import processing.sound.*;
void setup() {
size(800, 800);
noStroke();
frameRate(50);
flower = loadImage("Flower.png");
flower.resize(200, 200);
sun = loadImage("Sun.png");
sun.resize(200, 200);
fire = loadImage("campfire.png");
fire.resize(200, 200);
snowman = loadImage("snowman.png");
snowman.resize(200, 200);
xpos = width/1.8;
ypos = height/2;
// will play sound
minim = new Minim(this);
winter = minim.loadFile("Winter Mix.wav");
summer = minim.loadFile("Summer Mix.wav");
spring = minim.loadFile("Spring Mix.wav");
autumn = minim.loadFile("Autumn Mix.wav");
}
void draw() {
background(#CBE9F0);
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width || xpos < 5) {
xdirection *= -1;
}
if (ypos > height || ypos < 10) {
ydirection *= -1;
}
// Draw the shape
image(flower, xpos, ypos);
image(sun, -xpos, ypos);
image(fire, xpos, -ypos);
image(snowman, -xpos, -ypos);
}
void keyPressed()
{
if (winter.isPlaying() )
{
winter.pause();
} else if (winter.position() == winter.length() )
{
winter.rewind();
winter.play();
}
summer.rewind();
summer.play();
}
void mousePressed()
{
spring.rewind();
}
Answers
Working code is always better. Not only to see the problem but to move to a solution. Here I modified your code:
Now, the problem here is that you manage all four images using a single set of coordinates. Doing it the way you are doing it, it will always give you this behavior. If you want to change it, then you will need to provide different coordinates to each object. Do you still want the objects to leave the screen or do you want to constrict them within the sketch area?
This tutorial will come handy as the solution will be using classes. Not difficult but reviewing the concepts will help: https://processing.org/tutorials/objects/
Kf
Kf, thank you for making it accessible for others.
Yes, I would like for the objects to stay within the sketch. So are you saying that I need to make :
different for every image?
Something like this.
Kf
Hey, Kf!
Thank you again for your help. I have been replicating your format for the classes with my own images, but I am not sure why they aren't working. The Errors listed will say "Return Type for method is Missing" or "NullPointer Exception"
Also, I need the Pimage part and the void update in my code for the the sketch to run?
You shouldn't have a Sun() constructor inside a Flower class.
Hi, TFGuy44 and thanks for the reply.
Before I added the Sun() constructor, i tried to test it out with only the Flower, and the sketch still didn't run.
and now Ive tried to make more of the same Flowers() with variations and it has not worked.
We're at a total loss when it comes to seeing where you're at now. Post your code again.
Hello! I kept trying to edit what kfrager posted as an example, with replacements of the images I needed. I am thankful for the link to the lesson on classes. However, I do not know how to replace the rectangles with my images, now that the objects stay within the perimeter of the canvas. I see that adding :
makes for 4 shapes to bounce around. But, is there is a way I should put PImage there instead so that my images bounce?
Thanks!
Please add a new constructor to MyImage class:
Then you need to do two things. In setup:
Now in draw, you need:
Kf
Thank you so much! I will try it out!