Button question

Hey guys.

I am currently making a game and have set up a button so i can drag my character around the screen. However, I am looking to implement gravity so when i let go the character falls to the floor. My understanding was that if i used my character y variable i could control the behavior however, it seems this isn’t the case. is there a button.y paramenter? Code I am using is below, thanks for any assistance.

[code]

–[[
Angry Dogs V1.0
Programming : Zack
–]]

–# SETTINGS #–
display.setStatusBar(display.HiddenStatusBar)
–activate multitouch
system.activate(“multitouch”)
–seed randomizer
local seed = os.time()
math.randomseed(seed)
–load libraries
local ui = require(“ui”)

–# VARIABLES #–
local background = {x = 0, y = 0, w = 35, h = 25, r = 10, img = “gfx/backgrounds/blue_bg.png”}
local dog = {x = 35, y = 260, img = “gfx/dog/dog-blink1.png”}
local gravity = {floor = 260, current = 0, speed = 2}

–# DISPLAY IMAGES #–
display.newImage(background.img, background.x, background.y, true)

local function controlGravity()
if dog.y <= gravity.floor then
dog.y = dog.y - gravity.speed
end
end
–# TOUCH EVENT FUNCTIONS #–
local function printTouch( event )
if event.target then
local bounds = event.target.stageBounds
print( “event(” … event.phase … “) (”…event.x…","…event.y…") bounds: “…bounds.xMin…”,"…bounds.yMin…","…bounds.xMax…","…bounds.yMax )
end
end

local function onTouch(event)
local t = event.target

– Print info about the event. For actual production code, you should
– not call this function because it wastes CPU resources.
printTouch(event)

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
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
end
end

– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end
local button = display.newImage(dog.img, dog.x, dog.y, true)
button:addEventListener(“touch”, onTouch)

– listener used by Runtime object. This gets called if no other display object
– intercepts the event.
local function printTouch2( event )
print( “event(” … event.phase … “) (”…event.x…","…event.y…")" )
end
–# MAIN #–

Runtime:addEventListener(“touch”, printTouch2)
controlGravity()
[/code] [import]uid: 6981 topic_id: 1518 reply_id: 301518[/import]

I haven’t run your example, but yes, any display object has a y property that you can set.

In your example, you only call controlGravity() once. Did you want it to be called repeatedly?
[import]uid: 54 topic_id: 1518 reply_id: 4302[/import]

Your button position would be control by button.x and button.y

You are using dog.x and dog.y, but they are only use to set the initial location of your button (image) and do not control it after it’s created. It looks like you want to use controlGravity() to control the button
y" value but as Eric mentioned, it’s not connected to anything. Changing dog.y to button.y in the function would allow you to control the button location. [import]uid: 7559 topic_id: 1518 reply_id: 4308[/import]