Log In  

Hi all.
The following code doesn't work -- players don't get drawn.
What am I doing wrong?
Thanks in advance.
Alex

players = {}

function make_player(col)
  local p = {}
  p.x = 0
  p.y = 0
  p.color = col
  return p
end

function draw_player(p)
  rectfill(p.x-2,p.y-2,p.x+2,p.y+2,p.color)
end

function _init()
  players.home = make_player(1)
  players.visitor = make_player(8)
end

function _draw()
  cls()
  foreach(players, function(p) draw_player(p))
end
P#14559 2015-09-24 10:12 ( Edited 2015-09-25 01:57)

foreach() and all() and count() only operate on sequential integer indexes starting from 1.

so

> a = {name="bob","foo","bar","baz", x=1, y=5,[4]="wizzle",[6]="nope"}
> foreach(a,print)
foo
bar
bar
wizzle
P#14560 2015-09-24 10:25 ( Edited 2015-09-24 14:25)

Thanks for the fast reply. Clear.

P#14561 2015-09-24 10:32 ( Edited 2015-09-24 14:32)

0.1.2 is getting pairs() to iterate over all objects.

P#14566 2015-09-24 10:45 ( Edited 2015-09-24 14:45)

Do you have any sample code -- or some docs -- for pairs? I'm curious as to usage.

P#14580 2015-09-24 16:55 ( Edited 2015-09-24 20:55)

Got it. Thx.

P#14610 2015-09-24 21:57 ( Edited 2015-09-25 01:57)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 17:21:09 | 0.020s | Q:19