我現在用的是Box2dJS庫,它使用的原型,所以我必須包括爲原型庫它工作。我沒有在我的主腳本中使用任何原型代碼,只有Box2d對象使用原型。我在我的腳本中使用了JQuery,並且由於我包含了原型,所以jQuery事件似乎並沒有觸發。
下面是相關的JS代碼:
$.noConflict();
jQuery(document).ready(function(){
init();
});
function init(){
//add event handler to surrounding DIV to monitor mouse move and keystrokes
jQuery("#container").keypress(function(e){
keys[e.keyCode] = true;
alert("key pressed!");
});
jQuery("#container").keydown(function(e){
keys[e.keyCode] = true;
});
jQuery("#container").keyup(function(e){
keys[e.keyCode] = true;
alert("Key released!");
});
}
這裏是html代碼:
<html>
<head>
<title>Box2D Test</title>
<!--[if IE]><script src="lib/excanvas.js"></script><![endif]-->
<script src="lib/prototype-1.6.0.2.js"></script>
<!-- box2djs scripts here-->
</head>
<body>
<center>
<div id="container" style="border:1px solid; cursor:none; width:600px; height:400px;">
<canvas id="canvas" width="600" height="400" >
Could not create Canvas!
</canvas>
</div>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script src='js/box2dutils.js'></script>
<script type="text/javascript" src="main.js"></script>
</center>
</body>
</html>
警報都不要去了,但一切工作都OK,當我刪除原型(和box2d)庫。我怎樣才能解決這個問題?
@Nicklas A.並非如此。我不是試圖從jQuery中觸發一個原型事件,我試圖只使用jQuery事件。而且,這個問題甚至沒有解決。 – Keelx