简体   繁体   English

如果我然后移动鼠标,则在鼠标单击停止移动后触发移动的对象

[英]Object triggered to move after mouse click stops moving if i then move the mouse

Making a simple game where i click on the screen and a rocket moves left to right because of the click.制作一个简单的游戏,我点击屏幕,火箭会因为点击而从左向右移动。 It gets its y position from mouseY when i click and has an initialised x which begins to change after the click.当我点击时,它从 mouseY 获得它的 y 位置,并且有一个初始化的 x,它在点击后开始改变。 The problem is simply moving the mouse whilst the object is moving causes it to stop and another problem is holding the left mouse button down makes makes the y change continuously with mouseY which i don't want.问题只是在对象移动时移动鼠标会导致它停止,另一个问题是按住鼠标左键会使 y 随 mouseY 不断变化,这是我不想要的。 Clicking again makes the object move from x position where it left off and jumps to new mouseY.再次单击会使对象从其停止的 x 位置移动并跳转到新的鼠标 Y。 I want the Y to be set after the first click.我希望在第一次点击后设置 Y。 How would i fix these issues?我将如何解决这些问题? thanks a lot in advance for any help.非常感谢您提供任何帮助。

I don't really know what to try as i don't know whats causing it to stop moving.我真的不知道该尝试什么,因为我不知道是什么导致它停止移动。

Rocket class火箭级

class Rocket
{ 
  int x = -100;
  int y;


  void render()
  {
    fill(153,153,153);
    rect(x,y,40,10);  //rocket body  
    fill(255,0,0);
    triangle(x+60,y+5,x+40,y-5,x+40,y+15);  //rocket head
    triangle(x+10,y+10,x,y+15,x,y+10);  //bottom fin
    triangle(x+10,y,x,y,x,y-5);  //top fin
    fill(226,56,34);
    stroke(226,56,34);
    triangle(x-40,y+5,x,y,x,y+10);  //fire
    fill(226,120,34);
    stroke(226,120,34);
    triangle(x-20,y+5,x,y,x,y+10);  //fire
  } 
  void mouseClicked()
  {
    if (mouseButton == LEFT)
    {
      y = mouseY;
      this.x = x+5;
    }
  }

  void update()
  {
    render();
    mouseClicked();
  }
}

Main sketch主要草图

ArrayList<Alien> aliens = new ArrayList<Alien>();
Rocket rocket;

void setup()
{
  size(1200,900);
  for (int i = 0; i < 5; i++)
  {
    aliens.add(new Alien());
  }
  rocket = new Rocket();
}

void draw()
{
  background(0);
  moon(); 
  for (int i = aliens.size()-1; i >= 0; i--)
  {
    aliens.get(i).update();
    if (aliens.get(i).CheckHit())
    {
      aliens.remove(i);
    }
  } 
  rocket.update();
}

Add an attribute which stated when the rocket was started and add a method to the class Rocket which changes the y coordinate and starts the rocket:添加一个声明火箭何时启动的属性,并向Rocket类添加一个方法,该方法更改 y 坐标并启动火箭:

class Rocket
{
    boolean started = false;

    // [...]


    void setY(int newY) {
        this.y = newY;
        started = true;
    }

    void mouseClicked() {

        if (started) {
            this.x = x+5;
        }
    }
} 

Implement the mousePressed , which set the y coordinate on the object rocket :实现mousePressed ,它在对象rocket上设置 y 坐标:

void mousePressed() {

    if (mouseButton == LEFT) {4
        rocket.setY(mouseY);  
    }
}   

Note, the event only occurs once when the mouse button is pressed.请注意,该事件仅在按下鼠标按钮时发生一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM