【发布时间】:2016-03-08 16:30:50
【问题描述】:
我正在使用 NodeJS 和 Socket.io。这是我的 index.js
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
app.listen(3000);
console.log("Listening on port 3000");
function handler(req, res) {
fs.readFile(__dirname + '/index.html', function( err, data ) {
if( err ) {
res.writeHead( 500 );
return res.end('Error loading index.html');
}
res.writeHead( 200 );
res.end( data );
});
}
这是我的 index.html
<!DOCTYPE html>
<html>
<head>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<title>Impact Game</title>
<style type="text/css">
html,body {
background-color: #333;
color: #fff;
font-family: helvetica, arial, sans-serif;
margin: 0;
padding: 0;
font-size: 12pt;
}
#canvas {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
<script src="lib/impact/impact.js"></script>
<script src="lib/game/main.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
当我转到 localhost:3000 时,我得到“SyntaxError: expected expression, got '
我尝试遵循的教程是使用 ImpactJS + NodeJS + Socket.IO 制作多人游戏,但这是迄今为止我所掌握的最远的。
这是main.js的内容
ig.module(
'game.main'
)
.requires(
'impact.game',
'impact.font'
)
.defines(function(){
MyGame = ig.Game.extend({
// Load a font
font: new ig.Font( 'media/04b03.font.png' ),
init: function() {
// Initialize your game here; bind keys etc.
},
update: function() {
// Update all entities and backgroundMaps
this.parent();
// Add your own, additional update code here
},
draw: function() {
// Draw all entities and backgroundMaps
this.parent();
// Add your own drawing code here
var x = ig.system.width/2,
y = ig.system.height/2;
this.font.draw( 'It Works!', x, y, ig.Font.ALIGN.CENTER );
}
});
// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
ig.main( '#canvas', MyGame, 60, 320, 240, 2 );
});
【问题讨论】:
-
main.js的内容是什么? -
你有一个流浪括号...
-
如果错误在
main.js,那是你应该发布的文件。 -
您是否尝试运行 main.js 而不是 index.js?
-
当我在本地运行 index.html 时,它可以工作,所以该文件应该是好的。但是当我通过 Node 执行此操作时,我得到了错误。
标签: javascript node.js