Height manipulation

I’m trying to make it so when I tap the sprite moves up and when I let go I descend, what I have so far is
if currenttouch.state=BEGAN or currenttouch.state=MOVING then
Sprite(sprite,100,???)
Thanks for the help

@Wallisch_pls Here’s a simple way of moving the sprite up or down. I also added code to move the sprite left or right by moving your finger left or right.


displayMode(FULLSCREEN)

function setup()
    x=WIDTH/2
    y=100
    v=0
end

function draw()
    background(40, 40, 50)
    sprite("Planet Cute:Character Boy",x,y)
    calcY()
end

function calcY()
    y=y+v
    if y>HEIGHT then
        y=HEIGHT
    end
    if y<=100 then
        y=100
    end
end

function touched(t)
    if t.state==BEGAN then    -- move sprite up
        v=1
    end
    if t.state==MOVING then     -- move sprite left or right
        x=x+t.deltaX
    end
    if t.state==ENDED then    -- move sprite down
        v=-1
    end
end

Is v vector? Thanks!

No, v is just a variable name that I picked. It could have been any name. I just change v to 1 or -1 depending on which direction I want the sprite to go.

Alright that works perfectly and I changed it a little to form to my project, and my brother had made this game before (helicopter) and for the “enemies” he used the same 3 ellipses they just changed place when the helicopter got past them, so how would I do something like this? Would the enemies be coming toward me? I’ll try to do it myself but I’m just wondering

I just made an ellipse(x,y,70) and I realize that it just follows my sprite. Do I make another class for this?

Are you after something like this.


displayMode(FULLSCREEN)

function setup()
    score=0
    en={}
    createEnemy()
    x=WIDTH/2
    y=200
    v=0
    gameOver=false
end

function draw()
    background(40, 40, 50)
    fill(255)
    text("SCORE  "..score,WIDTH/2,HEIGHT-25)
    text("ENEMY #  "..#en,WIDTH/2,HEIGHT-50)
    if gameOver then
        endScreen()
    else
        sprite("Planet Cute:Character Boy",x,y)
        calcY()
        showEnemy()
        checkCollision()
        addScore()
    end
end

function calcY()
    y=y+v
    if y>HEIGHT then
        y=HEIGHT
    end
    if y<=100 then
        y=100
    end
end

function addScore()
    score=score+1
    if score%250==0 then
        table.insert(en,vec2(math.random(WIDTH,WIDTH+100),math.random(0,HEIGHT)))
    end
end

function checkCollision()
    v1=vec2(x,y)
    for a,b in pairs(en) do
        d=v1:dist(vec2(b.x,b.y))
        if d<40 then
            gameOver=true
        end
    end
end

function endScreen()
    fill(255)
    fontSize(25)
    text("Game over",WIDTH/2,HEIGHT/2)
    text("Double tap screen to restart",WIDTH/2,HEIGHT/2-50)
end
    
function touched(t)
    if t.state==BEGAN then    -- move sprite up
        if gameOver and t.tapCount==2 then
            restart()
        end
        v=3
    end
    if t.state==MOVING then     -- move sprite left or right
        x=x+t.deltaX
    end
    if t.state==ENDED then    -- move sprite down
        v=-3
    end
end

function createEnemy()
    for z= 1,8 do
        table.insert(en,vec2(math.random(WIDTH,WIDTH+100),math.random(0,HEIGHT)))
    end  
end

function showEnemy()
    for a,b in pairs(en) do
        sprite("Space Art:Green Explosion",b.x,b.y)
        b.x=b.x-3
        if b.x<0 then
            b.x=math.random(WIDTH,WIDTH+200)
            b.y=math.random(0,HEIGHT)
        end
    end
end


Yes! Thank you so much, and how do I make it so when I touch them the game restarts?
If (sprite) x,y ?

@Wallisch_pls I made changes in the above code. I increased the number of enemy objects. I increased the speed of the enemy and the sprite. I added collision code and an end screen. Also, there’s a restart.

How can I make it so the enemies spawn more? More per frame

@Wallisch_pls I made changes to the above code to add a running score. I also increased the number of enemy objects every 250 points of score. I also display the number of enemy objects.