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)

:: impbox

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)
:: alexr

Thanks for the fast reply. Clear.

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

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

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

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)
:: matt
P#14601 2015-09-24 19:25 ( Edited 2015-09-24 23:25)
:: alexr

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 2019-12-13 14:07 | 0.015s | 2097k | Q:22