所以看一下:我在游戏中有一个怪物,当玩家与他发生碰撞时,会出现一个面板并说再试一次。 现在有一个盾牌对象,可以保护玩家免于死亡,所以当玩家与它发生碰撞然后如果你触摸怪物你不应该死一次:就像那样,你触摸盾牌和你免受一人死亡。你能帮我写这段代码吗?
public GameObject panel;
public bool hasShield = false; /* no shield in the beginning */
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Shield")
{
hasShield = true; //We are safe now.
/* TODO: StartCoroutine() or Invoke() to reset the variable and the graphic effect after some amount of time. */
}
else if (col.gameObject.tag == "Monster" && !hasShield)
{
//We hit a monster and had no shield. Display gameover.
panel.SetActive (true);
}
//I assume the panel is inactive by default so no need to call SetActive(false) on it.
}
答案 0 :(得分:5)
永远无法访问此代码的内部:
if (col.gameObject.tag == "Shield")
{
if(col.gameObject.tag == "Monster")
{
panel.SetActive(false);
}
}
对于要通过的第一个if
检查,标记必须为“Shield”,但是对于要通过的第二个测试,它必须是“Monster”。这两个条件不能同时为真,代码相当于
if(col.gameObject.tag == "Shield" && col.gameObject.tag == "Monster")
但是,如果您在上面的表达式中将&&
替换为||
(逻辑或),您将获得所需的结果。所以,重写的代码将是
if (col.gameObject.tag == "Monster")
{
panel.SetActive (true);
}
if (col.gameObject.tag == "Shield" || col.gameObject.tag == "Monster")
{
panel.SetActive(false);
}
如果是“盾牌”或“怪物”,内部代码现在会触发。
编辑: 正如我们在评论中谈到的那样,逻辑总体上存在缺陷。现在根据你的描述
我触摸怪物后需要激活面板,但是如果我触摸盾牌然后触摸怪物那么面板就不应该出现了
我建议如下:收集盾牌后,将其保存到某个变量,例如
bool hasShield;
收集盾牌时将其设为true
。然后,如果你击中某个东西,检查它是否是一个怪物,如果你没有盾牌,那么你会看到“游戏结束”屏幕。所以玩家应该看起来像
public GameObject panel;
public bool hasShield = false; /* no shield in the beginning */
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Shield")
{
hasShield = true; //We are safe now.
/* TODO: StartCoroutine() or Invoke() to reset the variable and the graphic effect after some amount of time. */
}
else if (col.gameObject.tag == "Monster")
{
if(!hasShield)
panel.SetActive (true); //We hit a monster and had no shield. Display gameover.
else
hasShield = false; //loose the shield
}
//I assume the panel is inactive by default so no need to call SetActive(false) on it.
}
答案 1 :(得分:0)
这是核心代码!
Please use onchange event instead of onclick and pass the value in the ValueID function.
Please replace javascript and php code with this:
<script>
function ValueID(vid){
$.ajax({
url: 'select_modelcar.php?brandid='+vid,
type: 'POST',
success: function (data) {
if (data === 'success') {
document.getElementById("getval").innerHTML = data;
}
}
});
};
</script>
<select name="select_brandcar" id="select_brandcar" onchange="ValueID(this.value);" >
<option>Press Choose</option>
<?php
$sql = sprintf ("SELECT * FROM brand" );
$res = mysql_query ($sql);
while ($arr = mysql_fetch_array($res)){
printf ("<option value='%s'>%s</option>" ,$arr['id'], $arr['name']);
}
?>
<span id="getval"></span>
In PHP FIle:
<?php
mysql_query("SET NAMES UTF8");
include 'include_connectdb.php';
@$varbrandid = $_GET['brandid'];
@$sql = sprintf("SELECT * FROM maingeneration WHERE brandfk = %s", $varbrandid);
/*id ของตาราง catagory*/
@$res = mysql_query($sql);
$select = "<select name='select' id='select'>");
while ($arr = mysql_fetch_array($res)) {
$select .= printf("<option value='%s'>%s</option>", $arr['maingenerationid'], $arr['maingenerationname']);
}
$select .="</select>";
echo $select;
exit;
?>