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.

1
2
3
4
5
6
7
8
--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

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

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

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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

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.

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.

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.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--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
views:1591 update:2012/1/3 13:02:13
corona forums © 2003-2011