简体   繁体   中英

In Corona SDK, how can I convert my local variable into a global variable?

I would like to convert local line into a global variable so that I can refer to it later on. This is my code so far:

local physics = require "physics"
physics.start()

local line
lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 0

local function distanceBetween(x1, y1, x2, y2)
local dist_x = x2 - x1
local dist_y = y2 - y1
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end

local function drawLine(e)
  if(e.phase == "began") then
    if(line) then
        lineGroup:remove(1)
        line = nil
    end
    prevX = e.x
    prevY = e.y
    isDrawing = true
  elseif(e.phase == "moved") then
    local distance = distanceBetween(prevX, prevY, e.x, e.y)
    if(isDrawing and distance < 100) then
        if(line) then lineGroup:remove(1) end
        line = display.newLine(prevX, prevY, e.x, e.y)
        line:setStrokeColor( 0.5,0,1 )
        line.strokeWidth = 5

        local dist_x = e.x - prevX
        local dist_y = e.y - prevY
        physics.addBody(line, "static", { density = 1,
                                          friction = 0.5,
                                          bounce = 2,
                                          shape = {0,     0, dist_x, dist_y, 0, 0} } )
        lineGroup:insert(line)
    end
  elseif(e.phase == "ended") then
    isDrawing = false
  end
end

Runtime:addEventListener("touch",drawLine)

Whenever I try to refer to line in this next function, I get an error message saying:

attempt to index global 'line'(a nil value):

function onCollision(e)
  audio.play(bounceSnd)
  score.text = tostring(tonumber(score.text) + 1)
  score.x = 300
end

gameListeners('add')
end

function gameListeners(action)
  if(action == 'add') then        
    line:addEventListener( 'collision', onCollision )
  else
     line:addEventListener( 'collision', onCollision)
  end
end

If anyone could help, I would be extremely grateful.

You could add the eventlistener when you create the line, not from a seperated function. If you need additional control for when the line responds to collisions and when not, you can probably do this via some flags for the physics body. (I never used the physics module)

That looks like you don't have a mydata.lua file anywhere. I know in the examples they use that reference a lot, but you have to actually create a mydata.lua file if you want to use that. It's a technique they use to have a global object that can be referenced in different files. For example:

mydata.lua

local M = {}
M.someVariable = WhateverYouWant

return M

Then in any file you want to have access to mydata:

local mydata = require("mydata")

Then you can use it in that file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM