Zoom on touch event - lua

I'm trying to put in place a zoom function on touch event. I scale the parent group using transition.to and onComplete move it to the event center. But then at one point it just jumps to the parent group origin. Any ideas?
I'm now pasting a shortened version of the code.
local physics = require( "physics" )
physics.start()
physics.setContinuous( false )
--physics.setScale( 60 )
local height = display.contentHeight
local width = display.contentWidth
local backGround = display.newRect( 0,0,width,height )
backGround:setFillColor(91,91,91)
local allElements = display.newGroup()
local grip = {}
local gripSize = {
-- w: gripwidth, h: gripheight, s: strength required
{w=30, h=20, s=1},
{w=20, h=10, s=1.5},
{w=10, h=10, s=2},
}
local r
local function createTexture()
local originX = 0
local originY = height -75
for i=0,50 do
r = math.random(3)
local x = originX + math.random(width)
local y = originY - math.random(2*height)
grip[i] = display.newRect( allElements, x, y, gripSize[r].w, gripSize[r].h)
grip[i].size = gripSize[r].s
if (r == 1) then
grip[i]:setFillColor(51,255,0)
elseif (r == 2) then
grip[i]:setFillColor(255,51,51)
elseif (r == 3) then
grip[i]:setFillColor(51,51,255)
end
end
end
createTexture()
wallBottom = display.newRect( allElements, 0,height-20,width,20)
physics.addBody(wallBottom, "static", { density=5, friction=0.5, bounce=0.3 } )
head = display.newCircle( allElements, width/2,50,20 )
physics.addBody( head, { density=5, friction=0.5, bounce=0.3 } )
local touchBorder = 20
local function calcTouchOffset( e )
local x, y = 0, 0
if (e.x < touchBorder) then
x = e.x - touchBorder
elseif (e.x > width-touchBorder) then
x = e.x - (width-touchBorder)
end
if (e.y < touchBorder) then
y = e.y - touchBorder
elseif (e.y > height-touchBorder) then
y = e.y - (height-touchBorder)
end
return x, y
end
local function startDrag( e )
local body = e.target
local phase = e.phase
local stage = display.getCurrentStage()
if (e.phase == "began") then
e.target.bodyType = "dynamic"
e.target.hasFocus = true
local x, y = allElements:contentToLocal( e.x, e.y )
e.target.touchjoint = physics.newJoint( "touch", e.target, x, y )
stage:setFocus( e.target )
transition.to( allElements, { time = 200, x= -body.x, y= -body.y, xScale = 2, yScale = 2,})
xOffset, yOffset = 0, 0
return true
elseif (e.target.hasFocus) then
if (e.phase == "moved") then
local x,y = allElements:contentToLocal(e.x, e.y) -- This line is changed
e.target.touchjoint:setTarget( x, y ) -- This line is changed
xOffset, yOffset = calcTouchOffset( e )
else
transition.to( allElements, { time = 200, x = body.x, y = body.y, xScale = 1, yScale = 1, })
e.target.hasFocus = false
e.target.touchjoint:removeSelf()
e.target.touchjoint = nil
stage:setFocus( nil )
xOffset, yOffset = 0, 0
end
return true
end
xOffset, yOffset = 0, 0
return false
end
head:addEventListener( "touch", startDrag )
function allElements:update()
allElements.x, allElements.y = allElements.x - xOffset, allElements.y - yOffset
allElements.x, allElements.y = allElements.x, allElements.y
if (allElements.x > -startX) and ( startX < 0 ) then
allElements.x = -startX
elseif ( routeW < width ) then
allElements.x = 0
elseif ( allElements.x < startX ) and ( startX < 0 ) then
allElements.x = startX
end
--[[if (allElements.x > 0) then
allElements.x = 0
elseif ( routeW < width ) then
allElements.x = 0
elseif ( allElements.x < (width-routeW) ) and ( routeW > width ) then
allElements.x = width-routeW
end ]]--
if (allElements.y > (routeH-height)) and ( routeH > height ) then
allElements.y = routeH-height
elseif ( routeH < height ) then
allElements.y = 0
elseif (allElements.y < 0) then
allElements.y = 0
end
end
function enterFrame()
allElements:update()
end

You just need to update this part:
elseif (e.target.hasFocus) then
if (e.phase == "moved") then
local x,y = allElements:contentToLocal(e.x, e.y) -- This line is changed
e.target.touchjoint:setTarget( x, y ) -- This line is changed
xOffset, yOffset = calcTouchOffset( e )
else
transition.to( allElements, { time = 200, x = 0, y = 0, xScale = 1, yScale = 1, })
e.target.hasFocus = false
e.target.touchjoint:removeSelf()
e.target.touchjoint = nil
stage:setFocus( nil )
xOffset, yOffset = 0, 0
end
return true
end
By replacing x=body.x by x=0 and y=body.y by y=0
Cheers :)

Related

Corona SDK - Removing drawn line group and then drawing again

I'm trying to remove any lines I've drawn with a widget button and then allow the player to draw again. I've tried to delete the group several times...and it works...but then when I go to draw again it crashes! Any help with this scenario?
display.setStatusBar( display.HiddenStatusBar )
local physics = require "physics"
physics.start()
local widget = require( "widget" )
local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 0
local kittenCrate = display.newRect(10,10,25,25)
physics.addBody(kittenCrate, "dynamic", { density = 1, friction = 0.5, bounce = 1.6})
local function distanceBetween(x1, y1, x2, y2)
local dist_x = x2 - x1
local dist_y = y2 - y1
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end
local function drawLine(e)
if(e.phase == "began") then
prevX = e.x
prevY = e.y
isDrawing = true
i = i + 1
elseif(e.phase == "moved") then
local distance = distanceBetween(prevX, prevY, e.x, e.y)
if(isDrawing and distance < 100) then
if(lines[i]) then lineGroup:remove(i) end
lines[i] = display.newLine(prevX, prevY, e.x, e.y)
lines[i]:setColor(255, 255, 0)
lines[i].width = 5
local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = 1, shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(lines[i])
end
elseif(e.phase == "ended") then
isDrawing = false
end
end
Runtime:addEventListener("touch",drawLine)
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
print( "Button was pressed and released" )
end
end
-- Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = "button1",
label = "Remove Rifts",
onEvent = handleButtonEvent
}
I changed your code to the below and it works ok but not perfect. You have to follow your code to see exactly what it does. Although it is strange that Corona crashes when not finding your object.
display.setStatusBar( display.HiddenStatusBar )
local physics = require "physics"
physics.start()
local widget = require( "widget" )
local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
i = 0
local kittenCrate = display.newRect(10,10,25,25)
physics.addBody(kittenCrate, "dynamic", { density = 1, friction = 0.5, bounce = 1.6})
local function distanceBetween(x1, y1, x2, y2)
local dist_x = x2 - x1
local dist_y = y2 - y1
local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
return distanceBetween
end
local function drawLine(e)
if(e.phase == "began") then
prevX = e.x
prevY = e.y
isDrawing = true
i = i + 1
lineGroup.isVisible =true
elseif(e.phase == "moved") then
local distance = distanceBetween(prevX, prevY, e.x, e.y)
if(isDrawing and distance < 100) then
if(lines[i]) then lineGroup:remove(i) end
lines[i] = display.newLine(prevX, prevY, e.x, e.y)
lines[i]:setColor(255, 255, 0)
lines[i].width = 5
local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = 1, shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(lines[i])
end
elseif(e.phase == "ended") then
isDrawing = false
end
end
Runtime:addEventListener("touch",drawLine)
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
print( "Button was pressed and released" )
i=0
lineGroup.isVisible = false
end
end
-- Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = "button1",
label = "Remove Rifts",
onEvent = handleButtonEvent
}

How to change the direction (angle) of bullet after collision in the direction of motion in corona sdk

I want to change the bullet angle after collision, it should change the direction in which it is moving after collision, and must stop rotating after collision.... please give any suggestions... thanks
Sample code is written below
local physics = require("physics")
physics.start()
local obj
physics.setScale( 60 )
physics.setGravity( 0, 0 )
display.setStatusBar( display.HiddenStatusBar )
local obstacle = display.newCircle( 250, 250, 60 )
obstacle.x = display.contentWidth/2; obstacle.y = 200
physics.addBody( obstacle, { density=150, friction=0.2, bounce=0} )
obstacle.isBullet=true
obj = display.newRect(0, 0,20,40)
obj.x = display.contentWidth/2; obj.y = 780
obj.isBullet = true
obj.color = "white"
physics.addBody( obj, { density=1, friction=0.4, bounce=0.1} )
local target = display.newCircle( 250, 250, 60 )
target.x = obj.x; target.y = obj.y; target.alpha = 0
local function Shot( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0
target.x = t.x
target.y = t.y
startRotation = function()
target.rotation = target.rotation + 4
end
Runtime:addEventListener( "enterFrame", startRotation )
local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil
elseif t.isFocus then
if "moved" == phase then
if ( myLine ) then
myLine.parent:remove( myLine ) -- erase previous line, if any
end
myLine = display.newLine( t.x,t.y, event.x,event.y )
myLine:setColor( 255, 255, 255, 50 )
myLine.width = 8
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
local stopRotation = function()
Runtime:removeEventListener( "enterFrame", startRotation )
end
local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )
if ( myLine ) then
myLine.parent:remove( myLine )
end
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )
end
end
return true
end
obj:addEventListener( "touch", Shot)
local physics = require "physics"
physics.start()
local rect1 = display.newRect(480,0,10,320)
physics.addBody(rect1, "static")
local rect2 = display.newRect(20,100,20,20)
local function shot()
physics.addBody(rect2, "dynamic")
rect2:applyForce( 1000, 0, rect2.x, rect2.y )
rect2.myName = "rect2"
end
rect2:addEventListener("touch", shot)

Corona SDK physics with line, why does the line turn into rectangle in the physics engine?

The issue I am having is simple. I have an object in the center of the screen, and then a circle orbiting that object. what i want to do is have the object orbit around the object, when the use touches the screen it will orbit around the object and stop facing where the user tapped the screen.
I am having HUGE amount of difficulty trying to get this work. but what i figured i would do is try to draw a line from the center of the object to where the use clicks and then the ball orbiting the planet hits the line it will stop (simple enough). the problem i am having is that the line object (when viewed in hybrid mode) is more of a rectangle then it is a line (in the physics engine so this messes with my expected result. does anyone have any idea on what im doing wrong here? or how corona is handling this? and how to possibly fix it? or any other solution would work great as well.
physics = require("physics") ; physics.start() ; physics.setGravity( 0,0 ) ;
physics.setDrawMode( "hybrid" )
display.setStatusBar( display.HiddenStatusBar )
math.randomseed( os.time() )
--set up some references and other variables
local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)
local cw, ch = display.contentWidth, display.contentHeight
--set up collision filters
local screenFilter = { categoryBits=2, maskBits=1 }
local objFilter = { categoryBits=1, maskBits=14 }
local fieldFilter = { categoryBits=4, maskBits=1 }
local magnetFilter = { categoryBits=8, maskBits=1 }
--set initial magnet pull
local magnetPull = 0.85
--set up world and background
local screenBounds = display.newRect( -ox, -oy, display.contentWidth+ox+ox, display.contentHeight+oy+oy )
screenBounds.name = "screenBounds"
screenBounds.isVisible = false ; physics.addBody( screenBounds, "static", { isSensor=true, filter=screenFilter } )
local function newPositionVelocity( object )
local math_random = math.random
local side = math_random( 1,4 ) ; local posX ; local posY ; local velX ; local velY
if ( side == 1 or side == 3 ) then
posX = math_random(0,display.pixelHeight)
velX = math_random( -10,10 ) * 5
if ( side == 1 ) then posY = -oy-40 ; velY = math_random( 8,18 ) * 16
else posY = display.contentHeight+oy+40 ; velY = math_random( 8,16 ) * -16
end
else
posY = math_random(0,display.pixelWidth)
velY = math_random( -10,10 ) * 5
if ( side == 4 ) then posX = -ox-40 ; velX = math_random( 8,16 ) * 16
else posX = display.contentWidth+ox+40 ; velX = math_random( 8,16 ) * -16
end
end
object.x = posX ; object.y = posY
object:setLinearVelocity( velX, velY )
object.angularVelocity = math_random( -3,3 ) * 40
object.alpha = 1
end
require( "repeller");
local rect = createRepeller(15, "rect",cw/2, cw/2);
local function objectCollide( self, event )
local otherName = event.other.name;
local function onDelay( event )
local action = ""
if ( event.source ) then action = event.source.action; timer.cancel( event.source ) end
if ( action == "makeJoint" ) then
self.hasJoint = true
self.touchJoint = physics.newJoint( "touch", self, self.x, self.y )
self.touchJoint.frequency = magnetPull
self.touchJoint.dampingRatio = 0.0
self.touchJoint:setTarget( 512, 384 )
elseif ( action == "leftField" ) then
self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil
else
if ( self.hasJoint == true ) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil end
newPositionVelocity( self )
end
end
if ( event.phase == "ended" and otherName == "screenBounds" ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftScreen"
elseif ( event.phase == "began" and otherName == "rect" ) then
transition.to( self, { time=400, alpha=0, onComplete=onDelay } )
elseif ( event.phase == "began" and otherName == "field" and self.hasJoint == false ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "makeJoint"
elseif ( event.phase == "ended" and otherName == "field" and self.hasJoint == true ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftField"
end
end
local function setupWorld()
for i=1, 0 do
local obj = display.newCircle(0,0, 12 )
physics.addBody( obj, "dynamic", { bounce=.3, radius=12, density = 0.0 --[[,filter=objFilter]] } )
obj. isBullet = true;
newPositionVelocity( obj )
obj.hasJoint = false;
obj.name = "ball";
obj.collision = objectCollide ; obj:addEventListener( "collision", obj )
end
local field = display.newCircle(cw/2, ch/2, 320);
field.alpha = 0.2;
field.name = "field";
field.x = display.contentCenterX ; field.y = display.contentCenterY;
physics.addBody( field, "static", { isSensor=true, radius=320, filter=fieldFilter });
magnet = display.newCircle(cw/2, ch/2, 40 )
magnet.name = "magnet"
magnet.x = display.contentCenterX ; magnet.y = display.contentCenterY
physics.addBody( magnet, "static", { bounce=0, radius=40, filter=magnetFilter } )
end
setupWorld()
deg = 0.0;
local prevPosY = 0
local line = display.newLine(cw/2,ch/2,cw/2,ch/2);
local function onTouch(event)
if line ~= nil then
line:removeSelf();
end
if event.phase == "began" then
prevPosY = event.y
end
if(event.y >= prevPosY) then
myJoint.motorSpeed = -(1.5 * prevPosY - event.y);
elseif(event.y <= prevPosY) then
myJoint.motorSpeed = 1.5 * prevPosY - event.y;
end
if(prevPosY - event.y == 0) then
--myJoint.motorSpeed = 0;
end
prevPosY = event.y;
line = display.newLine(cw/2,ch/2,event.x, event.y);
line.width= 1;
line.name = "line";
physics.addBody(line, { isSensor = true })
local function lineCol(self, event)
if (event.other.name=="rect") then
myJoint.motorSpeed = 0;
print(event.other.name, event.name);
--rect:removeSelf();
end
end
line.collision = lineCol;
line:addEventListener("collision", line);
end
line = display.newLine(cw/2,ch/2, 200 , 250);
line.width= 1;
line.name = "line";
physics.addBody(line, { isSensor = true, shape=line })
--Runtime:addEventListener( "collision", lineCol)
physics.addBody(rect,"dynamic", {bounce = 0, density = 0})
myJoint = physics.newJoint( "pivot", rect, magnet, cw/2, ch/2);
myJoint.isMotorEnabled = true;
myJoint.motorSpeed = 0;
myJoint.maxMotorTorque = 100;
Runtime:addEventListener("touch", onTouch);
and my "repeller.lua"
function createRepeller(size, name, locX, locY)
local rep = display.newCircle(locX,locY, size);
rep.name = name;
return rep;
end
In Corona, lines aren't really applicable for physics bodies... as in, you can apply one, but it probably won't be the shape you expect. What I would advise is to create a very thin rectangle (a newRect()) object and then rotate/position that line to the point where you need. Or, just use math formulas like the other user suggests.
Best of luck,
Brent

Corona SDK Physics goes on and off while object is in motion

Below is my main file. essentially what is happening is that the collision is working when the "rect" object is not moving but as soon as i move the rect object, the collision stops taking affect. I think that it knows it is colliding but it does not do anything (like interaction with the objects at all). does anyone have any thoughts, why this is happening? and how to fix it?
local physics = require("physics") ; physics.start() ; physics.setGravity( 0,0 ) ;
physics.setDrawMode( "debug" )
display.setStatusBar( display.HiddenStatusBar )
math.randomseed( os.time() )
physics.setVelocityIterations(10);
physics.setPositionIterations(20);
--set up some references and other variables
local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)
local cw, ch = display.contentWidth, display.contentHeight
--set up collision filters
local screenFilter = { categoryBits=2, maskBits=1 }
local objFilter = { categoryBits=1, maskBits=14 }
local fieldFilter = { categoryBits=4, maskBits=1 }
local magnetFilter = { categoryBits=8, maskBits=1 }
--set initial magnet pull
local magnetPull = 0.25
--set up world and background
local screenBounds = display.newRect( -ox, -oy, display.contentWidth+ox+ox, display.contentHeight+oy+oy )
screenBounds.name = "screenBounds"
screenBounds.isVisible = false ; physics.addBody( screenBounds, "static", { isSensor=true, filter=screenFilter } )
local function newPositionVelocity( object )
local math_random = math.random
local side = math_random( 1,4 ) ; local posX ; local posY ; local velX ; local velY
if ( side == 1 or side == 3 ) then
posX = math_random(0,display.pixelHeight)
velX = math_random( -10,10 ) * 5
if ( side == 1 ) then posY = -oy-40 ; velY = math_random( 8,18 ) * 16
else posY = display.contentHeight+oy+40 ; velY = math_random( 8,16 ) * -16
end
else
posY = math_random(0,display.pixelWidth)
velY = math_random( -10,10 ) * 5
if ( side == 4 ) then posX = -ox-40 ; velX = math_random( 8,16 ) * 16
else posX = display.contentWidth+ox+40 ; velX = math_random( 8,16 ) * -16
end
end
object.x = posX ; object.y = posY
object:setLinearVelocity( velX, velY )
object.angularVelocity = math_random( -3,3 ) * 40
object.alpha = 1
end
local rect = display.newRect(100,100,100,60);
local offset = 250
rect.x = cw/2;
rect.y = ch/2-offset;
--rect.xReference = 40;
rect.yReference = offset;
local function objectCollide( self, event )
local otherName = event.other.name
print(event.other.name);
local function onDelay( event )
local action = ""
if ( event.source ) then action = event.source.action ; timer.cancel( event.source ) end
if ( action == "makeJoint" ) then
self.hasJoint = true
self.touchJoint = physics.newJoint( "touch", self, self.x, self.y )
self.touchJoint.frequency = magnetPull
self.touchJoint.dampingRatio = 0.0
self.touchJoint:setTarget( 512, 384 )
elseif ( action == "leftField" ) then
self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil
else
if ( self.hasJoint == true ) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil end
newPositionVelocity( self )
end
end
if ( event.phase == "ended" and otherName == "screenBounds" ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftScreen"
elseif ( event.phase == "began" and otherName == "magnet" ) then
transition.to( self, { time=400, alpha=0, onComplete=onDelay } )
elseif ( event.phase == "began" and otherName == "field" and self.hasJoint == false ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "makeJoint"
elseif ( event.phase == "ended" and otherName == "field" and self.hasJoint == true ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftField"
end
end
local function setupWorld()
for i=1, 20 do
local obj = display.newCircle(0,0, 12 )
physics.addBody( obj, "dynamic", { bounce=.3, radius=12, density = .2 --[[,filter=objFilter]] } )
obj. isBullet = true;
newPositionVelocity( obj )
obj.hasJoint = false
obj.collision = objectCollide ; obj:addEventListener( "collision", obj )
end
local field = display.newCircle(cw/2, ch/2, 320);
field.alpha = 0.2;
field.name = "field";
field.x = display.contentCenterX ; field.y = display.contentCenterY;
physics.addBody( field, "static", { isSensor=true, radius=320, filter=fieldFilter });
magnet = display.newCircle(cw/2, ch/2, 40 )
magnet.name = "magnet"
magnet.x = display.contentCenterX ; magnet.y = display.contentCenterY
physics.addBody( magnet, "static", { bounce=0, radius=40, filter=magnetFilter } )
end
setupWorld()
physics.addBody(rect,"kinematic", { bounce = 1, density = 1})
deg = 0.0;
prevY = 0;
local function onTouch(event)
if event.phase == "began" then
prevY = event.y
end
if (deg<=360) then
deg=0;
end
if(event.y >= prevY) then
deg = deg + 10;
elseif(event.y <= prevY) then
deg = deg -10;
end
prevY = event.y
rect:rotate(deg);
end
Runtime:addEventListener("touch", onTouch);
kinematic bodies don't generate collisions. It needs to be dynamic.
It sounds like your body is going to sleep.
http://developer.coronalabs.com/content/game-edition-physics-bodies#body.isSleepingAllowed
Try:
rect.isSleepingAllowed = false

Flipboard Effect on Corona SDK using Lua

.
Hi, everybody,
This is not a question, is just to show the code if someone's interested on it.
I used the FLIPBOOK project presented in the Corona Share Your Code site, and changed it a little bit. (http://developer.coronalabs.com/code/flipbook-module-realistic-page-curling-effect).
The main.lua file:
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
local Flipbook = require 'flipbook'
local nrPages = 6
local myFirstFlipbook = Flipbook:newFlipbook(nrPages, "mainshadow2.png" )
for x=1,nrPages do
local grp1 = display.newGroup()
--grp1:setReferencePoint( display.TopLeftReferencePoint )
local rct1 = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
--grp1.isVisible = false
local txt1 = display.newText("Meu texto "..x.." ...", 2, display.contentHeight - 20, native.systemFont, 16)
txt1:setTextColor(0, 0, 0)
local img1 = display.newImageRect( "background"..x..".png",display.contentWidth/5*4,display.contentHeight/5*4 )
img1.x = display.contentWidth/2
img1.y = display.contentHeight/2
grp1:insert(rct1)
grp1:insert(img1)
grp1:insert(txt1)
local grp2 = display.newGroup()
--grp4:setReferencePoint( display.TopLeftReferencePoint )
local rct2 = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
--grp1.isVisible = false
local txt2 = display.newText("Meu texto "..x.." ...", 2, display.contentHeight - 20, native.systemFont, 16)
txt2:setTextColor(0, 0, 0)
local img2 = display.newImageRect( "background"..x..".png",display.contentWidth/5*4,display.contentHeight/5*4 )
img2.x = display.contentWidth/2
img2.y = display.contentHeight/2
grp2:insert(rct2)
grp2:insert(img2)
grp2:insert(txt2)
myFirstFlipbook:addPage( grp1 )
myFirstFlipbook:addMirror( nrPages, grp2 )
end
And flipbook.lua file:
-----------------------------------------------------------------------------------------
--
-- flipbook.lua
--
-----------------------------------------------------------------------------------------
local Flipbook = {
mask = graphics.newMask( "mask1.png" ),
mask2 = graphics.newMask( "mask2.png" ),
currentPage = 1,
pages = {},
backPages = {},
displayGroup = display.newGroup(),
control = 2
}
Flipbook.__index = Flipbook
function Flipbook:newFlipbook(nrPages, newBackShadow, newBasicShadow, newCurlShadow) --Create a new flipbook
local tempFlipbook = setmetatable({},self)
-- Adds a shadow for where the curled page overlaps itself
tempFlipbook.backShadow = display.newImage( newBackShadow )
tempFlipbook.backShadow.isHitTestMasked = false
tempFlipbook.backShadow.y = display.contentHeight / 2
tempFlipbook.backShadow.isVisible = false
tempFlipbook.displayGroup:insert( nrPages + 1, tempFlipbook.backShadow )
-- Adds event listener
tempFlipbook.displayGroup:addEventListener( "touch", tempFlipbook )
----
return tempFlipbook
end
function Flipbook:addPage( newPageGroup ) -- Add a new page to your flipbook
local tempPageImage = newPageGroup
tempPageImage:setMask( self.mask )
tempPageImage.maskX = display.contentWidth
tempPageImage.isHitTestMasked = false
tempPageImage:setReferencePoint( display.CenterReferencePoint )
table.insert( self.pages, tempPageImage ) -- Adds new page to pages index
self.displayGroup:insert( 1, tempPageImage ) -- Adds new page to display group
end
function Flipbook:addMirror( nrPages, newPageGroup )
local tempPageImageMirror = newPageGroup
tempPageImageMirror.isVisible = false
tempPageImageMirror:setMask( self.mask2 )
tempPageImageMirror:setReferencePoint( display.CenterReferencePoint )
table.insert( self.backPages, tempPageImageMirror ) -- Adds new page to pages index
self.displayGroup:insert( nrPages + 1, tempPageImageMirror )
end
function Flipbook:updateCurlEffect( handleX, handleY, originY ) -- Updates the curl effect assets
local hX = 0
if (handleX > display.contentWidth) then
hX = display.contentWidth
elseif (handleX < 0) then
hX = 0
else
hX = handleX
end
local controlX = 0
if (hX > display.contentWidth / 2) then
controlX = (( hX - display.contentWidth / 2) / (display.contentWidth / 2))
else
controlX = (( display.contentWidth / 2 - hX ) / (display.contentWidth / 2))
end
if controlX < 0.0001 then
controlX = 0.0001
end
if controlX > 0.9999 then
controlX = 0.9999
end
--print(alphaX)
self.pages[self.currentPage].maskX = hX
if (hX > display.contentWidth / 2) then
self.backPages[self.currentPage+1].isVisible = false
self.backPages[self.currentPage]:setMask(self.mask2)
self.backPages[self.currentPage].isVisible = true
self.backPages[self.currentPage].xReference = 0
self.backPages[self.currentPage].x = (display.contentWidth - hX)
self.backPages[self.currentPage].maskX = display.contentWidth / 2
self.backPages[self.currentPage].xScale = controlX
if (self.control > 1) then
self.backShadow:scale(-1,1)
self.control = 1
--self.backShadow.xReference = display.contentWidth / 2
self.backShadow.x = display.contentWidth + 21
self.backShadow.maskX = 0
end
else
self.backPages[self.currentPage].isVisible = false
self.backPages[self.currentPage+1].isVisible = true
self.backPages[self.currentPage+1]:setMask(self.mask)
self.backPages[self.currentPage+1]:toFront()
self.backPages[self.currentPage+1].xReference = 0
self.backPages[self.currentPage+1].x = hX
self.backPages[self.currentPage+1].maskX = display.contentWidth / 2
self.backPages[self.currentPage+1].xScale = controlX
if (self.control < 2) then
self.backShadow:scale(-1,1)
self.control = 2
--self.backShadow.xReference = display.contentWidth / 2
self.backShadow.x = -21
self.backShadow.maskX = 0
end
end
self.backShadow.alpha = controlX
--self.backShadow.sizeX = display.contentWidth
end
function Flipbook:setAssetsVisible( isVisibleBoolean ) -- Curl effect assets visibility switch box
if isVisibleBoolean then
self.backPages[self.currentPage].isVisible = isVisibleBoolean
self.backShadow.isVisible = isVisibleBoolean
self.backShadow:toFront()
self.backPages[self.currentPage]:toFront()
else
--for k,v in pairs(self.backPages) do
for k=1, #self.backPages do
self.backPages[k].isVisible = isVisibleBoolean
end
self.backShadow.isVisible = isVisibleBoolean
end
end
function Flipbook:touch( event )
if event.phase == "began" then
if event.xStart > 5 * display.contentWidth / 6 and self.currentPage < # self.pages
or event.xStart < display.contentWidth / 6 and self.currentPage > 1 then
if event.xStart < display.contentWidth / 6 then
self.currentPage = self.currentPage - 1
self.pages[self.currentPage].isVisible = true
end
display.getCurrentStage():setFocus( self.displayGroup )
self.isFocus = true
self:setAssetsVisible( true )
else
return false
end
end
if self.isFocus then
self:updateCurlEffect( event.x, event.y, event.yStart )
if event.phase == "ended" or event.phase == "cancelled" then
self:updateCurlEffect( display.contentWidth, 0, 0 )
if math.abs(event.x - event.xStart) > display.contentWidth / 6 then
if event.xStart > 5 * display.contentWidth / 6 and self.currentPage < # self.pages then
self.pages[self.currentPage].isVisible = false
self.currentPage = self.currentPage + 1
end
self:setAssetsVisible( false )
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
else
if event.xStart < display.contentWidth / 6 and self.currentPage > 1 then
self.pages[self.currentPage].isVisible = false
self.currentPage = self.currentPage + 1
end
self:setAssetsVisible( false )
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
end
end
return true
end
end
return Flipbook
For the files backgroundX.png I used the files from FLIPBOOK project.
And for the mainshadow2.jpg, I used a hole black image with a 75% alpha, based on the mainshadow.jpg size from the FLIPBOOK project.
Hope it helps.
Best regards.
Alexandre Flores de Almeida
It's already answered. It's not a question, but code for the community.

Resources