For loop 盒子颜色赢了';在for循环过程中不会发生变化。(表作为函数中的参数)

For loop 盒子颜色赢了';在for循环过程中不会发生变化。(表作为函数中的参数),for-loop,lua,love2d,For Loop,Lua,Love2d,最新的问题在底部描述。(表和函数) GIF: 因此,在for循环期间,我试图保存碰撞框的信息,然后在没有发生碰撞时应用它,在这种情况下,如果没有碰撞,我需要框保留math.random()生成的原始颜色 如果发生碰撞,我只需将碰撞框的颜色值更改为红色(255,0,0) 我很确定问题出在for循环和if语句块中。 我将在底部发布脚本的其余部分。这是一个街区: function movePlayer(dt) player.pos.x, player.pos.y, cols, cols_len =

最新的问题在底部描述。(表和函数)

GIF:

因此,在for循环期间,我试图保存碰撞框的信息,然后在没有发生碰撞时应用它,在这种情况下,如果没有碰撞,我需要框保留math.random()生成的原始颜色

如果发生碰撞,我只需将碰撞框的颜色值更改为红色(255,0,0)

我很确定问题出在for循环和if语句块中。 我将在底部发布脚本的其余部分。这是一个街区:

function movePlayer(dt)
  player.pos.x, player.pos.y, cols, cols_len = world:move(
    player, 
    player.pos.x + player.velocity.x *dt, 
    player.pos.y + player.velocity.y *dt)
  for i=1, cols_len do 
    local col = cols[i].other
    local previousColors = {r, g, b}
    local previousBox 
    if cols[i].other then -- if collision is detected then
      lovebird.print(string.format("%s collided with %s's box.", player.name, cols[i].other.name))
      previousBox = i -- store collided box id
      previousColors.r = cols[previousBox].other.color.r -- store color value of the previous collided box
      previousColors.g = cols[previousBox].other.color.g --
      previousColors.b = cols[previousBox].other.color.b --
      cols[i].other.color.r = 255 -- change the color of collided box to red
      cols[i].other.color.g = 0 --
      cols[i].other.color.b = 0 --
      -- debug info --
      lovebird.print(string.format("previousColors: %d, %d, %d, previousBox: %d.", 
      previousColors.r,
      previousColors.g,
      previousColors.b,
      cols[i].other.name))
    else -- if collision is not detected then
      cols[previousBox].other.color.r = previousColors.r -- change color back to the previously saved one
      cols[previousBox].other.color.g = previousColors.g --
      cols[previousBox].other.color.b = previousColors.b --
    end
  end
end
全文:

bump = require 'bump'
player = require 'player'
vector = require 'vector'
timer = require 'timer'
center = vector.new(lg.getWidth()/2, lg.getHeight()/2)
anim8 = require  'anim8'
lovebird = require 'lovebird'
local world = bump.newWorld()
local player = playerNew('sakvojaz', center, 50)

function randomColor()
  return math.random(64, 255)
end

function drawBox(box)
  love.graphics.setColor(box.color.r, box.color.g, box.color.b, 70)
  love.graphics.rectangle('fill', box.pos.x, box.pos.y, box.w, box.h)
  love.graphics.setColor(box.color.r/2, box.color.g/2, box.color.b/2)
  love.graphics.rectangle('line', box.pos.x, box.pos.y, box.w, box.h)
end

function drawPlayerBox(box, r, g, b)
  love.graphics.setColor(r, g, b, 70)
  love.graphics.rectangle('fill', box.pos.x, box.pos.y, box.w, box.h)
  love.graphics.setColor(r/2, g/2, b/2)
  love.graphics.rectangle('line', box.pos.x, box.pos.y, box.w, box.h)
end

function movePlayer(dt)
  player.pos.x, player.pos.y, cols, cols_len = world:move(
    player, 
    player.pos.x + player.velocity.x *dt, 
    player.pos.y + player.velocity.y *dt)
  for i=1, cols_len do 
    local col = cols[i].other
    local previousColors = {r, g, b}
    local previousBox 
    if cols[i].other then -- if collision is detected then
      lovebird.print(string.format("%s collided with %s's box.", player.name, cols[i].other.name))
      previousBox = i -- store collided box id
      previousColors.r = cols[previousBox].other.color.r -- store color value of the previous collided box
      previousColors.g = cols[previousBox].other.color.g --
      previousColors.b = cols[previousBox].other.color.b --
      cols[i].other.color.r = 255 -- change the color of collided box to red
      cols[i].other.color.g = 0 --
      cols[i].other.color.b = 0 --
      -- debug info --
      lovebird.print(string.format("previousColors: %d, %d, %d, previousBox: %d.", 
      previousColors.r,
      previousColors.g,
      previousColors.b,
      cols[i].other.name))
    else -- if collision is not detected then
      cols[previousBox].other.color.r = previousColors.r -- change color back to the previously saved one
      cols[previousBox].other.color.g = previousColors.g --
      cols[previousBox].other.color.b = previousColors.b --
    end
  end
end

function drawPlayer()
  drawPlayerBox(player, 0, 0, 255)
end

blocks = {}

local function addBlock(name, x, y, w, h, r, g, b)
  local block = {name=name, w=w, h=h}
  block.pos = {x=y, y=y}
  block.color = {r=r, g=g, b=b}
  blocks[#blocks+1] = block
  world:add(block, block.pos.x, block.pos.y, w, h)
end


for i=1, #blocks do 
  blocks[i] = addBlock()
end


local function drawBlocks()
  for _,block in ipairs(blocks) do
    drawBox(block)
  end
end

function love.load()
  world:add(player, player.pos.x, player.pos.y, 32, 32)

  for i=1,3 do
    addBlock(i, math.random(100, 600),
              math.random(100, 400),
              math.random(10, 100),
              math.random(10, 100), 
              randomColor(),
              randomColor(),
              randomColor()
    )
  end
end

function love.update(dt)
  --------------------------
  lovebird.update() -- console debug stuff
  --------------------------

  cols_len = 0
  player.Update(dt)
  movePlayer(dt)
end

function love.draw()
  drawPlayer()
  love.graphics.setColor(255, 255, 255, 255)
  player.Draw()
  drawBlocks()
end
所以我猜for循环中的“i”迭代器只是检测到的碰撞量。由于我需要跟踪什么是什么,我提出了以下想法:

local function addBlock(id, x, y, w, h, r, g, b)
  local block = {}
  block[id] = {name=id, w=w, h=h}
  block[id].pos = {x=y, y=y}
  block[id].color = {r=r, g=g, b=b}
  blocks[#blocks+1] = block[id]
  world:add(block[id], block[id].pos.x, block[id].pos.y, w, h)
end
我会在函数中传递一个id(数字),并将其分配给表。 这样我就可以像这样单独访问每个表:

previousBox = cols[i].other[id].name
但问题是我得到了这个错误:尝试索引全局“块”(一个nil值)

这就是问题所在,我认为:

function drawBox(box, id)
  love.graphics.setColor(box[id].color.r, box[id].color.g, box[id].color.b, 70)
  love.graphics.rectangle('fill', box[id].pos.x, box[id].pos.y, box[id].w, box.h)
  love.graphics.setColor(box[id].color.r/2, box[id].color.g/2, box[id].color.b/2)
  love.graphics.rectangle('line', box[id].pos.x, box[id].pos.y, box[id].w, box[id].h)
end
如何将具有正确索引的表传递给函数?
我很确定有更好的方法可以做到这一点。

我认为你的问题在于你在
循环中正确地初始化变量,因为在每次迭代中你都会擦除
PreviousColor
previousBox
的值,在循环之前尝试这样做:

function movePlayer(dt)
  player.pos.x, player.pos.y, cols, cols_len = world:move(
    player, 
    player.pos.x + player.velocity.x *dt, 
    player.pos.y + player.velocity.y *dt)
    local previousColors = {r, g, b}
    local previousBox
  for i=1, cols_len do
    if cols[i].other then -- if collision is detected then
      lovebird.print(string.format("%s collided with %s's box.", player.name, cols[i].other.name))
      previousBox = i -- store collided box id
      previousColors.r = cols[previousBox].other.color.r -- store color value of the previous collided box
      previousColors.g = cols[previousBox].other.color.g --
      previousColors.b = cols[previousBox].other.color.b --
      cols[i].other.color.r = 255 -- change the color of collided box to red
      cols[i].other.color.g = 0 --
      cols[i].other.color.b = 0 --
      -- debug info --
      lovebird.print(string.format("previousColors: %d, %d, %d, previousBox: %d.", 
      previousColors.r,
      previousColors.g,
      previousColors.b,
      cols[i].other.name))
    else -- if collision is not detected then
      cols[previousBox].other.color.r = previousColors.r -- change color back to the previously saved one
      cols[previousBox].other.color.g = previousColors.g --
      cols[previousBox].other.color.b = previousColors.b --
    end
  end
end

是的,似乎是这样。我一到家就确认,谢谢@不客气。如果我的回答对你真的有用,请接受。