Physics.addBody - creates two objects instead of one when using a button

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]

please post code in [lua] [/lua] tags

your create routine is happening for both event.phase==“began” and event.phase==“moved” (or probably event.phase=="ended)

you only want to create a new object in one phase [import]uid: 6645 topic_id: 6115 reply_id: 21049[/import]

If I am reading it right you seem to be using touch events which can fire when you touch and when you lift your finger.

The docs for this are here about half way down the page.

http://developer.anscamobile.com/content/events-and-listeners

So for example enclose your creation of the item in an event phase check.

So Your code would have this :-

[lua]local buttonHandler = function( event )
if event.phase == “began” then (OR YOU COULD USE AN EVENT PHASE OF ENDED)
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
end[/lua]

I also never tend to use kinematic as this causes me issues. Set it to static initially.

With regard to getting rid of them well you could cheat and insert them into a display group and clear that group when the right button is pressed. (Only of use if you want to clear them all.)

Its late here so i may not be very clear but I need a break from level design.

Cheers

Mike R [import]uid: 9950 topic_id: 6115 reply_id: 21041[/import]