2017-04-18 1 views
0

Das letzte Problem wird unten beschrieben. (Tabellen und Funktionen)Die Farbe der Box ändert sich während der for-Schleife nicht. (Tabellen als Parameter in Funktionen)

GIF: http://i.imgur.com/jZ9zmbG.gifv

So während der for-Schleife, Ich versuche, die Informationen von kollidierte Boxen zu retten, dann gelten, wenn Kollision nicht geschieht, in diesem Fall muss ich die Boxen behalten Sie ihre ursprünglichen Farben, die durch math.random() erzeugt werden, wenn es keine Kollision gibt.

Im Falle einer Kollision ändere ich einfach die Farbwerte der kollidierten Box auf rot (255, 0, 0).

Ich bin ziemlich sicher, dass das Problem in der for-Schleife und if-Anweisung Block liegt. Ich werde den Rest des Skripts am unteren Rand veröffentlichen. Dies ist der Block:

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 

ganze Skript:

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 

Also meine Vermutung ist, dass das "i" Iterator in der for-Schleife erfasst nur eine Menge an Kollisionen ist. Da ich den Überblick behalten muß, was Tisch ist das, was ich mit dieser Idee kam:

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 

würde ich eine ID (Nummer) in der Funktion übergeben und es auf den Tisch zuweisen. Auf diese Weise konnte ich jede Tabelle einzeln zugreifen wie folgt aus:

previousBox = cols[i].other[id].name 

Aber das Problem ist, dass ich diesen Fehler: Versuch Index global ‚block‘ (ein Null-Wert)

Dies ist, wo das Problem Lügen ich denke:

Wie übergebe ich die Tabelle mit korrektem Index an die Funktion? Ich bin mir ziemlich sicher, dass es einen besseren Weg gibt, dies zu tun.

Antwort

0

Ich denke, Ihr Problem ist, dass Sie Variablen direkt im for Schleife zu initialisieren, da bei jeder Iteration die Werte von previousColors und previousBox löschen, versuchen Sie dies vor der Schleife zu tun:

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 
+0

Ja, scheint der Fall sein. Ich werde bestätigen, sobald ich nach Hause komme, danke! – kepler425b

+0

@sakvojaz Sie sind willkommen. Wenn meine Antwort wirklich nützlich für Sie ist, akzeptieren Sie sie bitte. –

Verwandte Themen