Hero attacking once

When I added loading screen hero attacks only once.(When I touch attack button animation runs and when it ends it doesn’t disappear)

My code:

[lua]

local composer = require “composer”
local joystickPlugin = require “plugin.joystick”
local scene = composer.newScene();

function scene:show(event)
  local sceneGroup = self.view;
  system.activate(“multitouch”)
  local spaceBackground = display.newImageRect(“Images/SpaceBackground.png”,
  display.actualContentWidth, display.actualContentHeight)
  spaceBackground.x = display.contentCenterX
  spaceBackground.y = display.contentCenterY
  local loading = display.newText(“Loading…”, display.contentCenterX - 230, 330,
  “Lobster-Regular”, 40)
  local map = display.newImage(“Images/survivalModeMap.png”)
  map.isVisible = false
  map.x = display.contentCenterX
  map.y = display.contentCenterY
  local hero = display.newImage(“Images/mehaHat.png”)
  hero.isVisible = false
  hero.x = display.contentCenterX
  hero.y = display.contentCenterY
  local enemy = display.newImage(“Images/zombie.jpg”)
  enemy.isVisible = false
  enemy.x = display.contentCenterX
  enemy.y = -100
  local AttackButton = display.newImage(“Images/mehaAttackButton.png”)
  AttackButton.isVisible = false
  AttackButton.x = 480
  AttackButton.y = 270
  local direction = “up”
  local AttackAnimationSheetData = {
    width = 400,
    height = 400,
    numFrames = 10,
    sheetContentWidth = 800,
    sheetContentHeight = 2000
  }
  local AttackAnimationSheet = graphics.newImageSheet(“Images/mehaAttackAnimation.png”, AttackAnimationSheetData)
  local sequenceData = {
    { name = “Attack”, start = 1, count = 10, time = 500, loopCount = 1}
  }
  local props = {
    x = 10,
    y = 270,
    backgroundRadius = 50,
    movedStickRadius = 25,
  }
  local function loadingEnd()
    spaceBackground.isVisible = false
    loading.isVisible = false
    map.isVisible = true
    hero.isVisible = true
    enemy.isVisible = true
    AttackButton.isVisible = true

    local function zombie(event)
    end

    local Joystick = joystickPlugin.newJoystick(props)
    Joystick.background:setFillColor(0.5)
    Joystick.movedStick:setFillColor(0.7)
    local function enterFrame(event)
      if Joystick.isActivated() then
        local vector = Joystick.getVector()
        if vector.x < -0.5 then
          map.x = map.x + 2
          enemy.x = enemy.x + 2
          hero.rotation = 270
          direction = “left”
        elseif vector.x > 0.5 then
          map.x = map.x - 2
          enemy.x = enemy.x -2
          hero.rotation = 90
          direction = “right”
        end
        if vector.y < -0.5 then
          map.y = map.y + 2
          enemy.y = enemy.y + 2
          hero.rotation = 0
          direction = “up”
        elseif vector.y > 0.5 then
          map.y = map.y - 2
          enemy.y = enemy.y-2
          hero.rotation = 180
          direction = “down”
        end
      end
    end
    local AttackCoolDown = 0
    local function Attack(event)
      if(event.phase == “began”) then
        if AttackCoolDown == 0 then
          hero.isVisible = false
          local mehaAttackAnimation = display.newSprite( AttackAnimationSheet, sequenceData )
          mehaAttackAnimation.x = display.contentCenterX
          mehaAttackAnimation.y = display.contentCenterY
          if direction == “up” then
            mehaAttackAnimation.rotation = 0
          elseif direction == “right” then
            mehaAttackAnimation.rotation = 90
          elseif direction == “down” then
            mehaAttackAnimation.rotation = 180
          elseif direction == “left” then
            mehaAttackAnimation.rotation = 270
          end
          mehaAttackAnimation:play()
          AttackCoolDown = 1
          local function mehaAttackAnimationFinish()
            hero.isVisible = true
            mehaAttackAnimation:removeSelf()
            AttackCoolDown = 0
          end
        end
        timer.performWithDelay( 500, mehaAttackAnimationFinish )
      end
    end
    Runtime:addEventListener(“enterFrame”, zombie)
    Runtime:addEventListener(“enterFrame”, enterFrame)
    AttackButton:addEventListener(“touch”, Attack)
  end
  timer.performWithDelay( 2000, loadingEnd )
end
scene:addEventListener(“show”, scene);
return scene;

[/lua]

PS When I paste code all spaces disappear so it can be harder to read

The problem is

[lua]

timer.performWithDelay( 500, mehaAttackAnimationFinish )

[/lua]

can’t access the mehaAttackAnimationFinish() function because it is a local function inside a loop

move the timer inside the loop

to elaborate on what sporkin said

change

 local function mehaAttackAnimationFinish() hero.isVisible = true mehaAttackAnimation:removeSelf() AttackCoolDown = 0 end

to

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;timer.performWithDelay( 500, function() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hero.isVisible = true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.remove(mehaAttackAnimation) mehaAttackAnimation = nil &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AttackCoolDown = 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end)

Thanks for help

Great helping guys! Also you could add those spaced even if they were originally removed :stuck_out_tongue: