Question About Controls

How would I code a button that makes a object move continuously in a direction. Also multiple buttons? [import]uid: 8517 topic_id: 2629 reply_id: 302629[/import]

http://developer.anscamobile.com/code/joystick
[import]uid: 5712 topic_id: 2629 reply_id: 7541[/import]

//start of code - im doing this from memory

//load physics
local physics = require(“physics”)

//start physics
physics.start()

//make an object
//(associate and image for this object)

local obj_to_mv = display.newImage(“my_object.png”)
obj_to_mv.x = 50; obj_to_mv.y = 50

//make a button
//(associate and image for this object)
local butt_to_press = display.newImage(“my_button.png”)
butt_to_press.x = 150; butt_to_press.y = 150
physics.addbody(butt_to_press, {density = 3.0, friction =.5,
bounce = .3})
//make a the ground
//(associate and image for this object)
local ground = display.newImage(“ground.png”)
ground.x = 160; ground.y = 380
physics.addbody(ground,“static”, {friction = .5, bounce=.3})
//make a button listener and associate the button listener

function butt_to_touch:touch(event)
obj_to_mv:applylinearImpulse(-50, 0 ,obj_to_mv.x,obj_to_mv.y)
return true
end
butt_to_press:addEventListener(“touch”,butt_to_press)

//end of code - im doing this from memory

IMPORTANT understand “event.phases”

you can look at all the code you want but until
you cut-n-paste code and PLAY with it AND try
and make STUFF happen, you will never understand.

Also, there are several different ways to MOVE
an object.

you can directly affect the .x attribute of the object.
(not good way)

you could use the “translate.to” to move the object
(kindof good)

you could use the “tilt” function to move the object
accordingly

you could directly affect the gravity in a particular direction.

dont think there is any one way to do something.

you could set a timer and update the x position
every so often.
right now Im trying to comprehend building my own functions
and understanding the concept of GLOBAL, LOCAL & BLOCK
variable SCOPE is challenging as well.
use the print() function to check values.
PRINT(“this out to terminal” … object.x)

if a value is nil then your function cant access it(value)
just my 2 cents :smiley:

p.s. if anyone can point me in a solid tutorial with code example please point me towards it.

[import]uid: 11094 topic_id: 2629 reply_id: 61501[/import]