**EDIT**
I added draggable code for the big red circle, basically once you touch the big red circle, the blue square will chase you around.
What I first expected when I put this together, was the Blue Square would just “slide” down and around an object, I didn’t expect it to just go right through it. I want to allow the square to get to the Big red circle (that you drag around on screen) but only if it has a path to do so. In time I’ll have borders around the screen.
I am thinking something like if blue square is touching more than 2 objects, then calculate a new path unless the opening of another path is less than the width of the blue square.
Yea, how’s that for complicated?
Here is the code:
--The big red circle is draggable ..FYI.
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setScale( 60 )
physics.setGravity( 0, 0 )
physics.setPositionIterations(16)
local game = display.newGroup();
game.x = 0
game.y = 0
-- (Xlocation,Ylocation, Xsize, Ysize)
local square = display.newRect (0,0,50,50)
square:setFillColor( 0, 0, 255)
physics.addBody( square,"dynamic",{ density = 0.1, friction=0.3, bounce=0.3, } )
game:insert(square)
local circle = display.newCircle (50,50,50,50)
circle:setFillColor( 255, 0, 0 )
physics.addBody( circle, "dynamic", { friction=0.3, bounce=0.3 ,radius=50} )
circle.isBullet = true
game:insert( circle)
local topplat = display.newRect (100,200,150,50)
topplat:setFillColor( 255, 255, 0)
physics.addBody( topplat,"static",{friction=0.3, bounce=0.3, isGround = true } )
game:insert(topplat)
topplat.rotation = 20
topplat.isBullet = true
local bottomplat = display.newRect (100,300,150,50)
bottomplat:setFillColor( 0, 255, 0)
physics.addBody( bottomplat,"static",{friction=0.3, bounce=0.3, isGround = true } )
bottomplat.isBullet = true
game:insert(bottomplat)
local target = display.newCircle (160,400,20,20)
target:setFillColor( 255, 0, 255 )
physics.addBody( target,"static",{ friction=0.3, bounce=0.3 ,radius=20} )
target.isBullet = true
game:insert(target)
--Spawn the square on screen so it doesn't spawn off screen :)
square.x= math.random(square.width/2,
display.contentWidth - square.width/2)
square.y= math.random(square.height/2,
display.contentHeight - square.height/2)
local function dragscircle(event)
circle.x = event.x
circle.y = event.y
print "Dragging Circle"
end
--Function that will have square follow circle
local function followcircle(event)
transition.to(square, {time=1400, y = circle.y, x = circle.x, transition = easing.linear})
print "Circle Following Square"
end
circle:addEventListener ("touch", dragscircle)
circle:addEventListener ("touch", followcircle)
[import]uid: 61600 topic_id: 13801 reply_id: 313801[/import]
[import]uid: 61600 topic_id: 13801 reply_id: 50697[/import]
[import]uid: 71210 topic_id: 13801 reply_id: 50827[/import]