2015-02-09 79 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); 
}