Howdy, Stranger!

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

Dragging an object that is floating around

edited August 2014 in Questions Posts: 24

I have several objects that are draggable. They are currently in static random positions. I would like to have them float a bit and rotate to give them some movement. When I tried to do this I used translate and my touches stopped working. Everything works fine until I try to set push and pop on the objec


  • IgnatzIgnatz Mod
    Posts: 5,396

    @swiftjuan - it's hard to say without seeing some code. Can you post just enough to show the problem?

  • dave1707dave1707 Mod
    Posts: 7,532

    @swiftjuan Here's an example of objects floating around and rotating a little. You can also touch them to drag them around.

    displayMode(FULLSCREEN) function setup() count=0 -- set count sel=0 rot={} -- table for rotation flys={} -- table for flys for z=1,10 do table.insert(flys,vec4(math.random(WIDTH),math.random(HEIGHT),0,0)) rot[z]=0 end end function draw() background(40, 40, 50) count=count-1 if count<=0 then -- if 0, set to 60 count=60 -- this is equivalent to 1 second end for a,b in pairs(flys) do b.x=b.x+b.z -- add speed direction to x position b.y=b.y+b.w -- add speed direction to y position if b.x>WIDTH then -- check if off screen b.x=0 end if b.x<0 then b.x=WIDTH end if b.y>HEIGHT then b.y=0 end if b.y<0 then b.y=HEIGHT end pushMatrix() translate(b.x,b.y) rot[a]=rot[a]+math.random(-10,10) -- rotate a random angle rotate(rot[a]) sprite("Platformer Art:Battor Flap 1",0,0) popMatrix() if count==60 then -- change direction every second b.z=math.random(-1,1) b.w=math.random(-1,1) end end end function touched(t) if t.state==BEGAN then v1=vec2(t.x,t.y) for a,b in pairs(flys) do d=v1:dist(vec2(b.x,b.y)) if d<50 then sel=a end end end if t.state==MOVING and sel>0 then flys[sel].x=t.x flys[sel].y=t.y end if t.state==ENDED then sel=0 end end
  • Wow this is great thanks!

Sign In or Register to comment.