Is there a way to make a perpetual move? And some other things...

Hello. I’m pretty new to Codea, and I got some questions. Im trying to recreate a game called “Gravitron”. So I need some help:

  1. Is there a way to perform a perpetual move of and object, for example, an ellipse, that makes the Y of the ellipse go up and down for always when code runs.

  2. Is there a way to make the Controls of moving left and right (in my code) permanent when they are being touched? For example (in my code), when the controls are touched, it only moves the value, and then, you need to touch again. Is there a way to when they are being touched, they move and move before the stop being touched.

Thanks you so much for your help :slight_smile:

EDIT: This is my code


-- Test Gravitron Game

p = vec2(WIDTH/2)

-- Use this function to perform your initial setup
function setup()
    
    displayMode( FULLSCREEN )
    
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    
    fill(177, 133, 46, 255)
    noStroke()
    ellipse(p.x, HEIGHT/2, 150)
    
sprite("Cargo Bot:Command Right", WIDTH - 50 , 50 , 70, 70)

sprite("Cargo Bot:Command Left", 50, 50, 70, 70)

if p.x < 1 then 
    
    p.x = WIDTH - 2
    
end

if p.x > WIDTH - 1 then 
    
    p.x = 2
    
end 

end

function touched(t)

    if CurrentTouch.x > WIDTH - 100 and CurrentTouch.y < 100 then
    
       p.x = p.x + 10
    
    end
    
    if CurrentTouch.x < 100 and CurrentTouch.y < 100 then
        
        p.x = p.x - 10
        
    end
    
end

  1. try tweens - look at the Animation demo project for examples

  2. post some of your code, then it’s easier to suggest answers

Sorry, I forgot to add it

Try this, changes marked

p = vec2(WIDTH/2)

function setup()
    displayMode( FULLSCREEN )
    dx, dy = 0, 0      -------------------NEW
end

function draw()
    background(0, 0, 0, 255)
    fill(177, 133, 46, 255)
    noStroke()
    ellipse(p.x, HEIGHT/2, 150)
    sprite("Cargo Bot:Command Right", WIDTH - 50 , 50 , 70, 70)
    sprite("Cargo Bot:Command Left", 50, 50, 70, 70)
    p.x = p.x + dx       -------------------NEW 
    if p.x < 1 then 
        p.x = WIDTH - 2
    end
    if p.x > WIDTH - 1 then 
        p.x = 2
    end 
end

function touched(t)
    if CurrentTouch.x > WIDTH - 100 and CurrentTouch.y < 100 then
       dx = dx + 10       -------------------NEW
    end

    if CurrentTouch.x < 100 and CurrentTouch.y < 100 then
        dx = dx - 10       -------------------NEW
    end
end

@Ignatz I also wouldn’t use CurrentTouch in the touched function.

true, but one thing at a time

@SkyTheCoder and @Ignatz

Quick question. Why wouldn’t you?

CurrentTouch holds the most recent state, however long ago it happened (so it can mislead your program into thinking the touch continues), whereas the touched function is only triggered by current touches.

When using touched, you should check the state property, which can be BEGAN, MOVING or ENDED. If you’re just trapping a touch on the screen, then either BEGAN or ENDED will do.

Touches can be quite tricky, though, and take a little getting used to.

Is this similar to me asking for something to appear at the area of CurrentTouch, and me lifting my finger, and it staying there?

yes, but you don’t need CurrentTouch to make something stay at the position of the last touch

--remember last touch position
function touched(t)
    if t.state==ENDED then pos=vec2(t.x,t.y) end
end

I still don’t get why not to use CurrentTouch in function touch. Forgive me for being slow on the uptake.

You can use CurrentTouch - but it only remembers the LAST touch event that happened, if you touch the screen in multiple places at the same time you’ll probably miss the inbetween states.

Using the touched function ensures that none of these are missed as you can track each touch using the t.id field.

@CayDay47 I think that CurrentTouch in the touched function might be one frame behind, but besides, why use a single touch (which can cause errors if you have multiple fingers on the screen) when you have multitouch available easily?

Thanks for explaining everyone… :slight_smile: