ontouch function

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.

@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.

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

How do you rotate a sprite

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.

@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
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.

@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()

@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

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

@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.

@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

@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

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?

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

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.

Thank you

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

Not sure what happened to your account, but here’s an answer to your last question.

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

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.