Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Error: Attempt to index global
Started by bsi.lukas Feb 06 2020 10:51 AM

4 replies to this topic

Best Answer SGS , 07 February 2020 - 05:24 PM

The correct logic for a tap handler to do this would be

local tile1, tile2 = nil, nil

local function clicked( event )
    local self = event.target
    if self ~= tile1 then
        if not tile1 then 
            --store first tile tapped
            tile1 = self 
        elseif not tile2 then 
            --store second tile tapped
            tile2 = self 
        end
            
        if tile1 and tile2 then
            if tile1.name == tile2.name then
                --match found
                tile1.isVisible = false
                tile2.isVisible = false
                tile1 = nil; tile2 = nil
            else
                --no match so reset our taps
                tile1 = nil; tile2 = nil
            end
        end
    end
    return true
end

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

bsi.lukas

[GLOBAL: userInfoPane.html]
bsi.lukas
  • Observer

  • 2 posts
  • Corona SDK

Hello! Could someone help me?

 

I have four images where two are the same (memory game) and I would like to remove them from the scene when the same images are clicked, so I did this function.

local function clicked( event )
		if (click1 == nil) then
			click1 = event.target.id
			name1 = event.target.name
		else
			click2 = event.target.id
			name2 = event.target.name
			print ("Tap: " .. click2)
				if (click1 == click2) and (name1 ~= name2) then 
					print("Equals")
					click1.isVisible = false -- the error is on this line.
                                        click2.isVisible = false
					name1 = nil 
                                        name2 = nil
                                        click1 = nil
                                        click2 = nil
			else
				print ("Error!")
				nome1 = nil 
                                nome2 = nil 
                                click1 = nil 
                                click2 = nil
			end
		end
    	return true
	end


But I get an error when the clicks happen in the same pairs:
C:\memorygame\level1_1.lua: 54: attempt to index global 'click1' (a string value)
 
click1.isVisible = false -- the error is on this line.


[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,673 posts
  • Corona SDK

I'm afraid there are many issues with that code, chief among them that you can't have:

var1 = nil var2 = nil ...

those assignments should be one per line.

 

You also seem to spell the variables differently as you go along.  click1 versus  clique1... is is hard to tell though, because we don't have enough context into where the variables are used and how they are filled to give you proper assistance.

 

Also... you've completely ignored the phases of the touch event.  That won't work.  Unless you're using the tap event?  Again.  We need more context.

 

On a side note, I have a memory game example here:

https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/ICanMakeThat/Memory%20Flipper.zip

 

 

The game is quite old, but probably still useful.  It was developed for these videos:

https://www.youtube.com/watch?v=poEntIBn4Lo&feature=emb_logo

 

https://www.youtube.com/watch?v=tMDOwTPehfk



[TOPIC: post.html]
#3

bsi.lukas

[GLOBAL: userInfoPane.html]
bsi.lukas
  • Observer

  • 2 posts
  • Corona SDK

I'm afraid there are many issues with that code, chief among them that you can't have:

var1 = nil var2 = nil ...

those assignments should be one per line.

 

You also seem to spell the variables differently as you go along.  click1 versus  clique1... is is hard to tell though, because we don't have enough context into where the variables are used and how they are filled to give you proper assistance.

 

Also... you've completely ignored the phases of the touch event.  That won't work.  Unless you're using the tap event?  Again.  We need more context.

 

On a side note, I have a memory game example here:

https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/ICanMakeThat/Memory%20Flipper.zip

 

 

The game is quite old, but probably still useful.  It was developed for these videos:

https://www.youtube.com/watch?v=poEntIBn4Lo&feature=emb_logo

 

https://www.youtube.com/watch?v=tMDOwTPehfk

 

There were some typos in the publication. I just fixed it! Can you help me?



[TOPIC: post.html]
#4

pixec

[GLOBAL: userInfoPane.html]
pixec
  • Contributor

  • 222 posts
  • Corona SDK

.sVisible is a display object value, but you set click1 to event.target.id, which is prolly a string (or number). So it crashes because you are trying to access a table parameter within a string.

[TOPIC: post.html]
#5

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,219 posts
  • Corona SDK

  Best Answer

The correct logic for a tap handler to do this would be

local tile1, tile2 = nil, nil

local function clicked( event )
    local self = event.target
    if self ~= tile1 then
        if not tile1 then 
            --store first tile tapped
            tile1 = self 
        elseif not tile2 then 
            --store second tile tapped
            tile2 = self 
        end
            
        if tile1 and tile2 then
            if tile1.name == tile2.name then
                --match found
                tile1.isVisible = false
                tile2.isVisible = false
                tile1 = nil; tile2 = nil
            else
                --no match so reset our taps
                tile1 = nil; tile2 = nil
            end
        end
    end
    return true
end

  • dodi_games likes this


[topic_controls]
[/topic_controls]