亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果我隨后移動鼠標,則在鼠標單擊后觸發移動的對象停止移動

如果我隨后移動鼠標,則在鼠標單擊后觸發移動的對象停止移動

婷婷同學_ 2022-09-22 15:56:40
制作一個簡單的游戲,我點擊屏幕,火箭由于點擊而從左向右移動。當我單擊時,它從鼠標Y獲得其y位置,并具有初始化的x,該x在單擊后開始更改。問題是簡單地移動鼠標,而對象正在移動,導致它停止,另一個問題是按住鼠標左鍵使y與moeY不斷變化,我不想要。再次單擊會使對象從其離開的 x 位置移動,并跳轉到新的 mouseY。我希望在第一次單擊后設置 Y。我將如何解決這些問題?提前感謝任何幫助。我真的不知道該嘗試什么,因為我不知道是什么原因導致它停止移動。火箭級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();  }}主圖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();}
查看完整描述

1 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

添加一個聲明火箭啟動時間的屬性,并向類中添加一個更改 y 坐標并啟動火箭的方法:Rocket


class Rocket

{

    boolean started = false;


    // [...]



    void setY(int newY) {

        this.y = newY;

        started = true;

    }


    void mouseClicked() {


        if (started) {

            this.x = x+5;

        }

    }

實現鼠標按下,它在對象上設置 y 坐標:rocket


void mousePressed() {


    if (mouseButton == LEFT) {4

        rocket.setY(mouseY);  

    }

}   

請注意,該事件僅在按下鼠標按鈕時發生一次。


查看完整回答
反對 回復 2022-09-22
  • 1 回答
  • 0 關注
  • 75 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號