Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Hero attacking once
Started by ConveyedRex7592 Feb 14 2020 05:05 AM

4 replies to this topic

Best Answer SGS , 14 February 2020 - 05:34 PM

to elaborate on what sporkin said

 

change

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

to

 

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 39 posts
  • Corona SDK

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:

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;

 

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



[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 824 posts
  • Corona SDK

The problem is

timer.performWithDelay( 500, mehaAttackAnimationFinish )

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

 

move the timer inside the loop



[TOPIC: post.html]
#3

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,219 posts
  • Corona SDK

  Best Answer

to elaborate on what sporkin said

 

change

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

to

 

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

Edited by SGS, 15 February 2020 - 03:13 AM.

  • sporkfin likes this

[TOPIC: post.html]
#4

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 39 posts
  • Corona SDK

Thanks for help



[TOPIC: post.html]
#5

pixec

[GLOBAL: userInfoPane.html]
pixec
  • Contributor

  • 222 posts
  • Corona SDK

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


[topic_controls]
[/topic_controls]