Creating a custom map

Hi,

I am trying to create a custom map. Firstly I created the map in a png an d loaded it into the app. Then using the both the pinch and zoom, and the dragme examples I tried to manipulate the map.

However this does not work as it only sensors the first touch. I will post my code at the bottom.

Is there a better way to do this? or do I have to somehow use listeners for if the user has one or two fingers on the screen???

Many thanks

[code]

– activate multitouch
system.activate( “multitouch” )
– add bkgd image to screen
local background = display.newImage( “map2.png”, 0, 0 )
local function onTouch( event )
local background = 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 = background.parent
parent:insert( background )
display.getCurrentStage():setFocus( background, event.id )

– 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.
background.isFocus = true

– Store initial position
background.x0 = event.x - background.x
background.y0 = event.y - background.y
elseif background.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”).
background.x = event.x - background.x0
background.y = event.y - background.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( background, nil )
background.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
background:addEventListener( “touch”, onTouch )

local function calculateDelta( previousTouches, event )
local id,touch = next( previousTouches )
if event.id == id then
id,touch = next( previousTouches, id )
assert( id ~= event.id )
end

local dx = touch.x - event.x
local dy = touch.y - event.y
return dx, dy
end

– create a table listener object for the bkgd image
function background:touch( event )
local result = true

local phase = event.phase

local previousTouches = self.previousTouches

local numTotalTouches = 1
if ( previousTouches ) then
– add in total from previousTouches, subtract one if event is already in the array
numTotalTouches = numTotalTouches + self.numPreviousTouches
if previousTouches[event.id] then
numTotalTouches = numTotalTouches - 1
end
end

if “began” == phase then
– Very first “began” event
if ( not self.isFocus ) then
– Subsequent touch events will target button even if they are outside the stageBounds of button
display.getCurrentStage():setFocus( self )
self.isFocus = true

previousTouches = {}
self.previousTouches = previousTouches
self.numPreviousTouches = 0
elseif ( not self.distance ) then
local dx,dy

if previousTouches and ( numTotalTouches ) >= 2 then
dx,dy = calculateDelta( previousTouches, event )
end

– initialize to distance between two touches
if ( dx and dy ) then
local d = math.sqrt( dx*dx + dy*dy )
if ( d > 0 ) then
self.distance = d
self.xScaleOriginal = self.xScale
self.yScaleOriginal = self.yScale
print( "distance = " … self.distance )
end
end
end

if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event

elseif self.isFocus then
if “moved” == phase then
if ( self.distance ) then
local dx,dy
if previousTouches and ( numTotalTouches ) >= 2 then
dx,dy = calculateDelta( previousTouches, event )
end

if ( dx and dy ) then
local newDistance = math.sqrt( dx*dx + dy*dy )
local scale = newDistance / self.distance
print( “newDistance(” …newDistance … “) / distance(” … self.distance … “) = scale(”… scale …")" )
if ( scale > 0 ) then
self.xScale = self.xScaleOriginal * scale
self.yScale = self.yScaleOriginal * scale
end
end
end

if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event

elseif “ended” == phase or “cancelled” == phase then
if previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches - 1
previousTouches[event.id] = nil
end

if ( #previousTouches > 0 ) then
– must be at least 2 touches remaining to pinch/zoom
self.distance = nil
else
– previousTouches is empty so no more fingers are touching the screen
– Allow touch events to be sent normally to the objects they “hit”
display.getCurrentStage():setFocus( nil )

self.isFocus = false
self.distance = nil
self.xScaleOriginal = nil
self.yScaleOriginal = nil

– reset array
self.previousTouches = nil
self.numPreviousTouches = nil
end
end
end

return result
end

– Determine if running on Corona Simulator

local isSimulator = “simulator” == system.getInfo(“environment”)

– Multitouch Events not supported on Simulator

if isSimulator then
msg = display.newText( “Multitouch not supported on Simulator!”, 0, 20, “Verdana-Bold”, 14 )
msg.x = display.contentWidth/2 – center title
msg.y = display.contentHeight/2 – center title
msg:setTextColor( 255,255,0 )
end

– register table listener
Runtime:addEventListener( “touch”, background )
[/code] [import]uid: 10697 topic_id: 10699 reply_id: 310699[/import]

bump

anybody??? [import]uid: 10697 topic_id: 10699 reply_id: 40665[/import]