I have a small test program based on the “DragPlatforms sample project” I have two buttons, one in the upper left hand corner and the other in the right. When the left button is clicked a box is to be created but two are created instead of one. The right hand button is to make the boxes all disappear so you can start over. It does not work at all. Any help would be appreciated My code is below:
– Abstract: DragPlatforms sample project - Modified
local physics = require(“physics”)
physics.start()
local ui = require(“ui”)
display.setStatusBar( display.HiddenStatusBar )
– A basic function for dragging physics objects
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”
– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “dynamic”
end
end
end
– Stop further propagation of touch event!
return true
end
local background = display.newImage( “carbonfiber.jpg”, true )
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
local ground = display.newImage( “floor.png” )
ground.x = 160; ground.y = 440
physics.addBody( ground, “static”, { friction=0.6 } )
– button one
local buttonHandler = function( event )
local block6 = display.newImage( “block.jpeg” )
block6.x = math.random(150, 255); block6.y = math.random(50, 125)
physics.addBody( block6, { density=1.0, friction=0.1, bounce=0.1} )
block6.isFixedRotation = true – question blocks don’t rotate, they just fall straight down
block6:addEventListener( “touch”, startDrag )
end
– button two
local buttonHandlerReset = function( event )
block6:removeSelf()
block6 = nil
end
– Of course, buttons don’t always have labels
local buttonArrow = ui.newButton{
default = “buttonArrow.png”,
over = “buttonArrowOver.png”,
onEvent = buttonHandler,
id = “arrow”
}
– Of course, buttons don’t always have labels
local buttonArrowRight = ui.newButton{
default = “buttonArrow.png”,
over = “buttonArrowOver.png”,
onEvent = buttonHandlerReset,
id = “arrow2”
}
– Add touch event listeners to objects
buttonArrow.x = 25; buttonArrow.y = 15
buttonArrowRight.x = 300; buttonArrowRight.y = 15
[import]uid: 22152 topic_id: 6115 reply_id: 306115[/import]