Problem with Impact pong demo and Node/socket
Posted: Mon May 21, 2012 10:19 am
Hi guys,
I'm not having much luck getting this Impact demo of Pong to work well with Node and Socket.io.
I'm basically at the stage (the end of video 7 of 'hallsofvalhalla's' video tutorials) of having the 2nd paddle controllable by another browser instance (i.e. another client/player) but I'm receiving an error and the 2nd player entity isn't loading, nor is the 1st player's position and movements updating on the 2nd player's screen.
The error I receive in Firebug (firefox) is pointing at a the line:
and the error is: 'ig.game' is null.
Here is my code so far. Note, I'm only posting the index.html, app.js, main.js and the paddle-player.js files as they are the only ones that probably matter.
First off, my index.html
Next up, my app.js:
next is my paddle-player.js:
And finally, my main.js
I know this is a huge post and a bit of a pain to have to look over, but I would really appreciate any help given.
I'm not having much luck getting this Impact demo of Pong to work well with Node and Socket.io.
I'm basically at the stage (the end of video 7 of 'hallsofvalhalla's' video tutorials) of having the 2nd paddle controllable by another browser instance (i.e. another client/player) but I'm receiving an error and the 2nd player entity isn't loading, nor is the 1st player's position and movements updating on the 2nd player's screen.
The error I receive in Firebug (firefox) is pointing at a the line:
Code: Select all
var netplayers = ig.game.getEntitiesByType( EntityOtherplayer );
Here is my code so far. Note, I'm only posting the index.html, app.js, main.js and the paddle-player.js files as they are the only ones that probably matter.
First off, my index.html
Code: Select all
<!DOCTYPE html>
<html>
<head>
<script src="http://localhost:8025/socket.io/socket.io.js"></script>
<title>Impact Game</title>
<style type="text/css">
html,body {
background-color: #000;
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;
border: 1px solid #555;
}
</style>
<script type="text/javascript" src="lib/impact/impact.js"></script>
<script type="text/javascript" src="lib/game/main.js"></script>
</head>
<body>
<script type="text/javascript">
var namerand = Math.floor(Math.random()*999);
var playername = "player" + namerand;
var socket = io.connect('http://localhost:8025');
socket.on('message', function (data) {
var player = ig.game.getEntitiesByType( EntityPaddlePlayer )[0];
if(player)
{
player.messagebox = player.messagebox + '\n' + data + ' disconnected';
}
});
socket.on('playermove', function (positiony,thisgamename) {
var otherplayer = ig.game.getEntitiesByType( EntityOtherplayer );
if(otherplayer)
{
for(var i in otherplayer)
{
if(thisgamename == otherplayer[i].gamename)
{
// otherplayer[i].pos.x = positionx;
otherplayer[i].pos.y = positiony;
otherplayer[i].animation = currentanimation;
}}}
});
socket.on('netreplayer', function (playerlist) {
var netplayers = ig.game.getEntitiesByType( EntityOtherplayer );
//////////////loop to see if players exist
if(netplayers){
for(var i in netplayers)
{
netplayers[i].kill();
}}
for(var i in playerlist)
{
if(playername != playerlist[i])
{
ig.game.spawnEntity( EntityOtherplayer, 50, 250, {gamename:playerlist[i]} );
}
}
/////////////////////////////////
});
socket.on('addplayer', function (playerlist,otherplayername) {
var player = ig.game.getEntitiesByType( EntityPaddlePlayer )[0];
player.messagebox = player.messagebox + '\n' + otherplayername + ' joined';
for(var i = 0;i<playerlist.length;i++)
{
if(player.gamename != playerlist[i])
{
ig.game.spawnEntity( EntityOtherplayer, 50, 150, {gamename:playerlist[i]} );
}}
});
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
</script>
<canvas id="canvas"></canvas>
</body>
</html>
Code: Select all
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8025);
var playerlocation = 0;
var playerlist = [];
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);
});
}
io.sockets.on('connection', function (socket) {
socket.on('recievedata', function (positiony,gamename) {
socket.broadcast.emit('playermove',positiony,gamename);
});
socket.on('initializeplayer', function (newplayername) {
socket.clientname = newplayername;
playerlist.push(newplayername);
io.sockets.emit('addplayer',playerlist,newplayername);
});
socket.on('disconnect', function(){
delete playerlist[socket.clientname];
for(var i in playerlist)
{
if(playerlist[i] == socket.clientname)
{
playerlist.splice(i, 1);
}
}
socket.broadcast.emit('message',socket.clientname);
socket.broadcast.emit('netreplayer',playerlist);
});
});
Code: Select all
ig.module(
'game.entities.paddle-player'
)
.requires(
'impact.entity'
)
.defines(function(){
var ismove = 0;
var speed = 100;
var playername = "playername";
EntityPaddlePlayer = ig.Entity.extend({
size: {x: 64, y: 128},
messagebox: "",
type: ig.Entity.TYPE.A,
nettimer: 10,
name: "player",
gamename: playername,
messageboxtimer: 200,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.FIXED,
animSheet: new ig.AnimationSheet( 'media/paddle-red.png', 64, 128 ),
init: function( x, y, settings ) {
this.parent( x, y, settings );
//add animations
this.addAnim( 'idle', 1, [0] );
socket.emit('initializeplayer', this.gamename);
},
//movement keys
update: function() {
if( ig.input.state('up') ) {
this.vel.y = -100;
}
else if( ig.input.state('down') ) {
this.vel.y = 100;
}
else {
this.vel.y = 0
}
if(this.nettimer < 1)
{
this.nettimer = 5;
socket.emit('recievedata', this.pos.y, this.gamename);
}
this.nettimer = this.nettimer -1;
this.parent();
}
});
//otherplayer
EntityOtherplayer = ig.Entity.extend({
size: {x: 64, y: 128},
type: ig.Entity.TYPE.B,
speed: 100,
name: "otherplayer",
gamename: "",
checkAgainst: ig.Entity.TYPE.B,
collides: ig.Entity.COLLIDES.FIXED,
animSheet: new ig.AnimationSheet( 'media/paddle-blue.png', 64, 128 ),
init: function( x, y, settings) {
this.parent( x, y, settings);
this.addAnim( 'idle', 1, [0] );
},
netmoveplayer: function()
{
this.pos.y = positiony;
},
update: function() {
if( ig.input.state('up') ) {
this.vel.y = -100;
}
else if( ig.input.state('down') ) {
this.vel.y = 100;
}
else {
this.vel.y = 0
}
if(this.nettimer < 1)
{
this.nettimer = 5;
}
}
});
})
Code: Select all
ig.module(
'game.main'
)
.requires(
'impact.game',
'impact.font',
'game.levels.main',
'game.entities.paddle-player'
)
.defines(function(){
MyGame = ig.Game.extend({
// Load a font
font: new ig.Font( 'media/04b03.font.png' ),
init: function() {
ig.input.bind( ig.KEY.UP_ARROW, 'up' );
ig.input.bind( ig.KEY.DOWN_ARROW, 'down' );
this.loadLevel( LevelMain );
},
update: function() {
// Update all entities and backgroundMaps
this.parent();
// Add your own, additional update code here
var player = this.getEntitiesByType( EntityPaddlePlayer ) [0];
},
draw: function() {
// Draw all entities and backgroundMaps
this.parent();
var player = this.getEntitiesByType( EntityPaddlePlayer ) [0];
player.messageboxtimer = player.messageboxtimer -1;
if(player.messageboxtimer < 1)
{
player.messageboxtimer =100;
var newtext = "";
var newsplit = player.messagebox.split("\n")
for(var i = 0;i < newsplit.length; i++)
{
if(i > 1)
{
newtext = newtext + "\n" + newsplit[i];
}
}
player.messagebox = newtext;
}
this.font.draw( player.messagebox, 350, 10);
}
});
ig.main( '#canvas', MyGame, 60, 768, 480, 1 );
});