Hi, I am trying to make a game similar to Terraria but instead of a click (tap) and attack it will be a joystick. To clean the code and make it easier to read here I took it out and just made it rotate every frame, basically simulating a joystick movement. So the “sword” should detect when it hits the ground and then destroy, it but it isn’t working. I have tried moving it and changing it to a “dynamic” object similar to the ground but it still doesn’t work. It is definitely related to it always being set to the players position because if I take out the line of code that does that and change the weapon object to “dynamic” it falls and collides correctly. Here is the code:
[lua]
local M = {}
function M.new()
local group = display.newGroup()
physics.setDrawMode ( “hybrid” )
physics.start()
physics.setGravity(0, 20)
local worldGroup = display.newGroup()
group:insert(worldGroup)
local rect = display.newRect(0, ch-10, cw, 10)
rect.tag = “ground”
physics.addBody(rect, “static”, {bounce = 0})
local player = display.newRect(cw/2, 0, 50, 100)
player.tag = “player”
physics.addBody(player, “dynamic”, {bounce = 0})
local playerWeapon = display.newRect(0, 0, 100, 10)
playerWeapon:setReferencePoint( display.CenterLeftReferencePoint )
physics.addBody(playerWeapon, “static”, {bounce = 0}) --change this to dynamic to see it work correctly
playerWeapon.isSensor = true
local function playerWeaponCollision (event)
if event.other.tag ~= “player” then
display.remove(event.other)
end
end
playerWeapon:addEventListener(“collision”, playerWeaponCollision)
local function enterFrame( )
playerWeapon.x, playerWeapon.y = player.x, player.y – remove this line and line above to see it work correctly
playerWeapon:rotate(0.1)
end
Runtime:addEventListener(“enterFrame”, enterFrame)
return group
end
return M
[/lua]
Any help will be appreciated!