Game Development Reference
In-Depth Information
function that positions the object. That way, you will only have to call the function, and you can use
the same function name with any framework.
function position(theObject, xPos, yPos, refPoint)
local refPoint = refPoint or display.TopLeftReferencePoint
theObject:setReferencePoint(refPoint)
theObject.x = xPos
theObject.y = yPos
end
Next, we declare another function that will be responsible for the zooming:
local phase = event.phase
local target = event.target
if phase == "began"
display.currentStage:setFocus(target)
transition.to( back, {time=200, xScale=0.5, yScale=0.5} )
elseif phase == "ended" then
display.currentStage:setFocus(nil)
transition.to( back, {time=200, xScale=1, yScale=1} )
end
the touch begins and when the touch ends. When the touch starts we scale the entire display area
to 0.5, and on release we scale it back to 1.0. If you use this in your game, you can change these to
whatever best suits you.
Now for all the display elements:
local back = display.newGroup()
local wallpaper = display.newImage( back, "wallpaper.jpg", 0, 0, true )
local i
for i=1, 10 do
local rect = display.newRect( back, i*50, i*50, 40, 40 )
rect:setFillColor( 255-(i*10), 0, 0 )
end
position ( back, _W/2, _H/2, display.CenterReferencePoint )
local button = display.newRect( 10, 400, 50, 50 )
button:setFillColor(255,0,0,100)
button:addEventListener( "touch", zoomer )
Now when you touch the screen, all of the display objects scale out (since they are all part of the
group), and on release they all zoom in.
More Events
We added event listeners for touch , tap , and enterFrame . Similarly, we can add listeners for other events,
such as the system event, which raises events for system-related tasks.
 
Search WWH ::




Custom Search