I am working on a tattoo drawing game for mobile. There is a part in which player have to draw fill on the tattoo by finger. There I have to place a progress bar which will display how much area has been filled by players color. How can I trace the progress? There are almost 25 tattoos where this progress bar will work. So I need a generic technique.
Here is some source code I wrote up that might help you:
local endPoint, startPoint
local lines, line, lx, ly, sx, sy = {}
local points = {}
r,g,b = 255,0,0
lineWidth = 10
local drawPacket
local odd = true
local function draw(event)
if event.phase == "began" then
startPoint = display.newCircle(0,0,lineWidth/2)
startPoint.x, startPoint.y = event.x, event.y
startPoint:setFillColor(r,g,b)
if startPoint.x <350 or startPoint.x > 700 then
director:changeScene("fail")
end
endPoint = display.newCircle(-100,0,lineWidth/2)
endPoint:setFillColor(r,g,b)
lineGroup:insert(startPoint)
lineGroup:insert(endPoint)
elseif event.phase == "moved" then
if not line then
print "I am now drawing the line"
line = display.newLine(startPoint.x, startPoint.y, event.x, event.y)
lines[ #lines + 1 ] = line
line.width = lineWidth
line:setColor(r,g,b)
lx,ly = startPoint.x , startPoint.y
sx,sy = event.x, event.y
lineGroup:insert(line)
else
if math.sqrt((lx-event.x)^2+(ly-event.y)^2) > 2 then
--print "I am now appending the line"
line:append( event.x, event.y)
lx, ly = event.x, event.y
end
endPoint.x = event.x
endPoint.y = event.y
if odd then
points[#points+1] = event.x
points[#points+1] = event.y
end
odd = not odd
end
elseif event.phase == "ended" then
if win == true then
if endPoint.x <350 or endPoint.x > 700 then
director:changeScene("fail")
else
director:changeScene("scene11")
end
else
director:changeScene("fail")
end
line = nil
endPoint.x, endPoint.y = event.x, event.y
print "I have ended my touch, sending data"
points = {}
end
end
background:addEventListener("touch", draw)
Make sure to replace "background" with your image that you want the user to "tattoo". Good luck with your project.
Related
mate, i need help for using scrollview in corona sdk, fyi im new in corona sdk..
im trying to make some simple puzzle game here
so here we go pieces code:
local widget = require("widget")
_W = display.contentWidth -- Get device screen width
_H = display.contentHeight -- Get device screen height
imgPiece = 3
imgPieces = {}
imgPieceStartingY = { 80,230,380 }
imgPieceWidth, imgPieceHeight = 120, 120
-- Get some background image here
bg = display.newImageRect("bg.png", _W, _H)
bg.x = _W/2
bg.y = _H/2
-- Create some scrollview
scrollView = widget.newScrollView{
top=100,
left = _W/2,
height=_H/2,
width = 160,
hideBackground = true,
scrollWidth = 50 ,
scrollHeight = 1000 }
-- Set image inside the scrollview
for i = 1, imgPiece do
imgPieces[i] = display.newImageRect("s"..i..".jpg", imgPieceWidth, imgPieceHeight)
imgPieces[i].x = 85 -- Set Starting X position for img piece
imgPieces[i].y = imgPieceStartingY[i] -- Set Starting Y position for img piece
imgPieces[i].id = i
imgPieces[i].place = "start"
scrollView:insert(imgPieces[i])
end
the problem is, when i try to drag the image out from the scrollview the image will dissapear.
how can i make the image still visible when object is leaving the scrollview boundary box(which i set in 160 width) while user still dragging and hold the image on?
thx
I had the same problem and I solved with the image inserted in the main group in "began" phase.
local function dragElements(event)
local phase = event.phase
local obj = event.target
if phase == "began" then
display.getCurrentStage():setFocus(obj)
grp:insert(obj)
obj.StartMoveX = event.x
obj.StartMoveY = event.y
elseif phase == "moved" then
obj.x = (event.x - event.xStart) + obj.StartMoveX
obj.y = (event.y - event.yStart) + obj.StartMoveY
elseif phase == "ended" or phase == "canceled" then
display.getCurrentStage():setFocus(nil)
end
return true
end
I would like to convert local line into a global variable so that I can refer to it later on. This is my code so far:
local physics = require "physics"
physics.start()
local line
lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 0
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
if(line) then
lineGroup:remove(1)
line = nil
end
prevX = e.x
prevY = e.y
isDrawing = true
elseif(e.phase == "moved") then
local distance = distanceBetween(prevX, prevY, e.x, e.y)
if(isDrawing and distance < 100) then
if(line) then lineGroup:remove(1) end
line = display.newLine(prevX, prevY, e.x, e.y)
line:setStrokeColor( 0.5,0,1 )
line.strokeWidth = 5
local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(line, "static", { density = 1,
friction = 0.5,
bounce = 2,
shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(line)
end
elseif(e.phase == "ended") then
isDrawing = false
end
end
Runtime:addEventListener("touch",drawLine)
Whenever I try to refer to line in this next function, I get an error message saying:
attempt to index global 'line'(a nil value):
function onCollision(e)
audio.play(bounceSnd)
score.text = tostring(tonumber(score.text) + 1)
score.x = 300
end
gameListeners('add')
end
function gameListeners(action)
if(action == 'add') then
line:addEventListener( 'collision', onCollision )
else
line:addEventListener( 'collision', onCollision)
end
end
If anyone could help, I would be extremely grateful.
You could add the eventlistener when you create the line, not from a seperated function.
If you need additional control for when the line responds to collisions and when not, you can probably do this via some flags for the physics body. (I never used the physics module)
That looks like you don't have a mydata.lua file anywhere. I know in the examples they use that reference a lot, but you have to actually create a mydata.lua file if you want to use that. It's a technique they use to have a global object that can be referenced in different files. For example:
mydata.lua
local M = {}
M.someVariable = WhateverYouWant
return M
Then in any file you want to have access to mydata:
local mydata = require("mydata")
Then you can use it in that file.
--***************************************************
-- drawButtons() -->
--***************************************************
local flyPlane = function(event)
if event.phase == "began" and gameIsActive == true then
print("began")
physics.setGravity( 0, -10 )
elseif event.phase == "ended" and gameIsActive == true then
print("ended")
physics.setGravity( 0, 7 )
inBound = true
elseif event.phase == "moved" and gameIsActive == true then
-- do nothing
end
end
--***************************************************
-- keepPlaneInBound() -->
--***************************************************
local keepPlaneInBound = function()
-- print("checking")
if paperPlane.y <= paperPlane.height + 10 and gameIsActive == true and inBound == true then
inBound = false
paperPlane.y = paperPlane.height + 12
end
if paperPlane.y > display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then
paperPlane.y = display.contentHeight - paperPlane.height - scale.height
end
end
The function flyPlane is set on "touch" eventlistner and keepPlaneInBound has been set to "enterframe" eventlistner. What i want to achieve is that when the plane exceeds the upper bound limit. Its previous physcis.setGravity value to be completely removed and the new value to be assigned to it when the user lifts the finger (when event = "ended").
What happens odd is that it does not accept the new physics.setGravity() value. In case i use physics.pause() and then assign it the new gravity value. On physics.start it first completes the part of the previous setGravity value hence moving it upwards again.. :/
You shouldn't have "flyPlane" as the callback in "enterFrame". You should have something like this:
local function flyPlane(event)
your_code()
end
local function keepPlaneInBound(event)
your_code()
end
paperPlane:addEventListener("touch", flyPlane)
Runtime:addEventListener("enterFrame", keepPlaneInBound)
That is all you need. (Given that your game logic is correct)
folks. i got what i was looking for. The technique to remove the force applying onto the physics body completely in order to assign it a new gravity value is to simply change its bodyType to a different type and then assigning back the required bodyType again to the object. This way the effect of the previous gravity value on your physics object gets completely removed.This is what i did. It surely worked for me.
-- drawButtons() -->
--***************************************************
local flyPlane = function(event)
if event.phase == "began" and gameIsActive == true then
print("began")
-- paperPlane.bodyType = "dynamic"
physics.setGravity( 0, -10 )
-- physics.start()
elseif event.phase == "ended" and gameIsActive == true then
print("ended")
-- paperPlane.bodyType = "dynamic"
physics.setGravity( 0, 7 )
-- physics.start()
elseif event.phase == "moved" and gameIsActive == true then
end
end
--***************************************************
-- keepPlaneInBound() -->
--***************************************************
local keepPlaneInBound = function()
-- print("checking")
if paperPlane.y <= paperPlane.height + 10 and gameIsActive == true then
print("Out of Bound -y: ", paperPlane.bodyType)
physics.pause()
paperPlane.bodyType = "static"
paperPlane.y = paperPlane.height + 11
elseif paperPlane.y > display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then
print("Out of Bound +y: ", paperPlane.bodyType)
physics.pause()
paperPlane.bodyType = "static"
paperPlane.y = display.contentHeight - paperPlane.height - scale.height - 1
else
print("In Bound: ", paperPlane.bodyType)
paperPlane.bodyType = "dynamic"
physics.start()
end
end
hi i need help with this code. i am trying to make three ball bounce around by animating it. i i found some code that code be some help in the Corona sdk sample code. i try to change the image from a circle to a image i have in my folder but now luck it wont work. also i am using story board API i really need this thanks i am new to corona sdk.
this is the sample code
this code works for me but i want to add multiple balloons the would bounce around in their own direction and bounce in to each other and change direction. I am kinda stuck on this can someone help thanks :)....................
here the code you ask for sorry for taking so long
local function newBall( params )
local xpos = display.contentWidth*0.5
local ypos = display.contentHeight*0.5
local circle = display.newCircle( xpos, ypos, params.radius );
circle:setFillColor( params.r, params.g, params.b, 255 );
circle.xdir = params.xdir
circle.ydir = params.ydir
circle.xspeed = params.xspeed
circle.yspeed = params.yspeed
circle.radius = params.radius
return circle
end
local params = {
{ radius=20, xdir=1, ydir=1, xspeed=2.8, yspeed=6.1, r=255, g=0, b=0 },
{ radius=12, xdir=1, ydir=1, xspeed=3.8, yspeed=4.2, r=255, g=255, b=0 },
{ radius=15, xdir=1, ydir=-1, xspeed=5.8, yspeed=5.5, r=255, g=0, b=255 },
-- newBall{ radius=10, xdir=-1, ydir=1, xspeed=3.8, yspeed=1.2 }
}
local collection = {}
-- Iterate through params array and add new balls into an array
for _,item in ipairs( params ) do
local ball = newBall( item )
collection[ #collection + 1 ] = ball
end
-- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX
function collection:enterFrame( event )
for _,ball in ipairs( collection ) do
local dx = ( ball.xspeed * ball.xdir );
local dy = ( ball.yspeed * ball.ydir );
local xNew, yNew = ball.x + dx, ball.y + dy
local radius = ball.radius
if ( xNew > screenRight - radius or xNew < screenLeft + radius ) then
ball.xdir = -ball.xdir
end
if ( yNew > screenBottom - radius or yNew < screenTop + radius ) then
ball.ydir = -ball.ydir
end
ball:translate( dx, dy )
end
end
Runtime:addEventListener( "enterFrame", collection );
can someone help me to change the images from circle to my balloon01.png ,balloon02.png and balloon03.png images in my folder. also this is the error i get when i add it to my game that include the story board API
level1.lua 157:attempt to call global "newBall" (a nil value)
i was trying to post an image but because i am new i can't. I have the code that crate the ball in the create scene that's apart of the Corona SDK story board API thanks ...:0 for your help
I've updated the code. Just try replacing all your code with the following code:
local xpos,ypos = {},{}
local xdirection,ydirection = {},{}
local xMultiplier = {2.8,3.0,4.0} -- these arrays should contain the values for each objects
local yMultiplier = {1.0,2.2,5.5} -- these arrays should contain the values for each objects
local totalImages = 3 -- no. of images/object that you need in the scene
local circle = {}
local diameter = {50,30,20} -- these arrays should contain the values for each objects
for i=1,totalImages do
xpos[i] = display.contentWidth*0.5
ypos[i] = display.contentHeight*0.5
xdirection[i] = 1
ydirection[i] = 1
circle[i] = display.newImageRect("balloon0"..i..".png",diameter[i],diameter[i])
circle[i]:setFillColor(255,0,0,255)
end
local function animate(event)
for i=1,totalImages do
xpos[i] = xpos[i] + ( xMultiplier[i] * xdirection[i] )
ypos[i] = ypos[i] + ( yMultiplier[i] * ydirection[i] )
if (xpos[i] > display.contentWidth - 20 or xpos[i] < 20) then
xdirection[i] = xdirection[i] * -1;
end
if (ypos[i] > display.contentHeight - 20 or ypos[i] < 20) then
ydirection[i] = ydirection[i] * -1;
end
circle[i]:translate( xpos[i] - circle[i].x, ypos[i] - circle[i].y)
end
end
Runtime:addEventListener( "enterFrame", animate )
Note: Make sure to place the following image files in the same folder where your main.lua resides:
balloon01.png
balloon02.png
balloon03.png
Keep Coding............ :)
I'm having some troubles when I try to reload a scene.
In my program, users are able to draw a line with their finger (it's an iOS/Android app), and it works pretty good, but, when I try to reload the scene, the console returns
"Attempt to index field 'parent' (a nil value)"
at line 78
The code I've used to perform the "drawline" and the reload (replay in this case) features is
local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = true
local i = 1
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
for i = #lines, 1, -1 do
if (lines[i]) then
if (lines[i].parent) then
lines[i].parent:remove(lines[i])
end
lines[i] = nil
end
end
lines = {}
line_number = 100
prevX = e.x
prevY = e.y
isDrawing = 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 = 3
lines[i].myName = "lines"
if(lines[i].y < 400) then
for i = #lines, 1, -1 do
if (lines[i]) then
if (lines[i].parent) then
lines[i].parent:remove(lines[i])
end
lines[i] = nil
end
end
end
local dist_x = e.x - prevX
local dist_y = e.y - prevY
physics.addBody(lines[i], "static",
{ density = 1, friction = 0.5, bounce =
-0.8, shape = {0, 0, dist_x, dist_y, 0, 0} } )
lineGroup:insert(lines[i])
end
elseif(e.phase == "ended") then
isDrawing = true
end
return lineGroup
end
Runtime:addEventListener("touch",drawLine)
where line 78 is:
lineGroup:insert(lines[i])
I restart my game using
local replayBTN = display.newImageRect("images/replay.png", 25, 25)
replayBTN.alpha = 1
replayBTN.x = _W/2
replayBTN.y = _H/2
localGroup:insert( replayBTN)
function replay(event)
director:changeScene("game")
return true
end
replayBTN:addEventListener("touch", replay)
How can I fix my problem? THANKS ;)
the moment you try to go to another scene don't reset all the values because you set it to nil so when you go back to your scene again you get an error and i see your using director class to change scene and go to the same scene have you try using storyboard i don't know if it will be much convenient to you because i'm thinking that your just trying to reset the value of the line storyboard can recreate and reset all the value as you remove the scene and go to another scene. you can visit this link to compare director class and storyboard.
http://www.coronalabs.com/blog/2012/04/17/director-to-storyboard-transition-guide/