Stop Arrow Spam - lua

I'm doing my first game with Lua in college and I'm having a hard time part.
My char shoots arrows non-stop and I want it to have a delay to shot each arrow.
I tried to create functions to simulate a delay but it did not work
local function atkRight()
system.setTapDelay(10)
display.remove(char)
char = display.newImageRect ( "Sprites/archerRight.png", 50, 60)
char.x = display.contentCenterX
char.y = display.contentCenterY+50
physics.addBody (char, "static", { isSensor=false })
char.myName = "char"
local arrowRight = display.newImageRect ( "Sprites/arrowRight.png", 50, 5)
arrowRight.x = display.contentCenterX+40
arrowRight.y = display.contentCenterY+40
physics.addBody (arrowRight, "dynamic", { bounce = 0 })
arrowRight:setLinearVelocity(500, 0)
arrowRight.gravityScale = 0
arrowRight.myName = "arrowRight"
end
atkiconRight:addEventListener( "tap", atkRight )
I wish this attack function could only be executed every 0.5 seconds

There are various ways to achieve this. The simplest way is probably to have your event callback check the time.
https://docs.coronalabs.com/api/library/system/getTimer.html
Store the time a shot was fired in a global variable.
When a shot is fired and there is a timestamp of a preceeding shot, check and only shoot if it is at least 0.5 seconds later.
Another way would be to remove the event listener and start a timer event that will re-add the event listener after 500ms. Or you have a global flag that prevents shooting and have a timer reset this flag every 500ms.
Which way to go is up to you.

Related

How can I handle collisions of multiple blocks as if they were a single entity?

I'm creating a 2D platform game using Corona SDK and I'm stuck with collisions.
Basically there is a character that runs over this ground made of blocks. This is because sometimes there can be holes in the ground.
The game is an endless one, so as the character moves forward new blocks (and holes) are dynamically added - and also removed if they goes off screen.
It works nicely but this approach works against the collision system, let me explain how.
Now that I have the ground in place I want the character to jump but only if it is touching the ground, to avoid jumping while in air.
Whenever a collision is detected between character and ground an event is fired - two times. The first time when the character is entering a ground block and the second time when the character leaves it. So when the character lands on the ground a isGround Boolean is set to true. And when - after a jump - it leaves it the flag is set to false. The problem is that every time it exits a block to enter another - walking along the ground without jumping - the flag get updated. This makes the jump based on the isGround flag less reliable. Sometimes it happens that you can't jump because isGround == false though the character is on the ground.
Ground block creation snippet
-- init() method set the sprite of the ground block and physic to that sprite
function GroundBlock:init()
self.sprite = display.newImageRect(self.path, self.width, self.height)
self.sprite.x = self.x
self.sprite.y = self.y
physics.addBody(self.sprite, 'static', {
density = 0,
friction = 0,
bounce = 0,
box = {
halfWidth = self.width / 2,
halfHeight = self.height / 2,
y = 16,
x = 0
}
})
local collisionObj = {
name = 'ground'
}
self._collision = collisionObj
self.sprite._collision = collisionObj
self.isShow = true
end
Ground placing GroundBlocks snippet
-- init() method initialize the ground with a fixed number of blocks
function Ground:init()
self.offsetX = 0
while self.offsetX < self.camera.borderRight * 2 do
self._createBlock(1)
end
self.lastCameraPos = self.camera.borderRight
end
-- update() is called once per frame
function Ground:update()
if (self.camera.borderRight - self.lastCameraPos > self._blockWidth) then
local rand = math.ceil(math.random() * 10) % 2
if self._skippedBlock >= 2 or rand == 0 then
self._createBlock(1)
self._skippedBlock = 0
else
self._createBlock(0)
self._skippedBlock = self._skippedBlock + 1
end
self.lastCameraPos = self.camera.borderRight
end
for i, block in ipairs(self.blocks) do
if block.sprite.x < self.camera.borderLeft - block.width then
table.remove(self.blocks, i)
self.camera:remove(block.sprite)
block:delete()
end
end
end
Collision detection snippet
function Character:collision(event)
if ( event.phase == "began" ) then
if event.other._collision.name == "ground" then
self.isGround = true
end
elseif ( event.phase == "ended" ) then
if event.other._collision.name == "ground" then
self.isGround = false
print('nope')
end
end
end
A solution would be to make a ground as a single imgRect but how to make holes in it?
You could simplify your code and prevent this issue from ever occurring by tracking if the character can jump instead of tracking if the character is on the ground.
For instance,
function jump( event )
if event.phase == "began" then
if canJump then
canJump = false
-- your code that makes the player jump
end
end
end
You probably use touches to determine whether the player character jumps, right? This way, you'll trigger the jump when the touch starts as long as the character has not already jumped.
You could then reset this value in your collision function by editing it slightly:
function Character:collision(event)
if event.phase == "began" then
if event.other._collision.name == "ground" then
canJump = true
end
end
end
This way, the character's ability to jump is determined by whether or not the player has pressed jump already and if the character has hit the ground since the last jump.
This kind of approach also gives you the ability to pivot towards implementing mechanics like double jump. If instead of using a boolean canJump variable you chose to use a number variable, e.g. jumpsLeft, you could reduce the number of jumps left every time the character jumps and only let the character jump if jumpsLeft is larger than 0. Then you'd simply reset the value back to 1 (or whatever you'd want upon hitting the ground).

How do I implement recoil on a gun in LUA, 2D?

I'm trying to create a gun simulator type mobile application, and am currently trying to code a fire function. it's going pretty well so far, but me being a novice I'm having troubles figuring out what I can do to resolve my issue:
function pistolRecoil()
transition.to(pistol, {time = 30, rotation = -20 })
end
function revPistolRecoil()
transition.to(pistol, {time = 50, rotation = 20 })
end
--Fire the pistol--
function tapFirePistol (event)
--display flash
muzzleFlashP:toFront()
timer.performWithDelay(20, muzzleFlashPFunc())
pistolAmmo = pistolAmmo - 1
----timings
--sound(.wav),
audio.play(pFireSound)
--rotate,rotate
pistolRecoil()
print("1st", pistol.rotation)
pistolRecoil()
print("2nd", pistol.rotation)
revPistolRecoil()
----timings
if pistolAmmo <= 0 then fireButtonPistol:removeEventListener("tap",
tapFirePistol)
pistolAmmoCount.text = pistolAmmo
end
if pistolAmmo <= 0 then pistolAmmo = 0
pistolAmmoCount.text = pistolAmmo
end
pistolAmmoCount.text = pistolAmmo
end
fireButtonPistol:addEventListener("tap", tapFirePistol)
The problem I'm having is that the gun will recoil, but stay rotated, which is bad as I would like it to "fall" back down after a short period of time, and will not recoil any further even in the fire button is tapped while the pistol is still rotated (which I'm not bothered by)
Any help is greatly appreciated.
So, the recoil aspect is now working as I am now using timer.performWithDelay. I found out that the reason the timer.performWithDelay was not behaving the way I expected is because i was using parenthesis inside the performWithDelay parenthesis, which are not needed, and I also specified that it should be done once.
timer.performWithDelay(20, revPistolRecoil, 1)
I still call pistolRecoil as a function.
As a side note, the muzzleFlashP also now works when I apply the same changes.
Thanks for the help!

Corona SDK: Having Single and Double Tap Events on One Object

I'm trying to have a different amount of force applied to an object when the screen is tapped a different amount of times ( only once and twice ).
I'm not sure what I'm doing wrong. Here is the code:
local function moveUp(event)
if event.numTaps > 1 then
jumper:applyForce( 0, 250, jumper.x, jumper.y )
elseif event.numTaps < 1 then
jumper:applyForce( 0, 0, jumper.x, jumper.y )
else
jumper:applyForce( 0, 200, jumper.x, jumper.y )
end
end
-- start game
createPlayScreen( )
system.setTapDelay( 2 )
Runtime:addEventListener("tap", moveUp)
I've tried moving the Runtime:addEventListener into the function. I've also tried have the event.numTaps == 2 and event.numTaps == 1, but to no avail.
The issue is that the TapDelay refuses to wait for the second tap.
Any and all help is greatly appreciated
P.S. I have the seconds set to two for testing purposes, but once I find that this works, I will be lowering the time to like 0.3 or something
There are several issues with the strategy you are using. For one, it is not scalable (to more than two taps). But ok, maybe you are 100% sure you will never ever need more than 2 taps. The next problem is that event.numTaps can only be 1 or 2, yet your listener tests for < 1! This can never happen. The next problem is that when you tap twice, at least in the simulator (did not test on device) you get two events: one for the first tap with numTaps = 1, and another for 2nd tap with numTaps = 2. In other words, the Corona engine does not wait before emitting a one-tap event to know if a second tap event occurs within some time range. So you get two tap events for a two-tap event, there is no way of knowing in the handler whether you should "wait" to see if another tap might occur within an allowable delay to constitute a "two-tap" event instead.
What you will have to do is create your own N-tap event generator. Whenever a tap occurs, check if your timer has been started. If so, increase the tap count and reset the timer. If not, start the timer that expires some short delay later. If no other tap occurs in that delay, the count you have saved is your tap number. Reset the counter if your timer expires. I have created some functions that do this and I have put them all in a table "object":
local tapEvents = {
measureInterTapTime = false, -- set to true to measure how fast you can tap!
onTapHandler = nil, -- set this to your handler
-- implementation details
tapTimer = nil,
tapCounter = 0,
tapEventTime = 0,
doneTap = function(self, event)
self.tapTimer = nil
if self.onTapHandler then
self.onTapHandler(self.tapCounter)
end
self.tapCounter = 0
self.tapEventTime = 0
end,
-- end implementation details
tap = function(self, event)
self.tapCounter = self.tapCounter + 1
if self.tapTimer ~= nil then
timer.cancel(self.tapTimer)
self.tapTimer = nil
end
local delayMS = 250
self.tapTimer = timer.performWithDelay(delayMS, function(e) self:doneTap(e) end, 1)
-- check how much time between taps, for interest:
if self.measureInterTapTime then
if self.tapEventTime ~= 0 then
local interTapTime = system.getTimer() - self.tapEventTime
print("Time (ms) between taps:", interTapTime)
end
self.tapEventTime = system.getTimer()
end
end,
}
tapEvents.onTapHandler = function(tapCounter)
print(tapCounter .. "-tap event")
end
-- because tapEvents contains a 'tap' function, will get called automatically with self:
Runtime:addEventListener('tap', tapEvents)
This captures N-tap events without limit!! I have included a flag you can set to true if you want to print out the ms delay between single taps so you can determine what the optimum delay should be (you don't want delay to be too short or you might inadvertently break an N tap into two smaller events; you don't want it to be too long either, or user will have to noticeably wait to indicate "end of my multitap").
Tap events must be added to a display object, not to the Runtime.
If you have a display object jumper for example, use:
jumper:addEventListener("tap", moveUp)
More documentation here: http://docs.coronalabs.com/api/event/tap/index.html

In corona sdk how can I reference / specify all indexes of a spawned object on the screen?

Hi I've been wondering this for a while and it's causing me lots of problems not knowing more about how to reference / specify all indexes of a spawned enemy on the screen at one time.
Say when my character dies I want all the enemies on the screen at that time to move away from my dead character as the screen fades out. Simply calling 'enemy1' only makes one (the last spawned I think) do as it's told.
Here is my enemy spawn script:
local spawnTable2 = {}
local function spawnEnemy()
enemy1 = display.newSprite( group, sheetE, sequenceData2 )
enemy1.x=math.random(100,1300)
enemy1.y=math.random(360,760)
enemy1.gravityScale = 0
enemy1:play()
enemy1.type="coin"
enemy1.objTable = spawnTable2
enemy1.index = #enemy1.objTable + 1
enemy1.myName = "enemy" .. enemy1.index
physics.addBody( enemy1, "kinematic",{ density = 0, friction = 0, bounce = 1 })
enemy1.isFixedRotation = true
enemy1.type = "enemy1"
enemy1.timer = nil
enemy1.enterFrame = moveEnemy
Runtime:addEventListener("enterFrame",enemy1)
enemy1.objTable[enemy1.index] = enemy1
hudGroup:toFront()
return enemy1
end
To reference all objects, you would need to have two things.
A Counter
Loop
First make a counter:
counter = 0
Every time the funciton is called have a counter:
counter = counter + 1
For tables, you would do something like this:
spawnTable2[counter].x=math.random(100,1300)
Then when you want to remove the object, you would just do this:
display.remove(spawnTable2[counter])
Just keep in mind, everything you do to manipulate that object will have to be inside that function. Good luck and hope this helps.

play and stop spritesheet Using lua for corona

I have created a sprite sheet which plays continuously,
local sheet3 = sprite.newSpriteSheet( "sample.png",400,317)
local spriteSet3 = sprite.newSpriteSet(sheet3, 1, 8)
sprite.add( spriteSet3, "puma", 1, 8, 1000, 0 ) -- play 8 frames every 1000 ms
local instance3 = sprite.newSprite( spriteSet3 )
instance3.x = 2* display.contentWidth / 4 + 30
instance3.y = baseline - 5
instance3.xScale = .5
instance3.yScale = .5
instance3:prepare("puma")
instance3:play()
As we know spritesheet shows image sequence in loop. I want to stop play of image sequence after it completes one loop.
Anybody know how can i do this? or at least provide me any link to help me out to solve this problem?
When you define the animation in the first place you set whether or not to loop:
http://developer.anscamobile.com/reference/index/spriteadd
Alternatively if you need to adjust the animation programmatically (ie. loop until the player does something) then you can set an event listener and call pause() when the loop event happens:
http://developer.anscamobile.com/reference/index/spriteinstanceaddeventlistener

Resources