Help with moving objects separately

Hi, I’m fairly new to coding in general, and I have been working on some code that allows you to drag objects around the screen depending on your touch.x and touch.y values. However, now all the objects move to that one location, rather than the one object that I want to drag at a time. I understand that this is because I have set all of the objects x and y positions to touch.x and touch.y, but I don’t know what code is needed so they can all be dragged separately and start off in different positions?

If anyone could help make changes to this, that would be greatly appreciated.

Thanks in advance. (P.S change the sprites to different ones if you want to test the code)

-- Fruit and Veg

-- Use this function to perform your initial setup
function setup()

x=math.random(50,WIDTH-50)
y=math.random(50, HEIGHT-50)   
end

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

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
      
sprite("Documents:orange", x, y, 60, 60)
    
sprite("Documents:Banana", x, y, 60, 60)

sprite("Documents:Corn", x, y, 60, 60)
sprite("Documents:watermelon", x, y, 60, 60)
    
function touched(touch)
        
x=touch.x
y=touch.y
     end
end

if you want several objects to live their own life, then create a class() and instanciations of this class. here is how you can do that. Also dont put the touched function definition inside the draw, it is redefined 60 times pers second!

-- Fruit and Veg

Object = class()
function Object:init(name)
    self.img = readImage( name )
    self.w=60
    self.h=60
    self.x=math.random(50,WIDTH-50)
    self.y=math.random(50, HEIGHT-50) 
end
function Object:draw()
    sprite(self.img,self.x,self.y, self.w, self.h)
end
local abs = math.abs
function Object:touched(t)
    if abs(t.x-self.x)<self.w/2 and abs(t.y-self.y)<self.h/2
    then
        self.x = t.x
        self.y = t.y
    end
end

-- Use this function to perform your initial setup
function setup()
    s1 = Object("Planet Cute:Character Boy")
    s2 = Object("Planet Cute:Character Cat Girl")
end

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

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    s1:draw()
    s2:draw()
end
function touched(touch)
    s1:touched(touch)
    s2:touched(touch)
end

@Jmv38

Thanks a lot for helping me out, but I don’t understand what the s1 and s2 part is?
Would you be able to briefly explain that?

Thanks again

you have 2 objects on the screen, right? Well they are named s1 and s2, and you can access their properties and methods the way it is done there. s1:draw() means ´please object s1, apply your draw() function to youself. thank you '. the available properties and methods are defined in the class ‘Object’ which is used as a model to create s1, s2 and as many copies as you want. If this is new to you, google ‘object oriented programming’ and read the tutos for that. it may take you several weeks (it did for me) of reading before you get the concept, dont be put off by that.

Ah okay. Thanks for that explanation, I’m kind of getting my head around it :-?

@Lachiem You can also use tables to keep track of everything. Just touch a sprite to move it around.


-- Fruit and Veg

displayMode(FULLSCREEN)

function setup()
    imgTab={}   -- image table
    table.insert(imgTab,readImage("Planet Cute:Character Boy"))
    table.insert(imgTab,readImage("Planet Cute:Character Princess Girl"))
    table.insert(imgTab,readImage("Planet Cute:Character Horn Girl"))
    table.insert(imgTab,readImage("Planet Cute:Character Pink Girl"))
    
    locTab={}   -- location table
    table.insert(locTab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50)))
    table.insert(locTab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50)))
    table.insert(locTab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50)))
    table.insert(locTab,vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50)))    
end

function draw()
    background(40, 40, 50)
    for z=1,#imgTab do
        sprite(imgTab[z],locTab[z].x,locTab[z].y)   -- draw sprites
    end
end
function touched(t)
    if t.state==BEGAN or t.state==MOVING then
        for a,b in pairs(locTab) do -- which sprite is touched
            if t.x>b.x-30 and t.x<b.x+30 and t.y>b.y-30 and t.y<b.y+30 then
                b.x=t.x
                b.y=t.y
            end
        end
    end
end

@dave1707, why two seperates tables? This could be done with just one table, and might be less confusing this way.

@JakAttak I thought it would be easier to follow with 2 tables. One table for the images and another for the locations. If the tables aren’t balanced, that would cause a problem though.

Being a new coder myself, assuming we only want one class I would have each object be represented by a vec2 that acts as their location. Then I’d have boxes around them representing their hit boxes. When it’s touched, if the touch began on a hit box then that object’s vec2 would be updated with the touch’s location. This would of course require some use of the ID of the touch, but it’d be rather simple despite my sucky explanation.

@Monkeyman32123 That’s what I have in my example above. A vec2 for location and it’s updated with the new location as the object is moved around the screen. I’m using a square for the touched area, but I’m not using the objects id. As long as you’re moving one object at a time, you don’t need the id. I have another example that allows you to move several objects at the same time. That one uses the objects id.

EDIT: Actually the program uses a joystick for each object and allows as many objects as you have fingers on the screen.

Yeah, I was thinking moving more than once, and sorry, I didn’t read through your code, I promise I wasn’t ripping you off. I just like solving problems like this in my head, helps me learn and such :stuck_out_tongue:

@Monkeyman32123 I don’t read thru a lot of code either and what you wrote just shows that your thinking is similar to mine when writing code. Working out problems in your head saves a lot of time writing code. You don’t waste a lot of time rewriting things.

Great minds think alike :stuck_out_tongue:
Yeah, I mostly do everything in my head then put it into code, and try to solve problems mentally. Seems to usually work until either circles or the third dimension try to join the party XD

Thanks for the responses guys