Projectile only works if badguy not touching player (video)

Hi

Here’s a short video explaining: http://screencast.com/t/rsszmQok

Projectile code

–create projectile

      local sequenceData = {

          {name = “still”, sheet = spritesheet_misc, frames = {33}},

      }

      local projectile = display.newSprite(spritesheet_misc, sequenceData) 

      local projectile_speed = 100

      local shootforce = 1

      local shootforce_x = 0

      local shootforce_y = 0

      local projectile_offset_x = 0

      local projectile_offset_y = 0

       

      if(player_facingdirection == “north”) then

        shootforce_y = shootforce * -1

        projectile_offset_y = 5

        projectile_offset_x = 3

      elseif(player_facingdirection == “south”) then

        shootforce_y = shootforce

        projectile_offset_x = -4

        projectile.rotation = 180

      elseif(player_facingdirection == “east”) then

        shootforce_x = shootforce

        projectile.rotation = 90

        projectile_offset_y = 4

        projectile_offset_x = 7

      else

        shootforce_x = shootforce * -1

        projectile.rotation = 270

        projectile_offset_y = 4

        projectile_offset_x = -7

      end

                

    

      mte.addSprite(projectile, {

        kind = “sprite”,

        layer = player.layer,

        levelPosX = player.levelPosX + projectile_offset_x,

        levelPosY = player.levelPosY + projectile_offset_y,

        levelWidth = 32,

        levelHeight = 32

      }) 

      projectile:setSequence(“still”)

      projectile.type = “player_projectile”

      projectile.damage = 1 

      local projectile_shape = { 0, -1, 1, 1, -1, 1} 

      

      --probably need to put this as a function that is created once and not over and over for each projectile.

      function projectile:preCollision( event ) 

        if(event.other.type == “player_projectile”) then

        event.contact.isEnabled = false

      --elseif mte.getLevel(event.other.layer) ~= player.level then

       – event.contact.isEnabled = false

        end

      end 

      projectile:addEventListener( “preCollision” )

      

      

      function projectile:collision( event ) 

         --print("--------------------------------------------------------------------------------")

         --pr(event.other)

        if(event.other.type == “badguy”) then

        --event.contact.isEnabled = false

          event.other.hp = event.other.hp - projectile.damage

           print ("hp after: "…event.other.hp)

           if(event.other.hp <= 0) then

             timer.cancel(event.other.timer_movement)

             mte.removeSprite(event.other)

           end

           mte.removeSprite(event.target) 

        elseif(event.other.type ~= “player”) then

          mte.removeSprite(event.target)

        end

      

      end 

      projectile:addEventListener( “collision” )

      mte.physics.addBody(projectile, “dynamic”,  {friction = 0, shape = projectile_shape, bounce = 0.0, density = 100}) 

      projectile.isFixedRotation = true

      projectile:applyLinearImpulse( shootforce_x, shootforce_y, 0, 0 )

      --pr(projectile)

      --send it on its way

      --somewhere detect if hits a badguy type remove their hp

        

      

    end

I’ve only watched the video, but from what I understand you’re wondering why the projectile doesn’t hit the enemy? Isn’t that just because you’re already up against them, so once you shoot the collision detection kicks in immediately and destroys your projectile? Or are you asking something completely different…

Fixed it. 

I had some code that checked the tile in front of the player - and if there was a sprite in the next tile, it would not shoot (so the player could deal with a item).

Your help led me there. Thanks

I’ve only watched the video, but from what I understand you’re wondering why the projectile doesn’t hit the enemy? Isn’t that just because you’re already up against them, so once you shoot the collision detection kicks in immediately and destroys your projectile? Or are you asking something completely different…

Fixed it. 

I had some code that checked the tile in front of the player - and if there was a sprite in the next tile, it would not shoot (so the player could deal with a item).

Your help led me there. Thanks