event.x and event.y accelerometer

Hello,

I am trying to spawn an object based on the position of a user’s finger. The problem is, the object that is touched in order to spawn the object is controlled by the accelerometer. The object that is being spawned will also be controlled by the accelerometer. I’ve been working on this for 2 days and have had no luck. In previous projects, I used object.x = event.x and object.y = event.y to do this, but it doesn’t seem to be working. The error I am geeing is below:
Runtime error
/Users/username/Documents/Apps/Game/normal.lua:95: attempt to index global ‘event’ (a nil value)
stack traceback:
[C]: ?
/Users/username/Documents/Apps/Game/normal.lua:95: in function ‘spawnBlender’
/Users/username/Documents/Apps/Game/normal.lua:61: in function
?: in function <?:215>

[blockcode]function spawnBlender(event)
local blender = display.newImageRect (“images/blender.png”, 60, 67)
blender:setReferencePoint(display.CenterReferencePoint)
blender.x = event.x
blender.y = event.y
containerGroup:insert(blender)
blender.name = “blender”
physics.addBody(blender, “dynamic”)

blender:addEventListener(“touch”, removeBlender)[/blockcode]

Any help on this would appreciated. Is this even possible to accomplish?

Thank you.
[import]uid: 47722 topic_id: 18963 reply_id: 318963[/import]

Plug and play code would help a lot here.

It would also be useful to see more code, I’m especially interested in how you’ve added the listener.

Peach :slight_smile: [import]uid: 52491 topic_id: 18963 reply_id: 73049[/import]

@peach pellen Here’s some more code. Maybe this will help.

[blockcode]

– Accelerometer
local motionx = 0
local motiony = 0

function onAccelerate(event)
motionx = 25 * event.xGravity
motiony = 25 * event.yGravity
end
Runtime:addEventListener(“accelerometer”, onAccelerate)

function containerSwap()
local containerGroup = display.newGroup()

function spawnBasket(event)
local basket = display.newImageRect (“images/basket.png”, 110, 60)
basket:setReferencePoint(display.CenterReferencePoint)
–basket.x = _W/2; basket.y = _H - 30
basket.x = event.x
basket.y = event.y
containerGroup:insert(basket)
basket.name = “basket”
physics.addBody(basket, “dynamic”)

function moveBasket(event)
basket.x = basket.x - motiony
end
Runtime:addEventListener(“enterFrame”, moveBasket)

local function removeBasket(event)
basket:removeSelf()
basket = nil
Runtime:removeEventListener(“enterFrame”, moveBasket)
spawnBlender()
end
basket:addEventListener(“touch”, removeBasket)
end

function spawnBlender(event)
local blender = display.newImageRect (“images/blender.png”, 60, 67)
blender:setReferencePoint(display.CenterReferencePoint)
–blender.x = _W/2; blender.y = _H - 30
blender.x = event.x
blender.y = event.y
containerGroup:insert(blender)
blender.name = “blender”
physics.addBody(blender, “dynamic”)

function moveBlender(event)
blender.x = blender.x - motiony
end
Runtime:addEventListener(“enterFrame”, moveBlender)

local function removeBlender(event)
blender:removeSelf()
blender = nil
Runtime:removeEventListener(“enterFrame”, moveBlender)
spawnBasket()
end
blender:addEventListener(“touch”, removeBlender)
end

startBasket = timer.performWithDelay(0, spawnBasket, 1)
end

startGame = timer.performWithDelay(0, containerSwap, 1)
[/blockcode] [import]uid: 47722 topic_id: 18963 reply_id: 73131[/import]

OK - You’re spawning originally based on a timer, so there is not event.x or event.y.

Peach :slight_smile: [import]uid: 52491 topic_id: 18963 reply_id: 73148[/import]

Hi Peach,

I restructured my code and removed the timers. I also gave the basket an initial spawning position, but still have the same error. Any ideas?

Thank you. [import]uid: 47722 topic_id: 18963 reply_id: 73155[/import]

Plug and play code or a sample download to test would make it easier for people to assist you - right now I have no idea what your code looks like and can’t run it :wink:

Peach [import]uid: 52491 topic_id: 18963 reply_id: 73174[/import]

Hi Peach,

Can I have your email so I can send you a test download?

Thank you. [import]uid: 47722 topic_id: 18963 reply_id: 73212[/import]

Hey again,

I can’t actually accept any files via email unless they’re bug related - that’s why we have premium support here; http://www.anscamobile.com/corona/support/ - for one on one stuff like that.

If you just make some plug and play code that would make things easier all round if you’d prefer not to use Premium Support.

Peach :slight_smile: [import]uid: 52491 topic_id: 18963 reply_id: 73222[/import]

Hi Peach,

I’m embarrassed to say that I don’t know what you mean by plug and play code. I understand what plug and play is in general, but I’m not sure how it relates to code.

Thank you. [import]uid: 47722 topic_id: 18963 reply_id: 73335[/import]

I figured it out! Thanks for your help Peach. It helped steer me in the right direction.
[import]uid: 47722 topic_id: 18963 reply_id: 73400[/import]

Apologies - we’re working with a bit of a time difference I think :wink:

I’m glad to hear you figured it out and happy I was able to assist in that.

For future reference “plug and play” code means code someone can copy and paste into a main.lua file and just run - they don’t need images or audio or the like.

Usually you’d use rectangles and circles or the like rather than images in that code.

Hope that helps :slight_smile:

Peach [import]uid: 52491 topic_id: 18963 reply_id: 73417[/import]