#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# ontouch function

edited November 15 Posts: 15

Hello, my Codea account was deleted for no apparent reason, I had asked a question then like 30 minutes later I come on to sign in and my account is unknown, I’m on same email and password right now, so it was in fact removed.

• Posts: 7,923

``````displayMode(FULLSCREEN)

function setup()
x,y=WIDTH/2,HEIGHT/2
xv,yv=0,0
end

function draw()
background(40, 40, 50)
sprite("Space Art:UFO",x,y)
noStroke()
rect(WIDTH-150,100,100,50)
if laz then
stroke(49, 178, 239, 118)
strokeWidth(12)
line(x,y,x,y+WIDTH)
stroke(255, 204, 0, 255)
strokeWidth(4)
line(x,y,x,y+WIDTH)
end
end

function touched(t)
if t.state==BEGAN then
if t.x>WIDTH-150 and t.x<WIDTH-50 and t.y>100 and t.y<150 then
laz=true
end
elseif t.state==MOVING then
x=x+t.deltaX
y=y+t.deltaY
elseif t.state==ENDED then
laz=false
end
end
``````
• Posts: 15

Thank you

• Posts: 15

Where is the option where X and Y where I would change where the laser is coming from on the ship?

• Posts: 7,923

If you don’t want the laser coming from the center, then you can alter the x value in the `if laz` routine in the function draw.

• edited November 15 Posts: 15

Is there a way to add like a delay? Because If I swipe to move the ship over constantly, the laser ends, resetting it so could I do like:

sleep(500)
laz=false

Or something Like that?

• Posts: 7,923

You can move the ship with one hand and shoot the laser with the other.

• Posts: 7,923

@Valurim Here’s something you can play around with.

``````displayMode(FULLSCREEN)

function setup()
x,y=WIDTH/2,HEIGHT/2
x1,y1=0,0
x1v,y1v=2,4
end

function draw()
background(40, 40, 50)
sprite("Space Art:UFO",x,y)
noStroke()
rect(WIDTH-150,100,100,50)
if laz then
stroke(49, 178, 239, 118)
strokeWidth(12)
line(x+45,y,x1,y1)
line(x-45,y,x1,y1)
stroke(255, 204, 0, 255)
strokeWidth(4)
line(x+45,y,x1,y1)
line(x-45,y,x1,y1)
end
sprite("Space Art:Red Ship Icon",x1,y1)
x1=x1+x1v
y1=y1+y1v
if x1>WIDTH or x1<0 then
x1v=x1v*-1
end
if y1>HEIGHT or y1<0 then
y1v=y1v*-1
end
end

function touched(t)
if t.state==BEGAN then
if t.x>WIDTH-150 and t.x<WIDTH-50 and t.y>100 and t.y<150 then
laz=true
end
elseif t.state==MOVING then
x=x+t.deltaX
y=y+t.deltaY
elseif t.state==ENDED then
laz=false
end
end
``````
• edited November 15 Posts: 15

Is there like an ontouch function or something?
i.e
if x,y touch x1,y,1 then
sprite(explosion)
end

I’m trying to make the lazer explode the ship when it touches it

• Posts: 7,923

@Valurim When drawing the lines, x,y will always touch x1,y1 since a line has a starting position (x,y) and an ending position (x1,y1). There’s a raycast function that will return a value if a line drawn from one position will pass thru another position. But that involves using the physics functions which you don’t know about yet.

Instead of using a laser, shooting photon torpedoes might work better. As the torpedo moves across the screen you can compare the torpedo position to the ship position. If they’re within a certain distance of each other, the ship can explode.

• Posts: 7,923

@Valurim Heres an example using a torpedo. Move the ufo with one hand and fire a torpedo with the other. When the torpedo is within 15 pixels of the ship, an explosion will happen. This is simple code with the torpedo always going straight up. More code can be added to fire the torpedo in any direction, but that can be at a later time.

``````displayMode(FULLSCREEN)

function setup()
x,y=WIDTH/2,HEIGHT/2
x1,y1=0,0
x1v,y1v=1,3
px,py=0,0
pxv,pyv=0,8
end

function draw()
background(40, 40, 50)
sprite("Space Art:UFO",x,y)
noStroke()
rect(WIDTH-150,100,100,50)
sprite("Space Art:Red Ship Icon",x1,y1)
x1=x1+x1v
y1=y1+y1v
if x1>WIDTH or x1<0 then
x1v=x1v*-1
end
if y1>HEIGHT or y1<0 then
y1v=y1v*-1
end
sprite("Space Art:Green Explosion",px,py,30)
if px>0 or py>0 then
px=px+pxv
py=py+pyv
end
if math.abs(px-x1)<15 and math.abs(py-y1)<15 then
sprite("Tyrian Remastered:Explosion Huge",x1,y1)
end
end

function touched(t)
if t.state==BEGAN then
if t.x>WIDTH-150 and t.x<WIDTH-50 and t.y>100 and t.y<150 then
px=x
py=y
end
elseif t.state==MOVING then
x=x+t.deltaX
y=y+t.deltaY
end
end
``````
• Posts: 7,923

@Valurim Heres another version for you to try. Move the ufo up or drow and press the button to shoot. Try to hit the clouds coming from the right.

``````displayMode(FULLSCREEN)

function setup()
x,y=100,HEIGHT/2
x1,y1=100,HEIGHT/2
x1v,y1v=1,3
px,py=0,0
pxv,pyv=8,0
mis={}
ships={}
for z=1,20 do
table.insert(ships,vec2(math.random(WIDTH,WIDTH+2000),math.random(HEIGHT)))
end
end

function draw()
background(40, 40, 50)
sprite("Space Art:UFO",x,y)
rect(WIDTH-150,150,100,50)
for a,b in pairs(ships) do
sprite("Space Art:Cloudy Nebula",b.x,b.y,50)
b.x=b.x-3
if b.x<-20 then
table.remove(ships,a)
end
end
for a,b in pairs(mis) do
b.x=b.x+pxv
sprite("Space Art:Green Explosion",b.x,b.y,30)
if math.abs(px-x1)<15 and math.abs(py-y1)<15 then
sprite("Tyrian Remastered:Explosion Huge",x1,y1)
end
if b.x>WIDTH+50 then
table.remove(mis,a)
end
for c,d in pairs(ships) do
if math.abs(b.x-d.x)<20 and math.abs(b.y-d.y)<20 then
table.remove(ships,c)
table.remove(mis,a)
end
end
end
if #ships<20 then
table.insert(ships,vec2(math.random(WIDTH,WIDTH+2000),math.random(HEIGHT)))
end
end

function touched(t)
if t.state==BEGAN then
if t.x>WIDTH-150 and t.x<WIDTH-50 and t.y>150 and t.y<200 then
table.insert(mis,vec2(x,y))
end
elseif t.state==MOVING then
x=x+t.deltaX
y=y+t.deltaY
end
end
``````
• Posts: 15

How do you rotate a sprite

• Posts: 7,923

Here’s where I’m going to let you figure out things for yourself so you don’t have to wait on me to answer. In the upper right corner of this forum, above New Discussion is a search box. Type `sprite rotation` there and press the magnifying glass. That will bring up a bunch of discussions that give example of rotating a sprite. You can also find answers to any question you might have. If you can’t find what you’re looking for or you don’t understand something, let me know. I’m not trying to not answer your questions, I’m letting you find the answers so you’re not relying on or waiting for us to answer. Again, if you can’t find an answer or you don’t understand it, let me know and I’ll help. Don’t get discouraged if you can’t find what you want. There’s a lot to learn, but it’s well worth it.

• Posts: 7,923

@Valurim If you just need to flip a sprite as in up/down, right/left, you can try this. The last 2 numbers in each sprite line alters the size and putting a negative sign flips it.

``````displayMode(FULLSCREEN)

function draw()
background(40, 40, 50)
sprite("Planet Cute:Character Horn Girl",100,400,200,200)  -- normal
sprite("Planet Cute:Character Horn Girl",300,400,-200,200)  -- flip on x
sprite("Planet Cute:Character Horn Girl",300,200,200,-200)  -- flip on y
end
``````
• edited November 16 Posts: 15
``````displayMode(FULLSCREEN)

function setup()
x,y=100,HEIGHT/2
x1,y1=100,HEIGHT/2
x1v,y1v=1,3
px,py=0,0
pxv,pyv=8,0
mis={}
ships={}
for z=1,20 do
table.insert(ships,vec2(math.random(WIDTH,WIDTH+2000),math.random(HEIGHT)))
end
end

function draw()
background(40, 40, 50)
spritePos = vec2( WIDTH/2, HEIGHT/2 )
pushMatrix()
translate(x,y)
rotate(270)
sprite("Tyrian Remastered:Boss D",x, y)
popMatrix()

popMatrix()
rect(WIDTH-150,150,100,50)
for a,b in pairs(ships) do
sprite("Space Art:Cloudy Nebula",b.x,b.y,50)
b.x=b.x-3
if b.x<-20 then
table.remove(ships,a)
end
end
for a,b in pairs(mis) do
b.x=b.x+pxv
sprite("Space Art:Green Explosion",b.x,b.y,30)
if math.abs(px-x1)<15 and math.abs(py-y1)<15 then
sprite("Tyrian Remastered:Explosion Huge",x1,y1)
end
if b.x>WIDTH+50 then
table.remove(mis,a)
end
for c,d in pairs(ships) do
if math.abs(b.x-d.x)<20 and math.abs(b.y-d.y)<20 then
table.remove(ships,c)
table.remove(mis,a)
end
end
end
if #ships<20 then
table.insert(ships,vec2(math.random(WIDTH,WIDTH+2000),math.random(HEIGHT)))
end
end

function touched(t)
if t.state==BEGAN then
if t.x>WIDTH-150 and t.x<WIDTH-50 and t.y>150 and t.y<200 then
table.insert(mis,vec2(x,y))
end
elseif t.state==MOVING then
x=x+t.deltaX
y=y+t.deltaY
end
end
``````

rect(WIDTH-150,100,100,50)
ellipse( 923, 78, 15, 15)
ellipse( 950, 600, 15, 15)
ellipse( 50, 171, 15, 15)
ellipse( 350, 451, 15, 15)
ellipse(836, 250, 15, 15)
ellipse(750, 769, 15, 15)
ellipse( 75, 600, 15, 15)`
So I tried another rotation format, and there is a weird glitch where sliding up brings it vertical(right) down, vice versa. The torpedos are also several ways apart from ship.

• edited November 16 Posts: 7,923

@Valurim Any time you post code, you need to put ~~~ (3 tildes) on a line before and after the code. I added them to your code above. When you use translate, the translate statement has the values of where the object is placed. Then the values of the actual object become 0,0 . See the updated code below. The translate function rotates the sprite around the x,y value. By giving the sprite some values, then you’re moving the sprite away from the point of rotation. If you want to see what happens, just create another program with just the code for translate. Start with what’s below and them alter the 0,0 values to see what really happens.

I’m not sure what you were doing with the code that I didn’t include in the blue area.

``````    pushMatrix()
translate(x,y)
rotate(270)
sprite("Tyrian Remastered:Boss D",0,0)
popMatrix()
``````
• edited November 19 Posts: 15

Hi, so I was looking at this code and started to wonder, is there a way I can make the state last, almost as if drawing? Because just touch by itself doesn’t do anything, could you help me here:

``````function draw()
background(0,0,0,255)
fill(255,0,0,255)
if CurrentTouch.state == ENDED then
fill(0, 0, 0, 255)
end
ellipse(CurrentTouch.x,CurrentTouch.y, 100)
end
``````
• Posts: 7,923

@Valurim I'm not sure what you want to do. What you’re doing above is drawing a red circle when you’re touching the screen, but when you lift your finger, you’re changing the circle color to black which matches the background, so you can’t see it. Try changing the background color to something other than black and you’ll see what I’m saying.

• Posts: 15

Yes I know, that’s so when you draw it disappears when you lift, making it more natural. I’m trying to make it so a trail stays behind it, like drawing. So as you drag your finger across it “paints.” Changing the ENDED fill to red won’t do that, that just makes the red ball stay wherever you left off.

• edited November 19 Posts: 7,923

Try this.

``````displayMode(FULLSCREEN) -- show the full screen

function setup()
backingMode(RETAINED)   -- set to retain anything drawn on the screen
sx,sy=0,0   -- initialize the start x,y variables to 0,0
end

function draw()
stroke(255, 0, 0, 255)  -- set the line color to red
strokeWidth(2)          -- set the line width to 2 pixels
if sx>0 or sy>0 then    -- only draw the line when sx or sy > 0
line(sx,sy,cx,cy)   -- draw a line from sx,sy (start) to cx,cy (end)
sx,sy=cx,cy         -- make sx,sy (start) equal to cx,cy (end)
-- that means the next line will start where the previous line ended
-- and go to the current cx,cy position.
end
end

function touched(t)
if t.state==BEGAN then  -- when the screen is touched
cx,cy=t.x,t.y       -- set cx,cy to where the screen was first touched
sx,sy=t.x,t.y       -- set sx,sy to where the screen was first touched
-- otherwise a line will be drawn in the draw function from 0,0 (sx,sy)
-- which was set in the setup function to the current cx,cy
elseif t.state==MOVING then -- finger moving on the screen
cx,cy=t.x,t.y       -- set cx,cy to the current touch position.
end
end
``````
• Posts: 15

What math is used? I know it’s algebra, but is there a certain algebra subject I can study that will help me in this? Or is it just how lua works,

• Posts: 7,923

There’s no algebra used at all. The statement backingMode(RETAINED) just keeps whatever is drawn on the screen. background() should be removed when using backingMode. All this does is draw a line from a starting point (ss,sy) to the current point (cx,cy) as you move your finger. The current point then becomes the starting point when the line is drawn.

• edited November 19 Posts: 15

This is what confuses me, if I look at something, with variables being represented as greater than or less than, and be contrasted among other variables to equal new equations, I think of algebra. Why can’t there just be one x in line? I mean a variable could represent something infinite, so why solve the math in the code when you could just presume them? (x,t,x) makes sense but why so much else? It’s almost as if you told the system what each thing does, literally. I mean you keep altering between x, y, and c, yet it works, is c just a way to use x again or what? I mean I understand arrays, 12th grade matrix better than this. That might just be me, but if it’s gonna use math, use a recognizable Math subject. Matrix and linear algebra are the only forms I’ve seem that has corresponded to this is any way. It’s not like learning a vocal language where you keep same things but make them different. Math is math, and if this is not relating to any real usage of math, then I don’t know what to do.

I know a few programming languages; web design, C and a bit of C#, this one has confused me the most. I don’t know if it’s too simple or too “mind of its own”. I don’t want to give up on Lua, but I mean I literally review these things you show me over and over, I’ll go through it, the linear equations makes sense.. yeah, I under sta-Z. What? Where did that come from? I thought the only elements on the Cortesian plane were X, and Y, and as far as I know, this is not 3D.. Its not the functions that gets me, I understand those fine, the syntax and markup is suprisingly easy, it’s just the reoccurring stuff that appears out of nowhere. I don’t want to make you explain it to me, because I know you have better than to do, but do you know any websites or something that could help me with the math of this language. Anyways, I know I got a little mad, I’m just so lost on how this math works. This is not specifically targeting the drawing thing, I’m just saying in general of lua, as whole.

• Posts: 7,923

@Valurim You kind of lost me in what you’re saying above. I’m here to help and I don’t want you to give up on this. It’s a lot of fun once you understand it. I’ll go into as much detail as you want me to so you understand everything in the code above. There’s a lot to learn and it’s going to take time. Which lines of code are confusing and I’ll go into detail. Maybe that will help. Well have to wait till tomorrow, it’s 11pm and I’m heading to bed.

• Posts: 15

For the one most recent; lines, 5,12,13,14,19,20,23. Basically the ones with numbers.

• Posts: 7,923

@Valurim I added a bunch of comments to the code above. Just to go over things, the function setup() only executes once when the program starts. That’s where you can initialize any variables you use. The function draw() gets executed 60 times per second. If you use backingMode(RETAINED), anything you draw stays on the screen. If you use background(some color), each draw cycle the screen is cleared and filled with (some color). So any code that was executed in the function draw will execute again and redraw something on the screen. In the above code, if we draw circles instead of a line, you would just have circles drawn every so many pixels depending on how fast you move your finger. By drawing a line from where the previous line ended, you then have a continuous line instead of a bunch of circles. Try the code below which draws circles every 1/60th of a second as you move your finger on the screen. The faster you move your finger, the farther apart the circles are.

``````displayMode(FULLSCREEN) -- show the full screen

function setup()
backingMode(RETAINED)
cx,cy=0,0
end

function draw()
fill(255)
ellipse(cx,cy,5)
end

function touched(t)
if t.state==MOVING then -- finger moving on the screen
cx,cy=t.x,t.y       -- set cx,cy to the current touch position.
end
end
``````
• edited November 22 Posts: 15

So I came back to the rocket code, this time it’s tilt to move, so how would would I make the clouds come from the top of the screen to the bottom, and the ship faces up. I tried changing some code but nothing happened.

``````displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)

function setup()
x=WIDTH/2
y=HEIGHT/2
x,y=100,HEIGHT/2
x1,y1=100,HEIGHT/2
x1v,y1v=1,3
px,py=0,0
pxv,pyv=8,0
mis={}
ships={}
for z=1,10 do
table.insert(ships,vec2(math.random(HEIGHT,HEIGHT+2000),math.random(WIDTH)))
end
end

function draw()
background(40, 40, 50)
spritePos = vec2( WIDTH/2, HEIGHT/2 )
pushMatrix()
translate(x,y)
rotate(270)
x=x+Gravity.x*0
y=y+Gravity.y*20
sprite("Tyrian Remastered:Boss D",0,0)
popMatrix()

popMatrix()
rect(WIDTH-150,150,100,50)
for a,b in pairs(ships) do
sprite("Space Art:Cloudy Nebula",b.x,b.y,50)
b.x=b.x-3
if b.x<-20 then
table.remove(ships,a)
end
end
for a,b in pairs(mis) do
b.x=b.x+pxv
sprite("Space Art:Green Explosion",b.x,b.y,30)
if math.abs(px-x1)<15 and math.abs(py-y1)<15 then
sprite("Tyrian Remastered:Explosion Huge",x1,y1)
end
if b.x>WIDTH+50 then
table.remove(mis,a)
end
for c,d in pairs(ships) do
if math.abs(b.x-d.x)<20 and math.abs(b.y-d.y)<20 then
table.remove(ships,c)
table.remove(mis,a)
end
end
end
if #ships<10 then
table.insert(ships,vec2(math.random(HEIGHT,HEIGHT+2000),math.random(HEIGHT)))
end
end

function touched(t)
if t.state==BEGAN then
if t.x>WIDTH-150 and t.x<WIDTH-50 and t.y>150 and t.y<200 then
table.insert(mis,vec2(x,y))
end
elseif t.state==MOVING then
x=x+t.deltaX
y=y+t.deltaY
end
end
``````
• Posts: 7,923

Make these changes to the lines I show in setup() and draw(). In setup change WIDTH and HEIGHT. In draw change the b.x to b.y and the WIDTH and HEIGHT.

``````function setup()
for z=1,10 do
table.insert(ships,vec2(math.random(WIDTH),math.random(HEIGHT,HEIGHT+1000)))
end
end

function draw()
for a,b in pairs(ships) do
sprite("Space Art:Cloudy Nebula",b.x,b.y,50)
b.y=b.y-3
if b.y<-20 then
table.remove(ships,a)
end
end

if #ships<10 then
table.insert(ships,vec2(math.random(WIDTH),math.random(HEIGHT,HEIGHT+1000)))
end
end
``````
• Posts: 15

Works perfectly, although I have another problem and it’s a border. So pretty much if I go back to far or left/right to far it just goes off the screen. I can’t seem to find a library that would add a sort of boundary.

• Posts: 7,923

@Valurim There’s really no border to add. The left/right boundaries (x) are 0 on the left and the value of WIDTH on the right. The bottom/top boundaries (y) are 0 at the bottom and the value of HEIGHT at the top. So to stop whatever you want from going off the screen you just need to add `if` statements and not allow the x,y values to go less than 0 or greater than WIDTH or HEIGHT.

• Posts: 7,923

Here’s an example.

``````displayMode(FULLSCREEN)

function setup()
sx,sy=WIDTH/2,HEIGHT/2
end

function draw()
background(40, 40, 50)
sprite("Planet Cute:Character Princess Girl",sx,sy)
end

function touched(t)
if t.state==MOVING then
sx=sx+t.deltaX
sy=sy+t.deltaY
if sx<30 then
sx=30
end
if sx>WIDTH-30 then
sx=WIDTH-30
end
if sy<50 then
sy=50
end
if sy>HEIGHT-30 then
sy=HEIGHT-30
end
end
end
``````
• Posts: 15

So I was messing around with the code; trying to make a script where if the ships touch the player or get to the bottom of the screen, the player goes to (Tyrian Remastered:Boss D Destroyed) and it falls to the bottom of the screen and it ends. Speaking of end, return true doesn’t work, is there an alternative?

• edited November 27 Posts: 7,923

Compare this code to what you have to see what changed. The changes are for when the ships reach the bottom of the screen and the variable bottom is set to true. As for when the ships hit the Tyrian ship, in the first for loop for ships, subtract the ships b.x and b.y values with the x,y values using math.abs and some value like what’s done farther down in the code. You can set the variable bottom to true also.

``````function draw()
background(40, 40, 50)
spritePos = vec2( WIDTH/2, HEIGHT/2 )
pushMatrix()
translate(x,y)
rotate(270)

if bottom then
sprite("Tyrian Remastered:Boss D Destroyed",0,0)
y=y-3
else
x=x+Gravity.x*0
y=y+Gravity.y*20
sprite("Tyrian Remastered:Boss D",0,0)
end

popMatrix()
popMatrix()
rect(WIDTH-150,150,100,50)
for a,b in pairs(ships) do
sprite("Space Art:Cloudy Nebula",b.x,b.y,50)
b.y=b.y-3
if b.y<-20 then
table.remove(ships,a)

bottom=true

end
end
for a,b in pairs(mis) do
b.x=b.x+pxv
sprite("Space Art:Green Explosion",b.x,b.y,30)
if math.abs(px-x1)<15 and math.abs(py-y1)<15 then
sprite("Tyrian Remastered:Explosion Huge",x1,y1)
end
if b.x>WIDTH+50 then
table.remove(mis,a)
end
for c,d in pairs(ships) do
if math.abs(b.x-d.x)<20 and math.abs(b.y-d.y)<20 then
table.remove(ships,c)
table.remove(mis,a)
end
end
end
if #ships<10 then
table.insert(ships,vec2(math.random(300,WIDTH),math.random(HEIGHT,HEIGHT+1000)))
end
end
``````

Code for ships hitting Tyrian.

``````    for a,b in pairs(ships) do
sprite("Space Art:Cloudy Nebula",b.x,b.y,50)
b.y=b.y-3
if math.abs(b.x-x)<25 and math.abs(b.y-y)<25 then
bottom=true
end
if b.y<-20 then
table.remove(ships,a)
bottom=true
end
end
``````