Hello,
In the code below I load a background and add a tile image and a button. For some reason I am having two problems with this.
I use the OnTouch event I wrote to drag an object on the screen and works well except when I drag the tile around without scaling first, the object is not release when I let go of the mouse. If I move the mouse and then click down anywhere else and move the mouse the tile still moves.
The other issue I have is when I click the button to scale the display it works great until I try to move the tile. It does move but does not stay with the mouse. It is probably because of the distance from the original x/y to the moved x/y is different due to scaling. I have to put my thinking hat on and calculate. Since I scaled to .8 should I get .2 difference and subtract that from t.x0?
Thanks,
Warren
local widget = require("widget") local composer = require( "composer" ) local scene = composer.newScene() local bgroup=display.newGroup() --------------------------------------------------------------------------------- local function onTouch( event ) local t = event.target 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 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 function btnZoomButtonEvent( event ) if ("ended" == event.phase ) then --bgroup:scale( .8, .8 ) transition.scaleTo( bgroup, { xScale=.7, yScale=.7, time=500 } ) end end local nextSceneButton function scene:create( event ) local sceneGroup = self.view end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen imgBack = display.newImageRect( bgroup, "images/back1.png", 3000, 3000 ) imgBack.anchorX = 0 imgBack.anchorY = 0 imgTile = display.newImageRect( bgroup, "images/tile1.png", 60, 120 ) imgTile.anchorX = 0 imgTile.anchorY = 0 imgTile.x = 680 imgTile.y = 320 imgTile:addEventListener( "touch", onTouch ) btnZoom = widget.newButton { width = 100, height = 100, label = "Zoom", onEvent = btnZoomButtonEvent } btnZoom.anchorX = 0 btnZoom.anchorY = 0 btnZoom.y = 200 btnZoom.x = 200 elseif phase == "did" then -- Called when the scene is now on screen end end