Author Topic: Attempt to index  (Read 3292 times)

johnno56

  • Guest
Attempt to index
« on: June 06, 2014, 09:52:11 AM »
I am trying to run a Asteroids game and the IDE issues an error message. lua:22: attempt to index global 'physics' (a nil value)

This is the listing.

(This is NOT my coding.  All I did was copy and paste.)

J

Code: [Select]
--------------------------------------------------------------------------------

-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
-- Copyright (C) 2011 Ansca Inc. All Rights Reserved.

--------------------------------------------------------------------------------

require "physics"

function main()

setUpPhysics()
createWalls()
createBricks()
createBall()
createPaddle()
startGame()

end

function setUpPhysics()
physics.start()
-- physics.setDrawMode("hybrid")
physics.setGravity(0,0)
end


function createPaddle()

local paddleWidth = 100
local paddleHeight = 10

local paddle = display.newRect( display.contentWidth / 2 - paddleWidth / 2, display.contentHeight - 50, paddleWidth, paddleHeight )
physics.addBody(paddle, "static", {friction=0, bounce=1})

local  movePaddle = function(event)
paddle.x = event.x
end

Runtime:addEventListener("touch", movePaddle)

end


function createBall()

local ballRadius = 10

ball = display.newCircle( display.contentWidth / 2, display.contentHeight / 2, ballRadius )
physics.addBody(ball, "dynamic", {friction=0, bounce = 1, radius=ballRadius})

ball.collision = function(self, event)
if(event.phase == "ended") then

if(event.other.type == "destructible") then
event.other:removeSelf()
end

if(event.other.type == "bottomWall") then

self:removeSelf()

local onTimerComplete = function(event)
createBall()
startGame()
end

timer.performWithDelay(500, onTimerComplete , 1)
end
end
end

ball:addEventListener("collision", ball)
end

function startGame()
ball:setLinearVelocity(75, 150)
end


function createBricks()

local brickWidth = 40
local brickHeight = 20

local numOfRows = 4
local numOfCols = 6

local topLeft = {x= display.contentWidth / 2 - (brickWidth * numOfCols ) / 2, y= 50}

local row
local col

for row = 0, numOfRows - 1 do
for col = 0, numOfCols - 1 do

-- Create a brick
local brick = display.newRect( topLeft.x + (col * brickWidth), topLeft.y + (row * brickHeight), brickWidth, brickHeight )
brick:setFillColor(math.random(50, 255), math.random(50, 255), math.random(50, 255), 255)
brick.type = "destructible"

physics.addBody(brick, "static", {friction=0, bounce = 1})
end
end
end


function createWalls()

local wallThickness = 10

-- Left wall
local wall = display.newRect( 0, 0, wallThickness, display.contentHeight )
physics.addBody(wall, "static", {friction=0, bounce = 1})

-- Top wall
wall = display.newRect(0,0, display.contentWidth, wallThickness)
physics.addBody(wall, "static", {friction=0, bounce = 1})

-- Right wall
wall = display.newRect(display.contentWidth - wallThickness, 0, wallThickness, display.contentHeight)
physics.addBody(wall, "static", {friction=0, bounce = 1})

-- Bottom wall
wall = display.newRect(0, display.contentHeight - wallThickness, display.contentWidth, wallThickness)
physics.addBody(wall, "static", {friction=0, bounce = 1})

wall.type = "bottomWall"
end


main()

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Attempt to index
« Reply #1 on: June 06, 2014, 01:46:42 PM »
Well, this is actually no EGSL code. I would suggest to try it with Corona SDK for which this code was written ...  ;)

johnno56

  • Guest
Re: Attempt to index
« Reply #2 on: June 06, 2014, 11:37:13 PM »
My apologies. I assumed that egsl would run lua script. I had no way of knowing that this script was application specific. Again. My apologies.

j

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Attempt to index
« Reply #3 on: June 07, 2014, 07:51:43 AM »
You are welcome. Actually EGSL does run Lua scripts as does Corona SDK. But there are some specific additions which only work for either of them. Only "pure" Lua works for both.