如何让鼠标像操纵杆一样移动。这是我的意思的一个例子,除了这只是向右移动,当鼠标一直向右移动时它停止。如果鼠标位于中心并且移动到鼠标所在的位置,我希望圆圈停止。
float ballX = 0; // need to keep track of the ball's current position
float ballY = 150;
void setup() {
size(300,300); // standard size
}
void draw() {
background(200); // dull background
float speed = 2.0 * ( mouseX / (width*1.0) );
println("speed is " + speed); // print just to check
ballX = ballX + speed; // adjust position for current movement
fill(255,0,0);
ellipse(ballX, ballY, 20,20);
}
答案 0 :(得分:0)
您想要检查窗口中心的mouseX位置,即宽度/ 2。
要查找鼠标的相对位置,只需从鼠标位置减去该中心位置即可。这样当鼠标位于中心的左侧时,该值为负值,并且您的球将向左移动。
您可能还想将球保持在屏幕上。
float ballX = 0; // need to keep track of the ball's current position
float ballY = 150;
void setup() {
size(300,300); // standard size
}
void draw() {
float centerX = width/2;
float maxDeltaMouseX = width/2;
float deltaMouseX = mouseX-centerX;
float speedX = deltaMouseX/maxDeltaMouseX;
background(200); // dull background
println("speed is " + speedX); // print just to check
ballX = ballX + speedX; // adjust position for current movement
//keep ball on screen
if(ballX < 0){
ballX = 0;
}
else if(ballX > width){
ballX = width;
}
fill(255,0,0);
ellipse(ballX, ballY, 20,20);
}