Ive been trying to implement a camera that zooms in on my game. Im having trouble though after the camera zooms in… I have a drag feature on the game where you drag the objects around on the screen, when the camera zooms in this movement gets screwed up… cant figure out why… main.lua code below
[code]
– The blue dot on the screen is a zoom button
– The red square is the object
– the white circles are vertex that you can drag the object to, supposed to be able to “drop” the letter on the vertex, and it will snap
– to it, not working for some reason? If this were working, the camera would zoom into the location where the object was dropped… via
– changing the reference point of the camera group
–Main problem… when the camera zooms in… the drag movement for the object messes up? How can I fix this?
cameraGroup = display.newGroup()
gameGroup = display.newGroup()
local zoom = false
–functions
local touch
local objectCollision
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )
physics.setDrawMode( “hybrid” )
local newX – supposed to be popupated when the object collides with one of the vertexes… not working right now?
local newY – supposed to be popupated when the object collides with one of the vertexes… not working right now?
local object = display.newRect(0, 0, 25, 25)
object:setFillColor(255, 0, 0)
object.x = 210
object.y = 260
object.xScale = .9
object.yScale = .9
physics.addBody( object,{isSensor = true} )
object.collision = objectCollision
object:addEventListener( “collision”, object )
cameraGroup:insert(object)
local vertex = {}
local count = 1
for i = 1, 3 do
for j = 1, 3 do
vertex[count] = display.newCircle( 0, 0, 10)
vertex[count].x = (j*120) - 45
vertex[count].y = (i*80) - 15
physics.addBody( vertex[count], { isSensor = true } )
cameraGroup:insert(vertex[count])
count = count + 1
end
end
objectCollision = function(self, event)
if event.phase == “began” then
self.xScale = .75
self.yScale = .75
newX = event.other.x
newY = event.other.y
print(newX)
end
end
touch = function(event)
local phase = event.phase
if “began” == phase then
object = event.target
object:toFront()
print(object.x …“objectx”)
print(object.y …“objecty”)
print(event.x …“eventX”)
print(event.y …“eventY”)
– if zoom == false then
– gameGroup:insert(object)
– end
if zoom == false then
cameraGroup:insert(object)
end
display.getCurrentStage():setFocus( object )
object.isFocus = true
newX = object.xOrigin
newY = object.yOrigin
object.x0 = event.x - object.x
object.y0 = event.y - object.y
end
if object.isFocus then
if “moved” == phase then
if zoom == false then
object.x = event.x - object.x0
object.y = event.y - object.y0
end
if zoom == true then – this was my attempt to fix problem… doesnt work
object.x = event.x - object.x0
object.y = event.y - object.y0
end
end
if “ended” == phase then
object.x = newX
object.y = newY
object.xScale = .75
object.yScale = .75
cameraGroup:insert(object)
display.getCurrentStage():setFocus( nil )
object.isFocus = false
end
end
end
local function onCameraTouch(event)
if event.phase == “began” then
zoom = true
if newX <= 135 then
if newY <= 75 then
cameraGroup:setReferencePoint(display.TopLeftReferencePoint)
elseif newY > 75 and newY < 165 then
cameraGroup:setReferencePoint(display.CenterLeftReferencePoint)
elseif newY >= 165 then
cameraGroup:setReferencePoint(display.BottomLeftReferencePoint)
end
end
if newX > 135 and newY < 285 then
if newY <= 75 then
cameraGroup:setReferencePoint(display.TopCenterReferencePoint)
elseif newY > 75 and newY < 165 then
cameraGroup:setReferencePoint(display.CenterReferencePoint)
elseif newY >= 165 then
cameraGroup:setReferencePoint(display.BottomCenterReferencePoint)
end
end
if newX >= 285 then
if newY <= 75 then
cameraGroup:setReferencePoint(display.TopRightReferencePoint)
elseif newY > 75 and newY < 165 then
cameraGroup:setReferencePoint(display.CenterRightReferencePoint)
elseif newY >= 165 then
cameraGroup:setReferencePoint(display.BottomRightReferencePoint)
end
end
timer.performWithDelay(12, function()cameraGroup.xScale = cameraGroup.xScale + .01;
cameraGroup.yScale = cameraGroup.yScale + .01 end, 50)
end
end
local btnCamera = display.newCircle( 500, 200, 10 )
btnCamera:setFillColor(0,0,255)
btnCamera.x = 240
btnCamera.y = 300
gameGroup:insert(btnCamera)
btnCamera:addEventListener(“touch”, onCameraTouch)
local cameraDrag = display.newRoundedRect(0, 0, 420, 240, 2)
cameraDrag.isVisible = false
cameraDrag.isHitTestable = true
local function dragCamera(event) – I was also hoping to be able to move the camera around, not sure how to limit its movement though.
local phase = event.phase
if zoom == true then
if “began” == phase then
cameraGroup:setReferencePoint(display.CenterReferencePoint)
display.getCurrentStage():setFocus( cameraDrag )
cameraDrag.isFocus = true
cameraDrag.x0 = event.x - cameraDrag.x
cameraDrag.y0 = event.y - cameraDrag.y
end
if cameraDrag.isFocus then
if “moved” == phase then
–if cameraGroup.x > 0 and cameraGroup.x < 420 and cameraGroup.y > 0 and cameraGroup.y < 240 then – this doesnt work
cameraGroup.x = event.x - cameraDrag.x0
cameraGroup.y = event.y - cameraDrag.y0
–end
end
if “ended” == phase then
display.getCurrentStage():setFocus( nil )
cameraDrag.isFocus = false
end
end
end
end
cameraDrag:addEventListener(“touch”, dragCamera)
object:addEventListener(“touch”, touch)
[/code] [import]uid: 28912 topic_id: 13212 reply_id: 313212[/import]
[import]uid: 71210 topic_id: 13212 reply_id: 48525[/import]