MousePressed 和 mouseReleased 创建对象
MousePressed and mouseReleased to create objects
我有一个创建球对象的球 class。我想在按下鼠标时创建一个球对象,如果按下并按住鼠标,球应该跟随鼠标(按下鼠标时)。当鼠标松开时,小球应该停留在当前的mouseX和mouseY,不再跟随鼠标。
再次按下鼠标时,将创建另一个球对象,并执行与第一个对象相同的操作,依此类推...每次按下鼠标都会创建一个新的球对象。
我有一些创建对象的代码,它跟随鼠标并在我释放鼠标时放置 canvas。但是如果我再次按下我的鼠标,原来的球会回到鼠标。我如何“脱离”球对象,以便我可以制作更多不影响先前放置的球的位置的球对象?
我打算用 ArrayLists 做很多对象。
class Ball {
float xPos, yPos;
Ball(float xPos, float yPos) {
this.xPos= xPos;
this. yPos= yPos;
}
void drawBall() {
ellipse(xPos, yPos, 50, 50);
println("X" + xPos + " Y:"+ yPos);
}
}
Ball ball1;
Ball ball2;
ArrayList<Ball> ballList = new ArrayList<Ball>();
void setup() {
size(600, 600);
ball1=new Ball(900, 900);
ball2=new Ball(900, 900);
}
void draw() {
background(150);
if (mousePressed ) {
ball1.xPos= mouseX;
ball1.yPos=mouseY;
}
ball1.drawBall();
}
这里的问题是您根本没有管理数组,而是一直在处理同一个对象。我使用数组列表重写了您的示例来存储 Ball 对象,并在用户单击鼠标按钮时创建新的球。
这里请注意,我故意使用“添加到数组列表的最后一项”而不是创建一个引用 this 对象的对象。这是因为我不想让您与参考资料等混淆。
ArrayList<Ball> ballList = new ArrayList<Ball>();
void setup() {
size(600, 600);
}
void draw() {
background(150);
// if the mouse button is held down, set the ball's coordinates to the mouse coordinates
if (ballList.size() > 0 && mousePressed) {
ballList.get(ballList.size() - 1).xPos = mouseX; // 'ballList.get(ballList.size() - 1)' is the java way to get the last item added to an arrayList
ballList.get(ballList.size() - 1).yPos = mouseY;
}
// draw every ball in the array (the weird format is just shorthand for a "for each" loop)
for(Ball b : ballList) {
b.drawBall();
}
}
// this method will trigger once every time the user press a mouse button
void mousePressed() {
ballList.add(new Ball(mouseX, mouseY));
}
class Ball {
float xPos, yPos;
Ball(float xPos, float yPos) {
this.xPos= xPos;
this.yPos= yPos;
}
void drawBall() {
ellipse(xPos, yPos, 50, 50);
}
}
通过定位最后创建的球而不是特定的球,您可以确保始终使用正确的对象。另外,请注意我是如何让球对象自己画成一个循环的。 'for each' 形式确保无论 arrayList 中当前有多少个球,它们都会被打印出来。
祝这个项目好运,如果我的回答有什么地方令人困惑,请随时联系我并提出问题。
玩得开心!
我有一个创建球对象的球 class。我想在按下鼠标时创建一个球对象,如果按下并按住鼠标,球应该跟随鼠标(按下鼠标时)。当鼠标松开时,小球应该停留在当前的mouseX和mouseY,不再跟随鼠标。
再次按下鼠标时,将创建另一个球对象,并执行与第一个对象相同的操作,依此类推...每次按下鼠标都会创建一个新的球对象。
我有一些创建对象的代码,它跟随鼠标并在我释放鼠标时放置 canvas。但是如果我再次按下我的鼠标,原来的球会回到鼠标。我如何“脱离”球对象,以便我可以制作更多不影响先前放置的球的位置的球对象?
我打算用 ArrayLists 做很多对象。
class Ball {
float xPos, yPos;
Ball(float xPos, float yPos) {
this.xPos= xPos;
this. yPos= yPos;
}
void drawBall() {
ellipse(xPos, yPos, 50, 50);
println("X" + xPos + " Y:"+ yPos);
}
}
Ball ball1;
Ball ball2;
ArrayList<Ball> ballList = new ArrayList<Ball>();
void setup() {
size(600, 600);
ball1=new Ball(900, 900);
ball2=new Ball(900, 900);
}
void draw() {
background(150);
if (mousePressed ) {
ball1.xPos= mouseX;
ball1.yPos=mouseY;
}
ball1.drawBall();
}
这里的问题是您根本没有管理数组,而是一直在处理同一个对象。我使用数组列表重写了您的示例来存储 Ball 对象,并在用户单击鼠标按钮时创建新的球。
这里请注意,我故意使用“添加到数组列表的最后一项”而不是创建一个引用 this 对象的对象。这是因为我不想让您与参考资料等混淆。
ArrayList<Ball> ballList = new ArrayList<Ball>();
void setup() {
size(600, 600);
}
void draw() {
background(150);
// if the mouse button is held down, set the ball's coordinates to the mouse coordinates
if (ballList.size() > 0 && mousePressed) {
ballList.get(ballList.size() - 1).xPos = mouseX; // 'ballList.get(ballList.size() - 1)' is the java way to get the last item added to an arrayList
ballList.get(ballList.size() - 1).yPos = mouseY;
}
// draw every ball in the array (the weird format is just shorthand for a "for each" loop)
for(Ball b : ballList) {
b.drawBall();
}
}
// this method will trigger once every time the user press a mouse button
void mousePressed() {
ballList.add(new Ball(mouseX, mouseY));
}
class Ball {
float xPos, yPos;
Ball(float xPos, float yPos) {
this.xPos= xPos;
this.yPos= yPos;
}
void drawBall() {
ellipse(xPos, yPos, 50, 50);
}
}
通过定位最后创建的球而不是特定的球,您可以确保始终使用正确的对象。另外,请注意我是如何让球对象自己画成一个循环的。 'for each' 形式确保无论 arrayList 中当前有多少个球,它们都会被打印出来。
祝这个项目好运,如果我的回答有什么地方令人困惑,请随时联系我并提出问题。
玩得开心!