Is there any way I can enable and disable collision objects/boxes?
I tried msg.post("#Sword Collision U,", disable")
but it didn’t work and just spat out this error:
This is my code for the player, I am trying to disable the collision boxes so I can enable them when I attack so that I can hit enemies.
go.property("speed", 80) -- sets the speed that the player will move
direction = "down" -- sets down as the default direction
local STATE_SWORD_ATTACK = hash("attack_sword")
local STATE_WALKING = hash("walking")
local STATE_IDLE = hash("idle")
msg.post("#Sword Collision U", "disable")
msg.post("#Sword Collision D", "disable")
msg.post("#Sword Collision L", "disable")
msg.post("#Sword Collision R", "disable")
local Health = require("main.Data Storage")
playerHealth = Health.my_value -- creates a health variable
function init(self)
msg.post(".", "acquire_input_focus") -- allows for inputs
self.vel = vmath.vector3() -- indicates velocity, starts at 0
end
function update(self, dt)
local pos = go.get_position() -- gets the current position and stores it
pos = pos + self.vel * dt -- adds the velocity to the position
go.set_position(pos) -- sets the position of the game object to the new position
end
function on_input(self, action_id, action)
if action_id == hash("up") or action_id == hash("down") or action_id == hash("left") or action_id == hash("right") then
self.state = STATE_WALKING
if action_id == hash("up") then -- checks the input
direction = "up"
self.vel.y = self.speed -- adds your speed to the your velocity
if action.pressed then -- checks if the action is being pressed
sprite.play_flipbook("#sprite", hash("NU_Walk")) -- Changes Animation to walk
elseif action.released then -- checks if the action has been let go
self.vel.y = 0 -- sets their y speed to 0
end
elseif action_id == hash("down") then -- checks the input
direction = "down"
self.vel.y = -self.speed -- adds your speed to the your velocity
if action.pressed then -- checks if the action is being pressed
sprite.play_flipbook("#sprite", hash("ND_Walk")) -- Changes Animation to walk
elseif action.released then -- checks if the action has been let go
self.vel.y = 0 -- sets their y speed to 0
end
elseif action_id == hash("left") then -- checks the input
direction = "left"
self.vel.x = -self.speed -- adds your speed to the your velocity
if action.pressed then -- checks if the action is being pressed
sprite.play_flipbook("#sprite", hash("NL_Walk")) -- Changes Animation to walk
elseif action.released then -- checks if the action has been let go
self.vel.x = 0 -- sets their x speed to 0
end
elseif action_id == hash("right") then -- checks the input
direction = "right"
self.vel.x = self.speed -- adds your speed to the your velocity
if action.pressed then -- checks if the action is being pressed
sprite.play_flipbook("#sprite", hash("NR_Walk")) -- Changes Animation to walk
elseif action.released then -- checks if the action has been let go
self.vel.x = 0 -- sets their x speed to 0
end
end
end
if self.vel.x == 0 and self.vel.y == 0 then
self.state = STATE_IDLE
if direction == "up" then -- checks what direction the player is facing
sprite.play_flipbook("#sprite", hash("NU_Idle")) -- Sets the animation to idle
elseif direction == "down" then -- checks what direction the player is facing
sprite.play_flipbook("#sprite", hash("ND_Idle")) -- Sets the animation to idle
elseif direction == "left" then -- checks what direction the player is facing
sprite.play_flipbook("#sprite", hash("NL_Idle")) -- Sets the animation to idle
elseif direction == "right" then -- checks what direction the player is facing
sprite.play_flipbook("#sprite", hash("NR_Idle")) -- Sets the animation to idle
end
end
if action_id == hash("sword") then
if direction == "up" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("NU_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if self.state == STATE_WALKING then
sprite.play_flipbook("#sprite", hash("NU_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("NU_Idle")) -- plays the idle animation
end
end)
elseif direction == "down" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("ND_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if self.state == STATE_WALKING then
sprite.play_flipbook("#sprite", hash("ND_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("ND_Idle")) -- plays the idle animation
end
end)
elseif direction == "left" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("NL_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if self.state == STATE_WALKING then
sprite.play_flipbook("#sprite", hash("NL_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("NL_Idle")) -- plays the idle animation
end
end)
elseif direction == "right" then -- checks what direction the player is facing
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
sprite.play_flipbook("#sprite", hash("NR_Attack"), function() -- plays the attack animation
msg.post(".", "acquire_input_focus") -- allows the player to move again
if self.state == STATE_WALKING then
sprite.play_flipbook("#sprite", hash("NR_Walk")) -- Changes Animation to walk
else
sprite.play_flipbook("#sprite", hash("NR_Idle")) -- plays the idle animation
end
end)
end
end
if playerHealth == 0 then
msg.post(".", "release_input_focus") -- stops the player from moving while the animation plays
self.vel.x = 0 -- sets their x speed to 0
self.vel.y = 0 -- sets their y speed to 0
sprite.play_flipbook("#sprite", hash("Death"),function() -- plays the death animation
os.exit() -- closes the game
end)
end
end