images and table problem

I am working on an app where students are given 4 directional arrows to select. They can select as many arrows as often as they like up to a sequence of 20. When they press “Go” the character moves according to the arrows chosen. My problem is this…

Each time a student selects an arrow, I show an image of it in a specific location. There is a clear button that erases the images so they can do it again. To this point, things work fine, but when an arrow is chosen after the images have been cleared I get an error “attempt to call global ‘image1’ (a table value)” I think it is how I am clearing the images, but I’m not sure. Snippets of code are pasted below.

--check the counter and call the image  
function checkForwardFunction()  
if turn1 == null then  
clear = true  
turn1 = "forward" counter=counter+1,image1() else if turn2 == null then  
turn2 = "forward" counter=counter+1,image2()   
end  
end  

Here is the table and how I am calling the image to the screen

imageTable = {}  
  
imageTable[1] = "images/goForward.png"  
imageTable[2] = "images/turnRight.png"  
imageTable[3] = "images/goBackward.png"  
imageTable[4] = "images/turnLeft.png"  
imageTable[5] = "images/invisibleSquare.png"  
  
function image1()  
  
imageGroup = display.newGroup()  
if  
turn1 == "forward" then  
 image1 = display.newImage(imageTable[1])  
 image1.x= 50; image1.y = 30  
 imageGroup:insert(image1)  
 print(imageGroup)  
print("first image added")  
else if turn1 == "right" then  
image1 = display.newImage(imageTable[2])   
image1.x= 50; image1.y = 30  
imageGroup:insert(image1)  
else if turn1 == "backward" then   
image1 = display.newImage(imageTable[3])  
image1.x= 50; image1.y = 30  
imageGroup:insert(image1)  
else if turn1 == "left" then  
image1 = display.newImage(imageTable[4])  
image1.x= 50; image1.y = 30   
imageGroup:insert(image1) else if  
turn1 == nil then  
image1 = display.newImage(imageTable[5])  
end  
end  
end  
end  
end  
end  

Here is how I am clearing the images from the screen

function removeImages()  
print (imageGroup)  
imageGroup:removeSelf()  
imageGroup = nil  
end  

any help would be appreciated.

Thanks [import]uid: 48122 topic_id: 19810 reply_id: 319810[/import]

The case where turn1 is nil gives a new image which is not part of your display group:

imageGroup:insert(image1) else if  
turn1 == nil then  
image1 = display.newImage(imageTable[5])  

if you lose the imageGroup:insert(image1) rows, and replace all

display.newImage(imageTable[2])

by

display.newImage(imageGroup, imageTable[2]) 

then it will work as you expect.

Clearing the images at the end then becomes:

display.remove(imageGroup)

which releases the images, any listeners, and the group, all in one shot
[import]uid: 108660 topic_id: 19810 reply_id: 76817[/import]

Hi Jeff472,

Thanks for the advice. The code you suggest is a bit cleaner, but the issue did not change. I get the exact same error as before. [import]uid: 48122 topic_id: 19810 reply_id: 76826[/import]

its seems that you removing things from display group by using “group:removeSelf()”, thats fine
but, you’re nilling it by “group = nil” and thats your problem
[lua]local t = {
img = “replay.png”
}
local group = display.newGroup()

local image = display.newImageRect(t.img,50,50)
image.x = 150
image.y = 150
group:insert(image)

for i=1,group.numChildren do
print(group[i])
end

display.remove(group)
group = nil

if group then
print(“there’s a group!”)
else print(“no group, nothing there”)
end[/lua] [import]uid: 16142 topic_id: 19810 reply_id: 76833[/import]

Thanks. My problem must be somewhere else, I ran it the way you suggest, and the group is no longer nil, but the issue has not changed. I tried simply removing the first image individually and the problem didn’t change then either. [import]uid: 48122 topic_id: 19810 reply_id: 76839[/import]

Your code is utterly confusing. I’m having trouble understanding what you are trying to do. Why do you have a function image1 that then sets image1 equal to a new image?

There’s probably a much better way to approach this solution. [import]uid: 71767 topic_id: 19810 reply_id: 76842[/import]

There probably is a better way. Originally the function image1 was in a module, but when I noticed the problem I put all of the code in a single file for testing purposes. [import]uid: 48122 topic_id: 19810 reply_id: 76854[/import]

It may not help that the module has the same name as the objects you are creating.

Maybe the problem is simpler than this: you do after all say
‘when an arrow is chosen after the images have been cleared…’
Is it as simple as there is no arrow to show?
The code doesn’t make a lot of sense to me either, maybe you could say what is supposed to happen in a different way?

For instance, inside the image1() function as it stands, you are using a more global variable of ‘turn1’ to determine which arrow to place.
That could and should be a parameter to the function.
All placed arrows are going to the same location: that may be intended, but from your first description I was expecting them to be side by side, like a sentence of movements.
This would allow the pupil to see the commands build up, e.g.
^^^>>>vv>>>>>^^>>vvvvv
I would expect these to correspond with an array of ‘turns’
The turns array only needs to hold numbers representing the direction.
Playing back the movements is a question of using a for loop.

But the code that says ‘check the counter and call the image’ looks a little like you have a variable for turn1, another for turn2 and so on.

Anyway, here is a tidier version of the image1() function , with names changed to avoid any collisions:

--create the group outside of the function so that it does not get  
-- re-dimmed every time the function  
-- is called  
  
imageGroup = display.newGroup()  
  
function AddImage(theTurn)  
--pass turn1 as a parameter  
--inside the function we can refer to that as theTurn  
  
local theImage = 0  
  
--allocate the image  
--(I really wish LUA had a 'select / case' construct' )  
  
if theTurn == "forward" then theImage = 1 end  
if theTurn == "right" then theImage = 2 end  
if theTurn == "backward" then theImage = 3 end  
if theTurn == "left" then theImage = 4 end  
if theTurn == nil then theImage = 5 end  
  
local newPic = display.newImage(imageGroup, imageTable[theImage])  
newPic.x= 50; newPic.y = 30  
  
end  

instead of this:

turn1 = “forward” counter=counter+1,image1()

use
counter = counter+1
Addimage(“forward”)

Hope that helps…
[import]uid: 108660 topic_id: 19810 reply_id: 76866[/import]

If you send me your entire code base, I’ll fix the problem for you, if I can. [import]uid: 71767 topic_id: 19810 reply_id: 77188[/import]

Hi Dejay, thanks for the offs, but I finally got it. @Jeff - Thank you so much for that snippet of code! It solved most of the issue and also gave me an example to study to see why yours worked so much better than mine. I’m not very strong at coding yet and examples like that go a long way in helping me improve my skills.

Much appreciated.

[import]uid: 48122 topic_id: 19810 reply_id: 77288[/import]

there’s even better way to do this things
[lua]local imageTable = {
forward = “forward.png”
}
local group = display.newGroup()

local function makeTurn(turn)
local turn = turn

local img = display.newImage(group, imageTable[turn])

end

makeTurn(“forward”)[/lua]
much easier and cleaner code [import]uid: 16142 topic_id: 19810 reply_id: 77290[/import]