Well I’ve been trying for days now and every time I get no errors nothing appears, I just reall want to know how to get it so that when you touch the screen a sprite appears , and when you let go it stays there
Here is one way to do it, but there are a lot of other ways. Those will depend on what you want to do with the sprite. Just drag your finger around the screen.
function setup()
end
function draw()
background(40, 40, 50)
sprite("Planet Cute:Character Boy",CurrentTouch.x,CurrentTouch.y)
end
Omygosh, wow thanks so much I can’t believe i never got that
So lets say when I tap on the screen I want a bullet to appear and go up, but not from where I touch but when I touch from a spaceships position
displayMode(FULLSCREEN)
function setup()
missle=false
end
function draw()
background(40, 40, 50)
sprite("Space Art:Red Ship",WIDTH/2,300)
if missle then
sprite("Space Art:Red Bullet",WIDTH/2,y)
y = y + 5
if y>HEIGHT then
missle=false
end
end
end
function touched(t)
if t.state==BEGAN then
missle=true
y=350
end
end
I’ve got the same idea in my code and just tested yours and I get the same problem, what happens is the bullet disappears every time you touch
I know it’s because the bullet has the same y value every time, so could you help me work around this?
That’s when you start using tables. Tap the screen as many times as you like.
displayMode(FULLSCREEN)
function setup()
mtab={} -- defines a table for the Missles
end
function draw()
background(40, 40, 50)
sprite("Space Art:Red Ship",WIDTH/2,300)
if #mtab then -- are there any missles in the table
for z=1,#mtab do -- loop thru all Missles in the table
sprite("Space Art:Red Bullet",WIDTH/2,mtab[z]) -- draw at missle y value
mtab[z] = mtab[z] + 5 -- add 5 to each missle y value
if mtab[z]>HEIGHT then -- check if missle is off the screen at the top
table.remove(mtab,z) -- remove that missle from the table
break -- missle removed, exit loop
end
end
end
end
function touched(t)
if t.state==BEGAN then
table.insert(mtab,350) -- screen tapped, add a missle to the table
end
end
Oh wow that’s confusing I’ve never used tables, before ever!, I don’t want to sound like a pain but could you help me out and put some notes in the code (noob friendly, explaining how tabs work)?
See the added comments in the above code.
Thank you very much for everything, but what’s the 350 mean beside Mtab in touched function?
And the # what are they for?
The spaceship is drawn at x,y values width/2,300. So when I put the missle in the table I set it’s initial y position to 350. I start it 50 pixels above where I draw the ship. So 350 gets put in the table to start and in the draw routine I add 5 to that value each time which moves the missle. The # is used to get the number of items in a table.
Ok and what’s the z for in the for loop, (I know I’m new)
The z is just a variable name I selected to use in the for loop. I could have used any name, but I selected z. To better understand all of the different ways of using a for loop, doing a search for “Lua for loops” will give you a better explanation than I can give you. Everyone was new at one time or another, so don’t hesitate to ask questions. I have no problem answering your questions, in fact I like helping, but you’ll understand things better if you try to figure things out on your own before asking. So I’m not sure if I’m helping you or hindering you.
You are doing an amazing job at helping me thank you, and ya I understand the for loops thing now
Keep learning and playing and you’ll be spitting out code without even thinking about it in no time.
.@dave1707 excellent! That’s given me a poke towards a better way of handling my touches.
As an aside, as far as I understand it the
If #mtab then
line doesn’t contribute anything as it is always true ( even when there is nothing in the table the value resolves to 0 which is different from a null/false). Am I missing something more subtle?
Good catch. I put it in place of “if missle” then I added the for loop. When I ran the program and it worked, I didn’t go back and check the code. I guess I make mistakes coding and watching TV at the same time.
i found this awesome tutorial on tables http://lua-users.org/wiki/TablesTutorial and now i understand completely whats happening, thanks for all the help!