Drag Object Crashes Simulator

Not sure if this is the correct forum to post this, i`m wondering if anyone has
a better method to do this or a work around this current crash. I want objects
to spawn on different location outside screen and when they spawn I need them
to slowly make their way to the middle of screen but you can drag them and throw
them back out of screen.

So far I can make objects spawn on random location I also added them a EventListener
to touch and drag them, the problem is when they spawn I make them transition.to
the middle of screen and while they are transitioning if I touch them or when transition
is completed the simulator crashes I tried this on latest stable build and build 639.

Thanks! [import]uid: 30314 topic_id: 17022 reply_id: 317022[/import]

Update:
It no longer crashes it IF you give the object a physics.addBody

So that takes care of the crashing problem, now can anyone point
me in the right direction on how to accomplish what i previously asked?
If I try to drag an object while is transitioning to the middle of screen it
will first finish the transition then drag. Is the best approach to cancel
the transition when you touch the object or any better method i`m not
thinking of?

Thanks,
[import]uid: 30314 topic_id: 17022 reply_id: 63892[/import]

Please post a code snippet of your touch event handler so it’s easier to see where there may be a problem… [import]uid: 52430 topic_id: 17022 reply_id: 63925[/import]

Hey John,

I manage to fix the crashing problem but if you want to see the problem
you can run the simulator using Multi Puck sample code and comment
out the physics.addBody part and it will crash the simulator the moment
you try to spawn an object. That was almost identical to my problem
I had objects spawning randomly and transitioning to middle of screen
and if you touch the object (to drag like Multi Puck Sample) it will crash.
By adding Physics.addBody to the object it fix the problem.

This is the main.lua file of Multi Puck just comment out line 79 to see Crash.
[lua]-- Abstract: MultiPuck sample project
– Demonstrates multitouch and draggable phyics objects using “touch joints” in gameUI library

– Version: 1.1

– Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
– Copyright © 2010 ANSCA Inc. All Rights Reserved.

– History
– 1.0 9/1/10 Initial version
– 1.1 5/13/11 Fixed bug with stuck pucks. Added “return” to dragBody()
– Added “return true” to spawnDisk()
– 1.2 6/28/11 Call table.remove in removeOffscreenItems to keep allDisks as an array

display.setStatusBar( display.HiddenStatusBar )

local physics = require(“physics”)
local gameUI = require(“gameUI”)
local easingx = require(“easingx”)

physics.start()
physics.setGravity( 0, 0 ) – no gravity in any direction

popSound = audio.loadSound (“pop2_wav.wav”)
labelFont = gameUI.newFontXP{ ios=“Zapfino”, android=native.systemFont }

system.activate( “multitouch” )

local bkg = display.newImage( “paper_bkg.png”, true )
bkg.x = display.contentWidth/2
bkg.y = display.contentHeight/2

local myLabel = display.newText( “Touch screen to create pucks”, 0, 0, labelFont, 34 )
myLabel:setTextColor( 255, 255, 255, 180 )
myLabel.x = display.contentWidth/2
myLabel.y = 200

local diskGfx = { “puck_yellow.png”, “puck_green.png”, “puck_red.png” }
local allDisks = {} – empty table for storing objects

– Automatic culling of offscreen objects
local function removeOffscreenItems()
for i = 1, #allDisks do
local oneDisk = allDisks[i]
if (oneDisk and oneDisk.x) then
if oneDisk.x < -100 or oneDisk.x > display.contentWidth + 100 or oneDisk.y < -100 or oneDisk.y > display.contentHeight + 100 then
oneDisk:removeSelf()
table.remove( allDisks, i )
end
end
end
end

local function dragBody( event )
return gameUI.dragBody( event )

– Substitute one of these lines for the line above to see what happens!
–gameUI.dragBody( event, { maxForce=400, frequency=5, dampingRatio=0.2 } ) – slow, elastic dragging
–gameUI.dragBody( event, { maxForce=20000, frequency=1000, dampingRatio=1.0, center=true } ) – very tight dragging, snaps to object center
end

local function spawnDisk( event )
local phase = event.phase

if “ended” == phase then
audio.play( popSound )
myLabel.isVisible = false

randImage = diskGfx[math.random( 1, 3 )]
allDisks[#allDisks + 1] = display.newImage( randImage )
local disk = allDisks[#allDisks]
disk.x = event.x; disk.y = event.y
disk.rotation = math.random( 1, 360 )
disk.xScale = 0.8; disk.yScale = 0.8

transition.to(disk, { time = 500, xScale = 1.0, yScale = 1.0, transition = easingx.easeOutElastic }) – “pop” animation

physics.addBody( disk, { density=0.3, friction=0.6, radius=66.0 } )
disk.linearDamping = 0.4
disk.angularDamping = 0.6

disk:addEventListener( “touch”, dragBody ) – make object draggable
end

return true
end

bkg:addEventListener( “touch”, spawnDisk ) – touch the screen to create disks
Runtime:addEventListener( “enterFrame”, removeOffscreenItems ) – clean up offscreen disks [import]uid: 30314 topic_id: 17022 reply_id: 63935[/import]