私は、ボールが生成された場所にプログラムを作成しています。マウスをクリックしてドラッグするとスローすることができます。私はボタンをクリックして壁を追加できる機能を追加することに決めました。壁はマウスの位置に移動されます。Running mouseReleased()特定の条件でのみ
void setup() {
size(640,360);
background(50);
}
// Speed of ball
float xSpeed = 5;
float ySpeed = 1;
// Ball size
float circleRad = 12;
// Ball position
float circleX = 0 + circleRad;
float circleY = 180;
// Ball physics
float gravity = 0.35;
float friction = 0.075;
// Wall position
float wallX = 320;
float wallY = 180;
// Wall dimensions
float wallWidth = 20;
float wallHeight = height;
// Determines whether or not the wall's position is currently being changed
boolean wall = false;
void draw() {
background(50);
noStroke();
fill(85);
rect(25,25,50,50);
ellipseMode(RADIUS);
rectMode(CENTER);
// If button is pressed, wall is being created/moved
if(mousePressed && mouseX <= 50 && mouseY <= 50) {
wall = true;
}
// If mouse is pressed and wall isnt being created/moved, move circle to mouse's position
if (mousePressed && wall == false) {
circleX = mouseX;
circleY = mouseY;
}
// Creates/moves wall
if(wall == true){
wallX = mouseX;
fill(170);
rect(wallX, wallY, wallWidth, wallHeight);
}
// Creates ball
fill(255);
ellipse(circleX,circleY,circleRad,circleRad);
// Bounces ball off of walls/ceiling/floor, implements ball physics
if (!mousePressed){
circleX+=xSpeed;
circleY+=ySpeed;
if(circleX >= width - circleRad) {
xSpeed *= -1;
circleX = width - circleRad;
}
if (circleX <= 0 + circleRad) {
xSpeed *= -1;
circleX = 0 + circleRad;
}
if (circleY >= height - circleRad) {
ySpeed *= -1;
ySpeed += 0.9;
circleY = height - circleRad;
}
if (circleY <= 0 + circleRad) {
ySpeed *= -1;
circleY = 0 + circleRad;
}
if((circleY <= 0 + circleRad || circleY >= height - circleRad) && xSpeed >= 0) {
xSpeed -= friction;
}
if((circleY <= 0 + circleRad || circleY >= height - circleRad) && xSpeed <= 0){
xSpeed -= friction * -1;
}
if(xSpeed >= -0.1 && xSpeed <= 0.1) {
xSpeed = 0;
}
if(xSpeed <= 0){
xSpeed -= friction * -0.1;
}
if(xSpeed >= 0){
xSpeed -= friction * 0.1;
}
ySpeed += gravity;
}
}
// Sets ball speed to speed of mouse once mouse is released, allowing for throwing of ball
void mouseReleased(){
xSpeed = mouseX - pmouseX;
ySpeed = mouseY - pmouseY;
}
私はまだ、この機能を完了していない(ボールがまだ壁の跳ね返るせず、マウスを次の中から壁を停止する方法はありません)が、私はこの問題に遭遇してきました。壁を作成するためにボタンをクリックするたびに、プログラムはマウスをリリースしたことを確認し、ボールを投げることを決めます。通常、誰かがクリックするとマウスを動かさないので、ボールのスピードは0に設定されます。ifステートメントのmouseReleased()関数の中でコードを折り返して解決しようとしました。
しかし、これは別の問題を引き起こします。マウスが押し下げられている間、ボールは空中で一時停止し、マウスが離されると再開する。ボタンを押したり、壁を作り始めたり、ボールが完全に影響を受けないようにするにはどうすればいいですか?
あなたは[mcve]を投稿できますか? – anacron
私が投稿したコードのほとんどは解決策を見つける上で重要だと考えています。解決策を見つけるために必要なコードをすべて掲載しました。回答者が処理をしている限り、問題の再現のためにコードを検証できます。タグの1つ)。 – Freguin
@anacronこれは実際に完全な例です。これは[タグ:処理]の質問と[Processing!= Java](http://meta.stackoverflow.com/questions/321127/processing-java)です。つまり、これは実際には**最小**の例ではありません。あなたはここに余分なコードがたくさんあります。なぜなら、あなたはそのロジックを考えるのに苦労している理由の一部です。実行可能な状態のまま、できるだけ少数の行に問題を絞り込む必要があります。 –