We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Help please (".)
Page Index Toggle Pages: 1
Help please (".) (Read 634 times)
Help please (".)
Apr 25th, 2007, 11:00pm
 
Hi, I was wondering how I should go about creating an object(a circle or something for example) that appears when the mouse is clicked at a certain point, and then moves toward the mouse  position at the moment the mouse was clicked....
i hope someone understands what i mean...
Re: Help please (".)
Reply #1 - Apr 25th, 2007, 11:30pm
 
Hi Ray,

this is a very basic code that does what you've described.

Quote:
void setup(){
 size(640,480);
 frameRate(30);
 smooth();
}

void draw(){
 background(255,0,0);

}

void mouseDragged(){
fill(255);    
ellipse(mouseX,mouseY,30,30);
}
Re: Help please (".)
Reply #2 - Apr 26th, 2007, 4:40pm
 
Ah, i haven't explained clearly, sorry.
I want to have an object that is created at a fixed position (lets say at (0, 0) just for the sake of arguement) when the mouse button is clicked. I then want the object to travel in the direction of the mouse position at the time the object was created.
Basically, i'm creating a sort of gun-like projectile that is directed by the mouse position at the time it is called.
Re: Help please (".)
Reply #3 - Apr 26th, 2007, 4:49pm
 
What you need is a class you can create objects of.

e.g.
Code:
class bullet
{
float x,y;
float targetX,targetY;
bullet(float _x, float _y, float _targetX, float _targetY)
{
x=_x;
y=_y;
targetX=_targetX;
targetY=_targetY;
}

void move()
{
//change x and y by some ammount to get to target.
}

void draw()
{
ellipse(x,y,10,10);
}
}

bullet[] bullets;

void setup()
{
//normal setup stuff...
bullets=new bullet[0];
}

void draw()
{
for(int i=0;i<bullets.length;i++)
{
bullets[i].move();
bullets[i].draw();
}
}

void mousePressed
{
bullets=(bullet[])append(new bullet(0,0,mouseX,mouseY),bullets);
}
Re: Help please (".)
Reply #4 - Apr 26th, 2007, 8:31pm
 
Sounds like Missle Command!
Page Index Toggle Pages: 1