i need help on my assignment with moving an object using parameters in processing

2264 views processing
3

I'm new to coding and I am trying to write a program that shows a car moving. I can't figure out how to move the car back and forth using parameters. Any help would be appreciated.

void setup()
{
size(500,500);
rectMode(CORNER);
ellipseMode(CORNER);

}

void draw()
{
  background(50,200,255);
  drawCar(80,340);
  drawWheel(45,410);
}

void drawCar(int x, int y)
{
  noStroke();
  fill(255,0,0);
  beginShape();
  vertex(x,y);
  vertex(x+50,y);
  vertex(x+80,y+50);
  vertex(x+110,y+50);
  vertex(x+110,y+80);
  vertex(x-60,y+80);
  vertex(x-60,y+50);
  vertex(x-30,y+50);
  endShape(CLOSE);
}

void drawWheel(int wx,int wy)
{
  fill(0);
  noStroke();
  ellipse(wx,wy,40,40);
  ellipse(wx+85,wy,40,40);
}

answered question

1 Answer

9

It's hard to help with broad "how do I do this" type questions, but I'll try to help in a general sense.

You have the code that draws your car here:

drawCar(80, 340);
drawWheel(45, 410);

Those numbers control where the car shows up. Right now you're always passing in the same values, so the car always shows up in the same place.

What happens if you pass in different values? Try something like this:

drawCar(mouseX, mouseY);
drawWheel(mouseY, mouseX);

This should show your car wherever your mouse is.

Now, if you want to show the care moving around on its own, you probably want to store your state in a set of variables, and then change those variables over time.

Shameless self-promotion: here is a tutorial on animation in Processing.

posted this

Have an answer?

JD

Please login first before posting an answer.