how to detect touch area in corona sdk? - coronasdk

In my application i want to count the touches.but when i touch that image that has to be cover some area then only count will be increse.plese help me
function pop(event)
if event.phase=="began" then
elseif event.phase == "ended" then
numValue=numValue+1
print("tapcount value"..numValue)
count.text=numValue
end
end
lips:addEventListener("touch",pop)

Do you want to count the taps or the touches? If it's taps, which is what it looks like, take a look at this tutorial for doing just that: http://corona.techority.com/2012/07/30/how-to-use-double-tap-in-your-corona-app/ I know the title says "double tap" but it will work for any kind of tap counting.

Is this is what you are looking for..?
local obj_1 = ..... ; obj.tag = 1
local lips = ..... ; lips.tag = 2
local count = .....
local numValue = 0
local clickedObj_tag = 0
local function pop(e)
if(clickedObj_tag~=e.target.tag)then
clickedObj_tag = e.target.tag
-- cover/highlight your object
else
numValue=numValue+1
print("tapcount value"..numValue)
count.text=numValue
-- do rest of your code
end
lips:addEventListener("touch",pop)
obj_1:addEventListener("touch",pop)

Related

how to: if the player touched anywhere but the buttons on the screen in a specific arrangement, he will lose a life. - Corona sdk

I am trying to make a game using corona SDK.
The game is about 3 buttons, the player needs to press them in a specific arrangement (1==>2==>3). and if the player touched anywhere but the buttons in that arrangement, he will lose a life.
my problem is in the 2nd part. how can i do that.
any suggestions please.
Thnx in advance.
Try (not tested)
numberOfTouch = 0
match = {1, 2, 3}
...
button1.id = 1
button2.id = 2
button3.id = 3
...
local function mylistener(event)
local phase = event.phase
local target = event.target
numberOfTouch = numberOfTouch + 1
if phase == "began" then
if match[numberOfTouch] == target.id then
if #match == numberOfTouch then
-- You win
end
else
-- You lose
end
end
return true -- to stop propagate event to more objects
end
...
button1:addEventListener("touch", mylistener)
button2:addEventListener("touch", mylistener)
button3:addEventListener("touch", mylistener)

ROBLOX Get name of a player that clicked a brick

I have this script in a brick:
local giver = 1
function onClicked()
game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value = game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
Now I need to somehow get the player's name that clicked it and put it where I need to.
The ClickDetectors's MouseClick event have the "Clicking Player" as parameter, so you can do it like this:
local giver = 1
function onClicked(Player)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + giver
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
However, this requires the FilteringEnabled to be set to false (not recomended).
To solve this, make a LocalScript in the brick with the code:
script.Parent.ClickDetector.MouseClick:connect(function(Player)
game.ReplicatedStorage:WaitForChild("BrickClick"):InvokeServer(script.Parent)
end)
And in a Script placed in the ServerScriptService put:
local Listener = game.ReplicatedStorage:FindFirstChild("BrickClick")
if Listener == nil then
Listener = Instance.new("RemoteFunction")
Listener.Name = "BrickClick"
Listener.Parent = game.ReplicatedStorage
end
function Listener.OnServerInvoke(Player,Brick)
Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + 1
end
I won't point you to the wiki page for further reading, even thought it contains a bit of what you need, it contains too little information.
The ClickDetector's MouseClick info, the guide about FilteringEnabled and the guide about RemoteFunctions are better.
Try this!
script.Parent.MouseClick:Connect(function(Player)
-- Kill The Player
-- The parameter is referring to game.Players So if you want to do a kill button use .Character
Player.Character:BreakJoints()
-- Change The Color To Red (Other details)
script.Parent.Parent.BrickColor = BrickColor.new("Really red")
script.Parent.MaxActivationDistance = 0
-- Wait 4 Secs
wait(5)
-- Change The Color To Green
script.Parent.Parent.BrickColor = BrickColor.new("Lime green")
script.Parent.MaxActivationDistance = 50
end)

Can someone test my Corona project and identify the issue with my jump function?

I'm all very new to this and everything in this project is just placeholders and scrap work. Some of you may recognize some of the graphics used from other Corona tutorials, but it's to help me get better, so don't be too judgemental. Here's a link to the download for the Corona Simulator: http://www.mediafire.com/download/6a78bsewgwsiyp2/GooMan.rar
Anyways, here's my issue. The jump button seems fine at first. If I hold it down, the character constantly jumps. And if I let go, he stops. If I simultaneously hold down jump while pushing one of the arrow buttons, he'll jump in that direction. However, it seems as though if I jump once, then I hit the jump button again RIGHT as the character is making contact with the ground, that he won't jump. There's a sudden lack of responsiveness. I literally have to pause slightly and wait for the character to fully hit the ground before I can make another successful jump. Why?
And here's all the relevant code for you to look at:
I have this at the beginning to help define my goo character's position:
local spriteInAir = false
local yspeed = 0
local oldypos = 0
local holding = false
I then created the jump button.
local bJump = display.newImage("Images/jumpbutton.png")
bJump.x = 445
bJump.y = 265
bJump.xScale = 0.78
bJump.yScale = 0.78
Followed up by creating an enterFrame Runtime Event which will update my goo character's position in the Y. Running the game at 60fps if that's relevant.
function checkSpeed()
yspeed = sprite.y - oldypos
oldypos = sprite.y
if yspeed == 0 then
spriteInAir = false
else
spriteInAir = true
end
end
Runtime:addEventListener( "enterFrame", checkSpeed )
Then the meat of it all. Created a function called hold which tells the game to not only make my goo character jump, but to keep my goo character constantly jumping as long as the bJump button is held down. Works perfectly. The "jumping" function is a touch event that listens for the hold function, and all of it is executed by the bJump button listening to the jumping function.
local function hold()
if holding and spriteInAir == false then
sprite:applyForce( 0, -8, sprite.x, sprite.y )
sprite:setLinearVelocity(0, -350)
spriteInAir = true
return true
end
end
local function jumping( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
event.target.alpha = 0.6
Runtime:addEventListener( "enterFrame", hold )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
event.target.alpha = 1
Runtime:removeEventListener( "enterFrame", hold )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
spriteInAir = false
return true
end
end
return true
end
bJump:addEventListener("touch",jumping)
Anyone who can help me identify this problem, I'd greatly appreciate it!
You're using a velocity check to detect if the character is on the ground. It can take a short while to set it back to zero after colliding with Box2D, so the better way to do it is to use a collision sensor:
sprite.grounded = 0 -- Use a number to detect if the sprite is on the ground; a Boolean fails if the sprite hits two ground tiles at once
function sprite:collision(event)
if "began" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded + 1 -- ...register a ground collision
end
elseif "ended" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded - 1 -- ...unregister a ground collision
end
end
end
sprite:addEventListener("collision")
Then, in your jump function, just check to see if sprite.grounded > 0. If it is, the player is grounded.

Lifebar : How to increase lifebar oncollision with other object?

I've build my own lifebar (progressive bar) as follow :
function scene:enterScene(event)
storyboard.removeScene("start")
screenGroup = self.view
lifeBar = {}
lives = 141 -- is the global width of my full lifebar
maxLives = 141
for i = 1, maxLives do
lifeBar[i] = display.newImage("lifebar.png") --lifebar.png image width is : 1px
lifeBar[i].anchorX=0
lifeBar[i].anchorY=0.6
lifeBar[i].x = fuel_title.x +114+13.5+(lifeBar[i].contentWidth * (i - 1))
lifeBar[i].y = 37 -- start at 10,10
lifeBar[i].isVisible=true
screenGroup:insert(lifeBar[i])
end
end
So, thanks to you, i've know a function which increase the lives (livesValue.text shows the increased lives), whatever, the lifebar{} table seems to have some problems because i cannot see a difference in the lifebar image. My question is : Is there something wrong with my table lifeBar ?
if (event.other.myName == "fuel") then
if lives > maxLives then
lives = maxLives
elseif lives < #lifeBar then
lives = lives + 1
lifeBar[lives].isVisible=true
end
livesValue.text = string.format("%d", lives)
local other = event.other
timer.performWithDelay(1, function() other:removeSelf() end)
end
Runtime:addEventListener("touch", FuelManage)
Try this:
function onCollision( self, event )
if event.phase == "began" then
if event.other.myName == "fuel" then
-- increase life bar, if slots left:
if lives < #lifeBar then
lives = lives + 1
lifeBar[lives].isVisible=true
end
-- schedule the fuel object we collided with for removal:
local other = event.other -- don't want to use event as upvalue
timer.performWithDelay(1, function() other:removeSelf() end)
end
end
end
Note that you are not allowed to remove collision objects in the collision handler, you have to do this with delay, as explained on the Corona Collision docs page. So I have done that. Also you should only add lives if you there is space in the lifeBar (similarly you should fix your code to remove life only if there is more than 0 lives).

Update the target coordinates while transitioning

I am making a game in corona and I am facing a problem. I have a circle on screen and I want it to follow the touch coordinates continuously. I am using transition.to function to do so but the thing is, whenever this function get a coordinate, it completes the transition even if the coordinates are updated during the transition.
if event.phase == "began" or event.phase == "moved" then
follow = true
touchX = event.x; touchY = event.y
elseif event.phase == "ended" then
follow = false
end
And in another function, I am doing this
if follow == true then
transition.to(circle, {time = 500, x = touchX, y = touchY, transition = easing.inOutQuad})
end
The code works fine for simple touch but I want the circle to follow the touch even when it's moving.
There are some examples which may solve your problem.
Refer:
1) Flight Path posted by carlos in corona Community.
2) Move Object through a path by renvis
Sample:
local circle = display.newCircle(10,10,20)
circle.x = 160
circle.y = 160
local olderTransition
local function moveCircle(e)
if olderTransition ~= nil then
transition.cancel( olderTransition )
end
olderTransition = transition.to(circle,{time=100,x=e.x,y=e.y})
end
Runtime:addEventListener("touch",moveCircle)
Keep Coding.......... :)
You cant add a new transition to an object, which already in a transition. Thats why you should cancel the older transition first. You can try :
local olderTransition -- This should be visible outside of your function
local function blabla()
if follow == true then
if olderTransition ~= nil then
transition.cancel( olderTransition )
end
olderTransition = transition.to(circle, {time = 500, x = touchX, y = touchY, transition = easing.inOutQuad, onComplete = function() olderTransition = nil end })
end
end
Btw If you want to drag and drop objects, transitions are bad in performance way

Resources