gpt4 book ai didi

java - 处理程序 postDelayed 只工作一次

转载 作者:行者123 更新时间:2023-12-02 09:14:39 24 4
gpt4 key购买 nike

我正在尝试更改 handler.postDelay() 中处理程序的延迟,但在这里它只工作一次然后停止。关键的方法在最后。我在想如何使可运行程序在 postDelay() 循环中工作。

class MyGestureListener implements GestureDetector.OnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e) {
column = (int) (e.getX() / cellWidth);
row = (int) (e.getY() / cellHeight);
selectedNode = (6 * row) + column;


if (selectedNode == enemyNode && !atak && GameMovingObject.ableToAttack(chibiAlive, enemyAlive, chibiNode, enemyNode)) {

if (attackOption) {
chibi1.attack(enemies.get(enemyId));
//attackOption=false;
atak = true;

if (!enemyAlive) {
enemyId = enemyId + 1;
enemies.add(new Enemy(enemyId, GameSurface.this, wolf, Enemy.genPosition(getWidth(), getHeight())[0], Enemy.genPosition(getWidth(), getHeight())[1], cellWidth, cellHeight));
enemyAlive = true;
}
}
}

// ...
return true;
}
}

相关方法

public static boolean ableToAttack(boolean chibiAlive, boolean enemyAlive, int chibiNode, int enemyNode) {
if(!chibiAlive || !enemyAlive)
return false;

boolean able = false;
if (chibiNode > enemyNode)
if (chibiNode - enemyNode == 1 || chibiNode - enemyNode == 5 || chibiNode - enemyNode == 6 || chibiNode - enemyNode == 7)
able = true;
if (enemyNode > chibiNode)
able = enemyNode - chibiNode == 1 || enemyNode - chibiNode == 5 || enemyNode - chibiNode == 6 || enemyNode - chibiNode == 7;
return able;
}

public void attack(Enemy enemy) {
if (partOfBody.equals("legs"))
enemy.getLegsDamage(generateDamage());

if (enemy.hp <= 0) {
enemy.hp = 0;
gs.attackOption = false;
gs.setEnemyAlive(false);
addExperience();
dbHelper.setExp("1", exp);
// ...
}
}

最后

public void getLegsDamage(int legsDamage) {
fullLegsDamage = fullLegsDamage + legsDamage;
hp = hp - legsDamage;
if(fullLegsDamage>50) {
legsDamaged=2;
handler2.removeCallbacksAndMessages(null);
handler2.postDelayed(runnable, 4000);
}
}

可运行的看起来像这样

runnable = new Runnable() {
@Override
public void run() {

hit=true;
//gs.canvas.drawBitmap(bitmap,200,200, null);
cc.setHp(cc.getHp() - generateDamage());
if(cc.getHp() <= 0) {
cc.setHp(0);
gs.setChibiAlive(false);
}
((Gra)gs.getContext()).updateChibiHpMp(cc);
}
};

到现在为止我已经想了整整一天了。请帮助我。

最佳答案

不完全确定您要什么,但我将问题解释为您希望 Runnable 每 4 秒运行一次,在这种情况下,Runnable 需要自行发布在完成之前,如下所示:

runnable = new Runnable() {
@Override
public void run() {
hit=true;
//gs.canvas.drawBitmap(bitmap,200,200, null);
cc.setHp(cc.getHp() - generateDamage());
if(cc.getHp() <= 0) {
cc.setHp(0);
gs.setChibiAlive(false);
}
((Gra)gs.getContext()).updateChibiHpMp(cc);

// Schedule the runnable again
handler2.postDelayed(this, 4000)
}
};

关于java - 处理程序 postDelayed 只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59094908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com