简体   繁体   English

停止箭头垃圾邮件

[英]Stop Arrow Spam

I'm doing my first game with Lua in college and I'm having a hard time part. 我正在大学里和Lua一起做我的第一场比赛,现在很难过。 My char shoots arrows non-stop and I want it to have a delay to shot each arrow. 我的char不停地射箭,我希望它能延迟射箭的时间。

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 我希望此攻击功能只能每0.5秒执行一次

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 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. 当开枪并且有前一枪的时间戳时,请检查并仅在至少0.5秒后拍摄。

Another way would be to remove the event listener and start a timer event that will re-add the event listener after 500ms. 另一种方法是删除事件侦听器并启动计时器事件,该事件将在500ms之后重新添加事件侦听器。 Or you have a global flag that prevents shooting and have a timer reset this flag every 500ms. 或者您有一个全局标志来阻止射击,并让计时器每500毫秒重置一次此标志。

Which way to go is up to you. 哪种方法取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM