1. I give the scorpion a variable 'self.state' which holds one of the 3 states' moveleft ',' moveright ',' idle 'and corresponding to those 3 states the scorpion will have velocity' self.vx 'are -25, 25 and 0. respectively. Then I declare a variable self.switchTime = 200. I reduce its value in the update function of' scorpion.lua ', if self.switchTime is greater than 0 I will keep reducing it, until it is less than or equal to 0 I will check the following: if the scorpion moves, let it sleep, and if the scorpion is sleeping, let it move. And I reset self.switchTime = 200 so this is checked continuously.
mycode: in scorpion.lua
Code: Select all
function scorpion:new()
...
self.vx = 0
self.state = 'moveleft'
self.switchTime = 200
...
end
function scorpion:update(dt)
if self.switchTime > 0 then
self.switchTime = self.switchTime - 1
else
self.switchTime = 200
print('a')
if self.canMove then self.canMove = false end
if self.canMove == false then self.canMove = true end
end
if self.canMove then
if self.x <= self.targetX1 then
self.state = 'moveright'
elseif self.x >= self.targetX2 then
self.state = 'moveleft'
end
elseif self.canMove == false then
self.state = 'idle'
end
if self.state == 'idle' then
self.vx = 0
elseif self.state == 'moveleft' then
self.vx = -25
self.scaleX = -1
elseif self.state == 'moveright' then
self.vx = 25
self.scaleX = 1
end
if self.vx == 0 then
else
self.anim:update(dt)
end
end
2. I try to use a timer library that pro has dedicated to the community (https://github.com/love2d-community/awe ... d#tweening). Specifically, I tried 'tick'. At first I tried this method before using method 1. I use the following statement:
Code: Select all
tick.recur(
function()
print("tick!")
if self.canMove == false then self.canMove = true elseif self.canMove == true then self.canMove == false end
end, 3)
For the second way, for my inferiority, it's a little hopeless :v. I'm struggling with the first method. But the current problem is that it's not what I want, the scorpion doesn't rest, but it keeps moving.
Hope you all take some time to help me. Can you tell me where my code is wrong? Your opinion on ways 1 and 2, which is feasible and better? And I would be happy if you taught me a way of turning my theoretical wishes into a more realistic reality than my dump way. I want the enemy to move for a short time then rest before moving on. I know this is a fairly simple problem, but I know my skills are also poor and insufficient so that I cannot solve this problem myself. ( Please sympathize with my bother. Please help me. Thank you so much.
Edit: In scorpion: update (dt) I realized the command had to fix from:
Code: Select all
if self.canMove then self.canMove = false end
if self.canMove == false then self.canMove = true end
Code: Select all
if self.canMove then self.canMove = false print('ok')
elseif self.canMove == false then self.canMove = true end