Re: [nodejs] CraftyJS, Node.js, SocketIO, V8 woes.

350 views
Skip to first unread message

Karl Tiedt

unread,
Aug 5, 2012, 4:36:16 PM8/5/12
to nod...@googlegroups.com
It seems to work fine in my repl test (and browser test)...

The reason splice returns an array is quite simple... thats what splice does ;)

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice

-Karl Tiedt

Martin Cooper

unread,
Aug 5, 2012, 4:52:50 PM8/5/12
to nod...@googlegroups.com
On Sat, Aug 4, 2012 at 11:40 AM, Brian Stahly <brian....@gmail.com> wrote:
> I am trying to build a small, online card game with NodeJS. Having C# and
> javascript experience, I am having way more trouble than I expected.
>
> Using node-static, I set up an http server (server.js) that successfully
> serves client.html and any requested assets.
> I am starting to build the basics of the card game using the CraftyJS
> library in client.js (running on the client.html page).
> Using Socket.io, I can successfully send information from server.js to my
> client.js file.
>
> My next step was to generate an array of cards in server.js, splice one out
> randomly, and send it to client.js. My hope was:
>
> var deck = new Array();
> deck.push(new Array("card1 info", "more info"));
> deck.push(new Array("card 2 info", "more info"));
> deck.push(new Array("card 3 info", "more info"));
>
> function drawCard(){
> return deck.splice(1,1);
> }
>
> I've spent the whole day reading, but I can't find a clear concise answer.
> Question 1: Why doesn't this work?

It does. You created an array of arrays, then you used splice to get
the middle one. In the repl, after setting up deck as above:

> deck
[ [ 'card1 info', 'more info' ],
[ 'card 2 info', 'more info' ],
[ 'card 3 info', 'more info' ] ]
> deck.splice(1,1)
[ [ 'card 2 info', 'more info' ] ]
> deck
[ [ 'card1 info', 'more info' ],
[ 'card 3 info', 'more info' ] ]

Presumably this isn't what you expected, but you didn't say what
you're looking for.

> The V8 compiler sees arrays as string
> values? This is not the javascript I know, is there a tutorial for this new
> way of dealing with arrays?
>
> After more reading, I got it working a this way:
> var deck = [];
> deck[0] = {info:"test", moreInfo:"test"};
> deck[1] = {info:"test", moreInfo:"test"};
> deck[2] = {info:"test", moreInfo:"test"};
>
> However:
> console.log(deck[0]) yields {info: 'test', moreInfo:'test'}
> console.log(deck.splice(0,1)) yields [{info: 'test', moreInfo:'test'}]
>
> Question 2: Why does splice return the square brackets, but calling the
> value directly does not?

Because, as Karl mentioned, that's what splice does. :) It returns an
array containing the elements that were spliced out of the original
array.

--
Martin Cooper


> Any answers or additional links are really appreciated. When I search for
> javascript help, I end up on HTML javascript pages, which obviously doesn't
> work as I expect.
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Adam Reynolds

unread,
Aug 5, 2012, 6:41:22 PM8/5/12
to nod...@googlegroups.com

I know this is slightly off-topic but why did you choose crafty over the other game engine that are out there?

Marak Squires

unread,
Aug 5, 2012, 7:38:23 PM8/5/12
to nod...@googlegroups.com
http://eloquentjavascript.net/

On Sat, Aug 4, 2012 at 11:40 AM, Brian Stahly <brian....@gmail.com> wrote:
I am trying to build a small, online card game with NodeJS. Having C# and javascript experience, I am having way more trouble than I expected.

Using node-static, I set up an http server (server.js) that successfully serves client.html and any requested assets.
I am starting to build the basics of the card game using the CraftyJS library in client.js (running on the client.html page).
Using Socket.io, I can successfully send information from server.js to my client.js file.

My next step was to generate an array of cards in server.js, splice one out randomly, and send it to client.js. My hope was:

var deck = new Array();
deck.push(new Array("card1 info", "more info"));
deck.push(new Array("card 2 info", "more info")); 
deck.push(new Array("card 3 info", "more info"));

function drawCard(){
  return deck.splice(1,1);
}

I've spent the whole day reading, but I can't find a clear concise answer.
Question 1: Why doesn't this work? The V8 compiler sees arrays as string values? This is not the javascript I know, is there a tutorial for this new way of dealing with arrays?

After more reading, I got it working a this way:
var deck = [];
deck[0] = {info:"test", moreInfo:"test"};
deck[1] = {info:"test", moreInfo:"test"}; 
deck[2] = {info:"test", moreInfo:"test"};

However:
console.log(deck[0]) yields {info: 'test', moreInfo:'test'}
console.log(deck.splice(0,1)) yields [{info: 'test', moreInfo:'test'}]

Question 2: Why does splice return the square brackets, but calling the value directly does not?

Gustavo Machado

unread,
Aug 7, 2012, 11:39:56 AM8/7/12
to nod...@googlegroups.com
Adam, which other ones do you recommend?

Gus

On Sun, Aug 5, 2012 at 7:41 PM, Adam Reynolds <awjre...@gmail.com> wrote:
> I know this is slightly off-topic but why did you choose crafty over the
> other game engine that are out there?
>

Adam Reynolds

unread,
Aug 7, 2012, 11:49:50 AM8/7/12
to nod...@googlegroups.com
Well I looked at Crafty and it doesn't seem to support touch screen. 

I don't think it's a wrong choice. I'm assuming you evaluated all of these :)

EaselJS 
ImpactJS
CraftyJS
MelonJS
Spaceport.io
LimeJS
Reply all
Reply to author
Forward
0 new messages