Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

I can't figure out the speed for this, how to make it move? This is done in processing in java language. move() This function moves the balloon as specified in the x and y speed instance variables. If the balloon would move off the screen, it reverses direction. The balloon may not leave the window; the string may. Also this is what I am suppose to get, http://evc-cit.info/cit020/objects.html

OpenStudy (anonymous):

Balloon balloon1; Balloon balloon2; void setup() { size(300, 300); balloon1 = new Balloon(50, 60, 20, 30, color(255, 0, 0)); balloon2 = new Balloon(70, 100, 30, 40, color(0, 255, 255)); } void draw() { background(255); balloon1.move(); balloon1.display(); balloon2.move(); balloon2.display(); } class Balloon { float x; float y; float w; float h; color c; float xSpeed; float ySpeed; Balloon(float bx, float by, float bw, float bh, color bc) { x = bx; y = by; w = bw; h = bh; c = bc; xSpeed = 1; ySpeed = 2; } void display() { fill(c); ellipse(x, y, w, h); } void move() { // do stuff here } }

OpenStudy (anonymous):

This is what I attempted so far but its not quite working. void move() { x += xSpeed; y += ySpeed; if (x >= width - w/2) { xSpeed = xSpeed * -1; if (x >= 0+w/2) { xSpeed = xSpeed * 1; }

OpenStudy (konradzuse):

you need to do paint/paintComponent and then repaint to make it move. It just wont move by itself.. If you're doing a gui that is.

OpenStudy (konradzuse):

yes this uses a gui.

OpenStudy (anonymous):

I am not sure what you mean by paint or gui?

OpenStudy (konradzuse):

do you see how it moves on th screen? That's a gui Graphical user Interface.

OpenStudy (konradzuse):

What is the problem you are facing?

OpenStudy (anonymous):

Well I am trying to make the balloon like in the link, but mine do not stay in the window. The balloons are not suppose to leave the window but mine do. I am trying to figure how to make them stay inside the window.

OpenStudy (konradzuse):

so they do move then? You need to use collision detection.

OpenStudy (konradzuse):

How do they move without you painting?

OpenStudy (konradzuse):

draw?

OpenStudy (anonymous):

mine just start from the top and leave the window

OpenStudy (anonymous):

Is that what you are trying to ask?

OpenStudy (konradzuse):

yes. But what does your draw method look liek?

OpenStudy (anonymous):

Um...I actually got it to stay in the window, here is the updated code. void move() { x += xSpeed; y += ySpeed; // if (x < 0 || x > width) xSpeed *= -1; if (y < 0 || y > height) ySpeed *= -1; } }

OpenStudy (anonymous):

I just need to make it go faster and add the string which I need to figure out; display() This function draws the balloon at its current position. It will also draw a a string at the bottom of the balloon (using a black line()). The string starts at the center of the balloon at the bottom, and extends to the right one-third the width of the balloon and the line’s height is one-third the height of the balloon. The string is outside the balloon’s bounding box.

OpenStudy (konradzuse):

I'm not sure why your code even displays anything moving. The speed is supposed to be random... I need to go to bed though, goo dluck I'll see hjow you are in the morning.

OpenStudy (anonymous):

ok that's fine

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!