is there a touch and hold event in Corona sdk, If not then how to do that - touch

is there a touch and hold event in Corona sdk, If not then how to do that. for example. i want to increase the radius of a circle while holding anywhere on the screen, and without moving. how to do that.
'
thanx

Try (as I know you can't change radius so I use xScale and yScale to increase circle)
local circle = display.newCircle( display.contentCenterX, display.contentCenterY, 50 )
step = 0.02
local holding = false
local function enterFrameListener()
if holding then
-- Holding button
circle.xScale = circle.xScale + step
circle.yScale = circle.yScale + step
else
-- Not holding
-- Code here
end
end
local function touchHandler( event )
if event.phase == "began" then
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.phase == "ended" or event.phase == "moved" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
end
return true
end
Runtime:addEventListener( "touch", touchHandler )
The code borrow from post from stackoverflow.com.

Related

Roblox Functions are running 20+ times

I seem to be having an issue of every time I want to pass a function when an event happens such as a Humanoid.Died() or a BindableEvent it runs the function 20+ times, also adding to an IntegerValue that many times as well. I have tried debounces, BindableEvents, connection:Disconnect(), etc. Is there something I'm doing wrong or missing?
Updated original code:
function module.damageHumanoid(attacker, player, humanoid, damage)
local connection
local debounce = false
connection = humanoid.Died:Connect(function()
if not debounce then
debounce = true
connection:Disconnect()
module.onDeath(player)
end
end)
if player:FindFirstChild("damage") == nil then
newFolder = Instance.new("Folder")
newFolder.Name = "damage"
newFolder.Parent = player
end
if newFolder:FindFirstChild(tostring(attacker)) == nil then
record = Instance.new("IntValue")
record.Name = tostring(attacker)
record.Parent = newFolder
end
if record.Value >= 100 then -- sanity check
else
record.Value = record.Value + damage
end
return humanoid:TakeDamage(damage)
end
Which triggers this code:
function module.onDeath(player)
if player:FindFirstChild("damage") ~= nil then
local folder = player:WaitForChild("damage")
local contents = folder:GetChildren()
for i,v in pairs(contents) do
local item = folder[tostring(v)]
damages[item.Name] = item.Value
end
end
local debounce = false
if not debounce then
debounce = true
module.handleKill(damages)
end
end
This is the sword script's function:
function Blow(Hit)
if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
return
end
local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
if not RightArm then
return
end
local RightGrip = RightArm:FindFirstChild("RightGrip")
if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
return
end
local character = Hit.Parent
if character == Character then
return
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health == 0 then
return
end
local player = Players:GetPlayerFromCharacter(character)
if player and player == Player then
return
end
UntagHumanoid(humanoid)
TagHumanoid(humanoid, Player)
module.damageHumanoid(plur, player, humanoid, Damage)
end
So I guess the way I was trying to use debounce isn't correct anymore. I referred to the Roblox documentation and found this: https://create.roblox.com/docs/scripting/scripts/debounce
If anyone would like to know what ended up working for me here it is:
humanoid.Died:Connect(function()
if not humanoid:GetAttribute("Killed") then
humanoid:SetAttribute("Killed", true)
module.onDeath(player)
task.wait(10)
humanoid:SetAttribute("Killed", true)
end
end)

ROBLOX - How would I move the player 8 studs, for 20 miliseconds smoothly (so tweening), in the direction the player is facing?

How would I move the player 8 studs, for 20 miliseconds smoothly (so tweening), in the direction the player is facing?
script.Parent.Activated:Connect(function()
--[This is because this is a localscript in a tool]
end)
local isGliding = false;
script.Parent.Activated:Connect(function()
isGliding = not isGliding;
local char = script.Parent.Parent; --The tool gets inside of the char when activated
local humanoidRootPart = char:WaitForChild("HumanoidRootPart");
coroutine.wrap(function()
while true do
humanoidRootPart.Positon += humanoidRootPart.LookVector * 8;
wait(0.3)
if isGliding == false then
break;
end
end
end)()
end)
Something like that probably. This should be in a ServerScript under the tool.

Why is my flight script not functioning correctly?

I'm making a flight script to use for ProtoSmasher and it's not working as intended. I want it to have a toggle button (G) and then be able to fly with a button (W). Instead of that, to get it to work I have to hold W and then press G; but if I try to let go of w to stop in the air I have to press G again.
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character
local hum = char.Humanoid
local Torso = char.HumanoidRootPart
local Mouse = plr:GetMouse()
local toggle = false
local wToggle = false
Mouse.KeyDown:Connect(function(key)
if key == "w" then
wToggle = true
end
if key == "g" then
if toggle == false then
toggle = true
local BV = Instance.new("BodyVelocity",Torso)
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
while wToggle == true do
BV.Velocity = Mouse.Hit.lookVector*200
wait()
end
end
if toggle == true then
toggle = false
Torso:FindFirstChildOfClass("BodyVelocity"):remove()
end
end
end)
Mouse.KeyUp:Connect(function(key)
if key == "w" then
wToggle = false
end
end)
It looks like you are trying to connect to keyboard key presses, but you are doing so in the event listener for mouse button presses.
Try something like this :
local isFlying = false
local function onKeyPress(actionName, userInputState, inputObject)
local isKeyDown = userInputState == Enum.UserInputState.Begin
local isKeyUp = userInputState == Enum.UserInputState.End
if actionName == "toggleFlight" then
if isKeyUp then
isFlying = not isFlying
end
elseif actionName == "fly" then
if isKeyDown then
-- start flying forward
elseif isKeyUp then
-- stop flying
end
end
end
-- listen for keyboard input
game.ContextActionService:BindAction("toggleFlight", onKeyPress, false, Enum.KeyCode.G)
game.ContextActionService:BindAction("fly", onKeyPress, false, Enum.KeyCode.W)

Corona - Lua - Dragging object without physics

I am relatively new to coding in lua so bear with me.
I'm ultimately trying to setup a drag & drop function for multiple 'tiles' on one scene. But to begin with (and to make sure I understand all of this correctly) I am just trying to get one tile moving around the screen.
So I browsed the interwebs for a while and found the solution below (among other similar solutions) and implemented this myself with my own object names etc.
It works great....but...unfortunatelt when i drag the object into the top right quadrant of the screen (on the simulator and a phone) the object gets stuck. It stops dragging as it hits any part of the top right quadrant of the screen, and I cannot re-select it to drag it back to the rest of the screen.
Any ideas as to why this is happening?? (my code is below)
local _H = display.contentHeight
local _W = display.contentWidth
local notesGroup = display.newGroup()
local tile1 = display.newImage ("graphics/image.PNG")
tile1.x = _W/2
tile1.y = _H/2
function tile1:touch( event )
if event.phase == "began" then
self.markX = self.x -- store x location of object
self.markY = self.y -- store y location of object
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y
end
return true
end
notesGroup:insert(tile1)
tile1:addEventListener("touch", tile1)
You need to set focus on touched object to prevent from loosing it while you drop it outside screen. Your touch event should look like this:
function tile1:touch( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
self.markX = self.x -- store x location of object
self.markY = self.y -- store y location of object
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus(nil)
end
return true
end
Try
-- create a circle and put it in the center of the screen
local circle = display.newCircle( display.contentWidth*0.5,display.contentHeight*0.5, 75)
circle:setFillColor( 255 )
-- touch listener function
function circle:touch( event )
if event.phase == "began" then
-- first we set the focus on the object
display.getCurrentStage():setFocus( self, event.id )
self.isFocus = true
-- then we store the original x and y position
self.markX = self.x
self.markY = self.y
elseif self.isFocus then
if event.phase == "moved" then
-- then drag our object
self.x = event.x - event.xStart + self.markX
self.y = event.y - event.yStart + self.markY
elseif event.phase == "ended" or event.phase == "cancelled" then
-- we end the movement by removing the focus from the object
display.getCurrentStage():setFocus( self, nil )
self.isFocus = false
end
end
-- return true so Corona knows that the touch event was handled propertly
return true
end
-- finally, add an event listener to our circle to allow it to be dragged
circle:addEventListener( "touch", circle )
A problem with your way of handling movement of the touch is that if the touch moves off the DisplayObject (in this case, tile1) between frames, the object's listener method won't be called before rendering the next frame and its position won't be updated. This may explain the weird "stuck in upper right quadrant" behaviour you're getting.
lduriat and Lukis show that setting the focus for this touch event (note the use of event.id by lduriat to handle the multitouch case) on the object avoids this problem. The use of setFocus() in handling touch events is explained in this section of the Corona Events/Listeners dev guide.

How to animate (moving by default) and inanimate (make stationary on tap) colliding objects

Hey tired this new code to animate objects (here bubbles) and make them stationary using sprites. The code is as below,
local ui = require("ui")
local gameUI = require("gameUI")
local easingx = require("easingx")
require "sprite"
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setScale( 60 )
local backgroundPortrait = display.newImage( "sky.png", 0, 0 )
local backgroundLandscape = display.newImage( "sky.png", 80, 80 )
backgroundLandscape.isVisible = false
local disp = backgroundLandscape
local function selectBubble( event )
local tapped = event.target --event.target is how Corona points to the tapped bubble
if ( tapped.bubbleSelected == false ) then
local vx,vy = tapped:getLinearVelocity()
tapped.xVel = vx --stores the current velocity into bubble's "xVel" variable
tapped.yVel = vy --likewise for yVel
tapped:setLinearVelocity( 0,0 ) --set bubble's velocity to 0!
tapped.currentFrame = (3)
tapped.bubbleSelected = true
elseif ( tapped.bubbleSelected == true ) then
tapped:setLinearVelocity( tapped.xVel, tapped.yVel ) --read previous velocity and set
tapped.bubbleSelected = false
end
end
--BUBBLE1
local bubble1 = sprite.newSprite( spriteSet1 )
bubble1.x = 100
bubble1.y = 100
physics.addBody(bubble1, {bounce=0.04, filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 2, 4 )
bubble1:addEventListener( "tap", selectBubble )
bubble1.bubbleSelected = false
bubble1:prepare("bubble")
bubble1:play()
--BUBBLE2
local bubble2 = sprite.newSprite( spriteSet1 )
bubble2.x = 210
bubble2.y = 20
physics.addBody(bubble2, {bounce=0.05, filter = bubbleCollisionFilter})
bubble2:setLinearVelocity( 2, 4 )
bubble2:prepare("bubble")
bubble2:play()
--BUBBLE3
local bubble3 = sprite.newSprite( spriteSet1 )
bubble3.x = 100
bubble3.y = 17
physics.addBody(bubble3, {bounce=0.02, filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 1, 2 )
bubble3:prepare("bubble")
bubble3:play()
--BUBBLE4
local bubble4 = sprite.newSprite( spriteSet1 )
bubble4.x = 310
bubble4.y = 20
physics.addBody(bubble4, {bounce=0.4, filter = bubbleCollisionFilter})
bubble4:setLinearVelocity( 2, 4 )
bubble4:prepare("bubble")
bubble4:play()
The problem is firstly the code doesn't seem to work. Secondly though the bubble changes color on tap (this color is same for most). Yet, each bubble has a unique letter on it. How to get this to work. Please help.
It's hard to know what exactly you're asking because your question is pretty vague (what does "animate" mean? And what do you mean by "doesn't seem to work"? explain these terms because they can mean multiple things) but I think you want to set the physics bodies to "static" in the tap event listener. Refer here for what I'm talking about:
http://developer.anscamobile.com/reference/index/bodybodytype
So inside the selectBubble() function you would type something like tapped.bodyType="static"