Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Bug
Started by ConveyedRex7592 Feb 16 2020 01:27 AM

6 replies to this topic
physics bug

Best Answer XeduR @Spyric , 16 February 2020 - 01:50 AM

You really need to use more descriptive titles for your posts. :P

This isn't a bug per say. There are a few issues with your code.

 

Firstly, function scene:show( event ) is always called twice. It will first be called when the scene is about to be shown, with phase "will", and after it has shown, with phase "did". So, whenever you are using scene:show(), you most likely need to want to use conditional statements so that your code will only run during either stage, i.e.

function scene:show( event )
  print( "running" )
  if event.phase == "will" then
    print( "will" )
  elseif event.phase == "did" then
    print( "did" )
  end
end

Now, if you have more than 2 zombies out there, then your issue might be that you are loading the scene multiple times. You can use my code from above to see if you get "running" printed just twice to see if that is the case.

 

So, what you should do is instead use function scene:create( event ) because create is called only once. This is where you can create your entire scene. You could have a look at https://docs.coronalabs.com/guide/system/composer/index.html

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

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

Somehow zombie multiplies(there are not 1 zombie, but about 5. And these

other zombies are under the object map(you can see only 1).).

My code:

 

local composer = require "composer"
local joystickPlugin = require "plugin.joystick"
local physics = require "physics"
local scene = composer.newScene();
 
function scene:show(event)
  physics.start()
  physics.setGravity(0,0)
  local sceneGroup = self.view;
  system.activate("multitouch")

  local function zombieCollision(self, event)
    if event.phase == "began" then
      print("zombie")
    end
  end
  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
  physics.addBody(hero,"static")
  hero.direction = "up"
  hero.hp = 100

  local zombie = display.newImage("Images/zombie.jpg")
  zombie.isVisible = false
  zombie.x = display.contentCenterX
  physics.addBody(zombie,"dynamic")
  zombie.collision = zombieCollision
  zombie:addEventListener("collision", zombie)
  zombie.y = -100
  zombie.hp = 10

  local AttackButton = display.newImage("Images/mehaAttackButton.png")
  AttackButton.isVisible = false
  AttackButton.x = 480
  AttackButton.y = 270

  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,
  }

  function loadingEnd()
    spaceBackground.isVisible = false
    loading.isVisible = false
    map.isVisible = true
    hero.isVisible = true
    zombie.isVisible = true
    AttackButton.isVisible = true
 
    function enemy(event)
      if zombie.x < display.contentCenterX then
        zombie.x = zombie.x + 1
        zombie.rotation = 270
      elseif zombie.x > display.contentCenterX then
        zombie.x = zombie.x - 1
        zombie.rotation = 90
      end
      if zombie.y < display.contentCenterY then
        zombie.y = zombie.y + 1
        zombie.rotation = 0
      elseif zombie.y > display.contentCenterY then
        zombie.y = zombie.y - 1
        zombie.rotation = 180
      end
    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
          zombie.x = zombie.x + 2
          hero.rotation = 270
          hero.direction = "left"
        elseif vector.x > 0.5 then
          map.x = map.x - 2
          zombie.x = zombie.x -2
          hero.rotation = 90
          hero.direction = "right"
        end

        if vector.y < -0.5 then
          map.y = map.y + 2
          zombie.y = zombie.y + 2
          hero.rotation = 0
          hero.direction = "up"
        elseif vector.y > 0.5 then
          map.y = map.y - 2
          zombie.y = zombie.y-2
          hero.rotation = 180
          hero.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
          physics.addBody(mehaAttackAnimation,"static")

          if hero.direction == "up" then
            mehaAttackAnimation.rotation = 0
          elseif hero.direction == "right" then
            mehaAttackAnimation.rotation = 90
          elseif hero.direction == "down" then
            mehaAttackAnimation.rotation = 180
          elseif hero.direction == "left" then
            mehaAttackAnimation.rotation = 270
          end

          mehaAttackAnimation:play()
          AttackCoolDown = 1

          timer.performWithDelay( 500, function()
          hero.isVisible = true
            mehaAttackAnimation:removeSelf()
            AttackCoolDown = 0
          end)
        end
      end
    end

    Runtime:addEventListener("enterFrame", enemy)
    Runtime:addEventListener("enterFrame", enterFrame)
    AttackButton:addEventListener("touch", Attack)
  end

  timer.performWithDelay( 2000, loadingEnd )

end

scene:addEventListener("show", scene);
return scene;

 

PS I am creating top view game



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,240 posts
  • Corona SDK

  Best Answer

You really need to use more descriptive titles for your posts. :P

This isn't a bug per say. There are a few issues with your code.

 

Firstly, function scene:show( event ) is always called twice. It will first be called when the scene is about to be shown, with phase "will", and after it has shown, with phase "did". So, whenever you are using scene:show(), you most likely need to want to use conditional statements so that your code will only run during either stage, i.e.

function scene:show( event )
  print( "running" )
  if event.phase == "will" then
    print( "will" )
  elseif event.phase == "did" then
    print( "did" )
  end
end

Now, if you have more than 2 zombies out there, then your issue might be that you are loading the scene multiple times. You can use my code from above to see if you get "running" printed just twice to see if that is the case.

 

So, what you should do is instead use function scene:create( event ) because create is called only once. This is where you can create your entire scene. You could have a look at https://docs.coronalabs.com/guide/system/composer/index.html


  • sieler2 likes this

[TOPIC: post.html]
#3

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

Looks like it worked. But there is one more issue.

Zombie can't turn left and right,but it can go left and right.Instead of turning left or right it turns up and down.



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,240 posts
  • Corona SDK

Well, your enemy function seems to constantly drag the zombie to left/right and up/down depending on what location they are on the map.

 

You should consider taking a look at some tutorials and/or game templates to get your bearings, there are some good links for you here: https://forums.coronalabs.com/topic/77147-can-i-have-a-collision-answer/



[TOPIC: post.html]
#5

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

I use function enemy to move zombie to player(hero)

Because player(hero) always stays in center.



[TOPIC: post.html]
#6

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,240 posts
  • Corona SDK

Yes, I can see that you are using it to move the zombie, but the way how you use it is a bit odd to me.

   function enemy(event)
      if zombie.x < display.contentCenterX then
        zombie.x = zombie.x + 1
        zombie.rotation = 270
      elseif zombie.x > display.contentCenterX then
        zombie.x = zombie.x - 1
        zombie.rotation = 90
      end
      if zombie.y < display.contentCenterY then
        zombie.y = zombie.y + 1
        zombie.rotation = 0
      elseif zombie.y > display.contentCenterY then
        zombie.y = zombie.y - 1
        zombie.rotation = 180
      end
    end

*Edit: nevermind, I just figured you had the values set to the zombie moving towards the centre, not away from it.



[TOPIC: post.html]
#7

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Enthusiast

  • 38 posts
  • Corona SDK

Just zombie don't want to turn right and left sometimes.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: physics, bug