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

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

通過觸摸屏幕移動矩形

通過觸摸屏幕移動矩形

瀟瀟雨雨 2021-04-14 21:18:15
我想移動具有不同x位置的塊而不通過減小x位置來更改其形狀。我試圖運行以下代碼,但似乎塊移動到拖曳位置的方式更快(正確的藥水和其他我看不到的地方)。downBlocks=new Arraylist<Rectangle>;for (DownBlocks downBlocks:getBlocks()){    if(Gdx.input.isTouched()) {        Vector3 touchPos = new Vector3();        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);        camera.unproject(touchPos);        downBlocks.x = (int) touchPos.x - downBlocks.x;    }}
查看完整描述

1 回答

?
溫溫醬

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

要進行拖動,您需要記住手指最后一次觸摸屏幕的位置,以便獲得手指增量。另外,如果只需要調用一次,則避免將代碼放入循環迭代中。一遍又一遍地為每個DownBlock取消投影屏幕的觸摸點是很浪費的。


static final Vector3 VEC = new Vector3(); // reusuable static member to avoid GC churn

private float lastX; //member variable for tracking finger movement


//In your game logic:

if (Gdx.input.isTouching()){

    VEC.set(Gdx.input.getX(), Gdx.input.getY(), 0);

    camera.unproject(VEC);

}


if (Gdx.input.justTouched())

    lastX = VEC.x; //starting point of drag

else if (Gdx.input.isTouching()){ // dragging

    float deltaX = VEC.x - lastX; // how much finger has moved this frame

    lastX = VEC.x; // for next frame


    // Since you're working with integer units, you can round position

    int blockDelta = (int)Math.round(deltaX);


    for (DownBlocks downBlock : getBlocks()){

        downBlock.x += blockDelta;

    }

}

不過,我不建議您使用整數單位作為坐標。如果您要進行像素畫,則建議使用浮點數來存儲坐標,并且僅在繪制時才對坐標進行四舍五入。這將減少看起來像生澀的運動。如果您不使用像素圖,我將一直使用浮動坐標。這是一篇很好的文章,可以幫助您理解單位。


查看完整回答
反對 回復 2021-04-28
  • 1 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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