Hello, Im wondering if i am able to use timers with extends KinematicBody2D
. I want to avoid using the engines update/tick function as much as possible and thought timers would be a good route to take.
Before, I had the code below using extends Node
, which made the timers work, but I could not get position
to modify the enemies position on screen. Alternatively when I used extends KinematicBody2D
i could get the position
, but the timers would no longer work.
I was thinking of making a child node and creating a script using extends Node
. This would let me make timers that would call functions in the parent node containing a script using extends KinematicBody2D
, so i can update the position of the enemy.
I was wondering if there is a simpler way to do this or if there are better ways of achieving what i am trying to do.
Thank you!
extends KinematicBody2D
var timerMovement = null
var timerAttack = null
var player = null
var currentPosition = Vector2(0,0)
export(int) var currentTileY = 1
func _enemyAttack1():
#Shoot Projectile
print("Stop movement timer and shoot projectile")
pass
func _enemyMovement():
#Get the position of the player
var playerX = player._GetCurrentTileX()
var playerY = player._GetCurrentTileY()
#If player is above the enemy, move the enemy one space above
if playerY > currentTileY:
position += Vector2(0, -256)
currentPosition += Vector2(0, -256)
currentTileY += 1
#Move one space below otherwise
elif playerY < currentTileY:
position += Vector2(0, 256)
currentPosition += Vector2(0, 256)
currentTileY -= 1
else:
#Else stay in place
pass
#Spawn Projectile that moves forward
pass
func _wheresThePlayer():
print("Where the hell is the player?")
func _ready():
#Check if the playerBattle node exists in the scene before assigning it.
if has_node("../playerBattle"):
player = get_node("../playerBattle")
else:
_wheresThePlayer()
currentPosition = position
#Add a timer make the enemy attack
timerAttack = Timer.new()
add_child(timerAttack)
#Every x seconds
timerAttack.connect("timerAttack", self, "_enemyAttack1")
timerAttack.set_wait_time(5.0)
timerAttack.set_one_shot(false) # loop timer
timerAttack.start()
#Add a timer to make enemy move
timerMovement = Timer.new()
add_child(timerMovement)
#Every x seconds
timerMovement.connect("timerMovement", self, "_enemyMovement")
timerMovement.set_wait_time(1.0)
timerMovement.set_one_shot(false) # loop timer
timerMovement.start()