迷你"得分守护者"项目 - 有几个问题

时间:2017-07-24 13:23:18

标签: javascript project

我正在开发一个简单的记分器应用程序,并且有一些东西没有按预期工作。

https://codepen.io/Modestas/pen/LjPPVQ

以下是我的代码部分:

/usr/bin/c++      CMakeFiles/webrtcbridge_node.dir/src/webrtcbridge_node.cc.o  -o /home/carlos/Documentos/ROS_WebRTC/catkin_ws/devel/lib/webrtcbridge/webrtcbridge_node -rdynamic /opt/ros/kinetic/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/kinetic/lib/librosconsole.so /opt/ros/kinetic/lib/librosconsole_log4cxx.so /opt/ros/kinetic/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/kinetic/lib/libxmlrpcpp.so /opt/ros/kinetic/lib/libroscpp_serialization.so /opt/ros/kinetic/lib/librostime.so /opt/ros/kinetic/lib/libcpp_common.so -lboost_system -lboost_thread -lboost_chrono -lboost_date_time -lboost_atomic -lpthread -lconsole_bridge /home/carlos/Documentos/ROS_WebRTC/catkin_ws/src/libwebrtc/out/lib/libwebrtc.a -lSM -lICE -lX11 -lXext -ldl -lrt -Wl **"here should be -pthread I think"**,-rpath,/opt/ros/kinetic/lib

1)每当" P1Span"和#34; P2Span"变得等于输入字段的值,游戏不会结束。我甚至尝试使用双重比较运算符,但仍然没有。

2)按下重置按钮只会将输入字段重置为默认值5,但不会将分数重置为0.

3)由于某种原因,当通过单击箭头(快速)更改输入值时,会出现很多延迟,并且更改并不总是反映在HTML中。

如果有人对这些问题有任何意见,我会非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

只需进行一些简单的更改

1)我在点击事件if(p1ScoreCount == inputScore.value) {...}

中添加了这些验证

<强> 2) p1Span.innerHTML = 0; p2Span.innerHTML = 0;

3)这不一定是个问题,它是事件"change"的返回时间,也许"click"事件就是你要找的东西

const p1Span = document.getElementById("p1Span");
const p2Span = document.getElementById("p2Span");
const p1Button = document.getElementById("p1");
const p2Button = document.getElementById("p2");
const inputScore = document.getElementById("inputScore");
const playToSpan = document.getElementById("playToSpan");
const resetButton = document.getElementsByTagName("button")[2];
let gameOver = false;
let p1ScoreCount = 0;
let p2ScoreCount = 0;


// player 1/2 buttons add score to span when clicked.

p1Button.addEventListener("click", () => {
  p1ScoreCount++;
  p1Span.textContent = p1ScoreCount;
  if (p1ScoreCount == inputScore.value) {
    alert("Player 1 Win");
    p1Span.className += " green";
    p1Button.disabled = true;
    p2Button.disabled = true;
    //reset();
  }
});

p2Button.addEventListener("click", () => {
  p2ScoreCount++;
  p2Span.textContent = p2ScoreCount;
  if (p2ScoreCount == inputScore.value) {
    alert("Player 2 Win");
    p2Span.className += " green";
    p1Button.disabled = true;
    p2Button.disabled = true;
  }
});

// specify what you're playing to using text input

inputScore.addEventListener("click", () => {
  playToSpan.textContent = inputScore.value;
});

// reset button resets the game

resetButton.addEventListener("click", function() {
  reset();
});

function reset() {
  gameOver = false;
  p1ScoreCount = 0;
  p2ScoreCount = 0;
  p1Span.innerHTML = 0;
  p2Span.innerHTML = 0;
  inputScore.value = 5;
  p1Span.classList.remove("green");
  p2Span.classList.remove("green");
  p1Button.disabled = false;
  p2Button.disabled = false;
}
.green {
  color: green;
}
<div>
  <h1><span id="p1Span">0</span> to <span id="p2Span">0</span></h1>
  <h3>Playing to: <span id="playToSpan">5</span></h3>
  <input id="inputScore" type="number" value="5">
  <button id="p1">Player One</button>
  <button id="p2">Player Two</button>
  <button id="reset">Reset</button>
</div>