关于全球价值观的Love2d算法

时间:2017-05-18 10:37:25

标签: lua love2d

我正试图在我的游戏中创造一个时间扭曲能力,如果你保持't',那么敌人的速度就会减慢。我只想让这个技能每场比赛使用一次。我已经实现了在158-169行上执行此操作的方法,但是我收到错误“main.lua:88尝试对全局'敌人速度'(布尔值)执行艺术”。我该如何解决这个错误?我的代码如下:

debug = true


-- Timers
-- Declared these values here so I don't have to edit them multiple places
canShoot = true
canShootTimerMax = 0.2 
canShootTimer = canShootTimerMax
createEnemyTimerMax = 0.4
createEnemyTimer = createEnemyTimerMax

-- Player Object
player = { x = 200, y = 590, speed = 250, img = nil }
isAlive = true
score  = 0

-- Image Storage
bulletImg = nil
enemyImg = nil

-- Entity Storage
bullets = {} -- Current bullets being drawn and updated
enemies = {} -- Current enemies on screen

-- Returns true if two boxes overlap, false if they don't
-- x1,y1 are the left-top coords of the first box, while w1,h1 are its width and height
-- x2,y2,w2 & h2 are the same, but for the second box
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end

-- Loading
function love.load(arg)
    player.img = love.graphics.newImage('assets/sheriff.png')
    enemyImg = love.graphics.newImage('assets/enemy.png')
    bulletImg = love.graphics.newImage('assets/bullet.png')
  start=false
  controls=false
  mainmenu=love.graphics.newImage('assets/mainmenu.png')
  starttext=love.graphics.newImage('assets/starttext.png')
  helpmenu=love.graphics.newImage('assets/helpmenu.png')
  controlsmenu=love.graphics.newImage('assets/controlsmenu.png')
  enemyspeed=200
  timeWarpAbility=1

end


-- Updating
function love.update(dt)
    -- I always start with an easy way to exit the game
    if love.keyboard.isDown('escape') then
        love.event.push('quit')
    end

    -- Time out how far apart our shots can be.
    canShootTimer = canShootTimer - (1 * dt)
    if canShootTimer < 0 then
        canShoot = true
    end

    -- Time out enemy creation
    createEnemyTimer = createEnemyTimer - (1 * dt)
    if createEnemyTimer < 0 then
        createEnemyTimer = createEnemyTimerMax

        -- Create an enemy
        randomNumber = math.random(10, love.graphics.getWidth() - 10)
        newEnemy = { x = randomNumber, y = -10, img = enemyImg }
        table.insert(enemies, newEnemy)
    end


    -- update the positions of bullets
    for i, bullet in ipairs(bullets) do
        bullet.y = bullet.y - (250 * dt)

        if bullet.y < 0 then -- remove bullets when they pass off the screen
            table.remove(bullets, i)
        end
    end

    -- update the positions of enemies
    for i, enemy in ipairs(enemies) do
        enemy.y = enemy.y + (enemyspeed * dt)

        if enemy.y > 850 then -- remove enemies when they pass off the screen
            table.remove(enemies, i)
        end
    end

    -- run our collision detection
    -- Since there will be fewer enemies on screen than bullets we'll loop them first
    -- Also, we need to see if the enemies hit our player
    for i, enemy in ipairs(enemies) do
        for j, bullet in ipairs(bullets) do
            if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), bullet.x, bullet.y, bullet.img:getWidth(), bullet.img:getHeight()) then
                table.remove(bullets, j)
                table.remove(enemies, i)
                score = score + 1
            end
        end

        if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.img:getWidth(), player.img:getHeight()) 
        and isAlive then
            table.remove(enemies, i)
            isAlive = false
        end
    end


    if love.keyboard.isDown('left','a') then
        if player.x > 0 then -- binds us to the map
            player.x = player.x - (player.speed*dt)
        end
    elseif love.keyboard.isDown('right','d') then
        if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
            player.x = player.x + (player.speed*dt)
        end
    end

    if love.keyboard.isDown("space") and canShoot then
        -- Create some bullets
        newBullet = { x = player.x + (player.img:getWidth()/2), y = player.y, img = bulletImg }
        table.insert(bullets, newBullet)
        canShoot = false
        canShootTimer = canShootTimerMax
    end

    if not isAlive and love.keyboard.isDown('r') then
        -- remove all our bullets and enemies from screen
        bullets = {}
        enemies = {}

        -- reset timers
        canShootTimer = canShootTimerMax
        createEnemyTimer = createEnemyTimerMax

        -- move player back to default position
        player.x = 50
        player.y = 710

        -- reset our game state
        score = 0
        isAlive = true
    end
  if love.keyboard.isDown("p") then
    start=true
end
if love.keyboard.isDown('c') then
  controls=true
end
end

function love.keypressed(key)
  if key=='t' and timeWarpAbility==1 then
    enemyspeed=100
  end
end
--Creating Time Warp Ability
function love.keyreleased(key)
  if key=='t' then
    enemyspeed=200 and
    timeWarpAbility==0
  end
end

-- Drawing
function love.draw(dt)
    for i, bullet in ipairs(bullets) do
        love.graphics.draw(bullet.img, bullet.x, bullet.y)
    end

    for i, enemy in ipairs(enemies) do
        love.graphics.draw(enemy.img, enemy.x, enemy.y)
    end

    love.graphics.setColor(255, 255, 255)
    love.graphics.print("SCORE: " .. tostring(score), 400, 10)

    if isAlive then
        love.graphics.draw(player.img, player.x, player.y)
    else
        love.graphics.print("Press 'R' to restart", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-10)
    end

    if debug then
        --fps = tostring(love.timer.getFPS())
        --love.graphics.print("Current FPS: "..fps, 9, 10)
    end
  if start==false then
    love.graphics.draw(mainmenu)
end
if love.keyboard.isDown('h') then
  love.graphics.draw(helpmenu)
end
if controls==true then
  love.graphics.draw(controlsmenu)
  end
end

2 个答案:

答案 0 :(得分:3)

错误应该在这里:

--Creating Time Warp Ability
function love.keyreleased(key)
  if key=='t' then
    enemyspeed=200 and -- <-- remove this `and`
    timeWarpAbility==0 -- <-- also here
  end
end

我想你的意思是重新分配两个变量enemyspeedtimeWarpAbility,但正如所写,你的代码只有一个重新分配,它不是什么你期待。实际上,我突出显示的行被解释为单个语句:

enemyspeed=200 and (timeWarpAbility==0)

它给你一个布尔值(true或false),因此当你在第88行尝试enemyspeed*dt时出错,其中dt是一个数字。

尝试:

function love.keyreleased(key)
  if key=='t' then
    enemyspeed=200
    timeWarpAbility=0
  end
end

答案 1 :(得分:0)

在第166-167行,您有:

enemyspeed=200 and
timeWarpAbility==0

要了解Lua解释这个问题的方法,请允许我添加一些括号。

enemyspeed = (200 and (timeWarpAbility == 0))

代码timeWarpAbility == 0正在测试timeWarpAbility是否等于0,并且由于您已将其设置为1,因此该表达式的计算结果为false。因此,在我们扩展内括号之后,我们有了这个:

enemyspeed = (200 and false)

在Lua中,种类x and y的表达意味着&#34;如果x是真实的,那么就给我y,否则给我x&#34;。 200是真值,因此200 and false评估为false。这意味着我们的原始表达式的评估结果为:

enemyspeed = false

错误发生在love.update的第88行,如下所示:

enemy.y = enemy.y + (enemyspeed * dt)

此行在第一次发布&#34; t&#34;之前一直正常。键。然后当love.update运行时,当你试图将dt乘以false时,Lua会抱怨。

要修复它,你需要失去&#34;和&#34;在第166行并使用单个等号。

enemyspeed = 200
timeWarpAbility = 0