# Sprite Lists Problem

I am extremely new at codea and lua and I am having trouble creating sprites and being able to remove them if something collides. I do have coding experience in Python/Pygame but the difference is that there is no sprite list inside codea (unless you make one). Can anyone explain how you input sprites in a group and remove the sprite when touched? Thanks.

@Crazypkr1099 Here’s a very simple way to move sprites and make them disappear when they collide. The collision isn’t real, just a math compare, and the sprites don’t really disappear, they just don’t get drawn when they’re close.

``````
function setup()
x1=WIDTH/2
y1=HEIGHT-100
x2=WIDTH/2
y2=100
end

function draw()
background(40,40,50)
if math.abs(y1-y2)>70 then
sprite("Planet Cute:Character Boy",x1,y1)
sprite("Planet Cute:Character Pink Girl",x2,y2)
end
y1=y1-1
y2=y2+1
end

``````

@dave1707 thanks, but my other question was how to spawn multiple sprites and make each disappear. I hope you understand @Crazypkr1099 I’ll give you an example shortly.

Oh and my last question, are they any great video tutorials or written tutorials on lua and codea? Again, sorry for double posting :\

@Crazypkr1099 Tap the screen to show a sprite. Tap the sprite to make it disappear. Yes, there are a lot of tutorials created by @Ignatz. See the link below.

``````http://coolcodea.wordpress.com/2013/06/19/index-of-posts/
``````

sample code modified from another discussion.

``````
displayMode(FULLSCREEN)

function setup()
rectMode(CENTER)
tt={}   -- create a table
end

function draw()
background(40,40,50)
fill(53, 255, 0, 255)
for a,b in pairs(tt) do -- loop thru table
sprite("Planet Cute:Character Pink Girl",b.x-20,b.y-20)   -- draw sprites
end
end

function touched(t)
if t.state==ENDED then  -- check touch dtate
for a,b in pairs(tt) do -- loop thru table
if t.x > b.x -40 and t.x < b.x +40 then
if t.y > b.y -40 and t.y < b.y + 40 then
table.remove(tt,a)  -- sprite touched, remove it
return  -- dont loop anymore
end
end
end
table.insert(tt,vec2(t.x,t.y))  -- save x,y values in table
end
end

``````

Thanks @dave1707 So let me get this straight…

tt is creating a table…
t is touch
t.x is touch.x
t.y is touch.y

What is a and b?

@Crazypkr1099 a and b are just variable names. I’m kind of lazy when it comes to variable names, so I just pick letters sometimes. The purpose of that “for” loop is to iterate thru the table tt. The variable “a” is the offset number in the table. The variable “b” is the value at that offset in the table. When items were inserted into the table, they went in as vec2 variables. That’s 2 values per entry, an “x” and a “y” screen position. If there’s only one value per entry, then “b” would be that value. If there are 2 entries (vec2), then the first value would be “b.x” and the second “b.y” . You could also have 3 (vec3) and 4 (vec4) entries. To use 3 entries, then you would use b.x, b.y, and b.z . For 4 entries, b.x, b.y, b.z, and b.w . Of course variables a and b could be more meaningful names.

@dave1707 Tell you the truth, I don’t understand this any written explanation for this? If not, thank you so much for trying to explain this

@Crazypkr1099 A lot of that is just standard Lua code. There are free books on Lua, you just have to search for them.

Thank you so much @dave1707 I now understand how to make loops! This little program demonstrates creating and removing sprites !

``````
Enemy = class()
enemies = {}
function Enemy:init(x,y)
self.x = x
self.y = y
table.insert(enemies,vec2(self.x,self.y))
print (self.x)

end

function Enemy:draw()
for a,b in pairs(enemies) do
sprite("Documents:UFO",b.x,b.y)
end
end

function Enemy:move()
for a,b in pairs(enemies) do
b.x = b.x + 1
if b.x > WIDTH then
table.remove(enemies,a)
print (#enemies)
end
end

end

function Enemy:touched(touch)
-- Codea does not automatically call this method
end

``````