2015-02-09 60 views
0

如何让鼠标像操纵杆一样移动。这里是我的意思的一个例子,除了这只是向右移动,并且当鼠标完全停在右边时。如果鼠标位于中心位置并移动到鼠标所在的位置,我想让圆圈停止。处理游戏杆初学者


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

你想检查对窗口的中心,这是宽度/ 2 mouseX位置。

要找到鼠标的相对位置,只需从鼠标位置中减去该中心位置即可。这样,当鼠标位于中间的左侧时,该值为负数,并且球会移动到左侧。

你可能也想保持你的球在屏幕上。

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); 
}