Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | jymis |
Just trying to continue a 2d isometric game from Godot 2 to 3. Have an issue with character controller, ‘func _input(event):’ related.
Here’s the script:
extends KinematicBody2D
# movement related variables and constants
var vel
var moveDir
# direction constants
var DIR_LEFT = Vector2(-64, -32).normalized()
var DIR_RIGHT = Vector2(64, 32).normalized()
var DIR_UP = Vector2(64, -32).normalized()
var DIR_DOWN = Vector2(-64, 32).normalized()
var DIR_ZERO = Vector2(0, 0)
const MAX_SPEED = 1.30
const MOVE_FORCE = 30.0
const GRID = 20
var animSprite
var animPlayer
func _ready():
# initializing params
vel = DIR_ZERO
moveDir = DIR_ZERO
animSprite = get_node("animSprite")
animPlayer = get_node("animPlayer")
# enabling process
set_physics_process(true)
set_process_input(true)
func _physics_process(delta):
# calculate velocity
var deltaV = moveDir * MOVE_FORCE * delta
if vel.length() + deltaV.length() <= MAX_SPEED:
vel += deltaV
else: # cap max velocity
vel = moveDir * MAX_SPEED
# move the char if dir is non-zero
if moveDir != DIR_ZERO:
move_and_collide((vel)
# process button press from player
func _input(event):
# controls for PC
if event is InputEventKey:
# deciding the direction of movement and setting animation
if event.is_action_pressed("down_move") && !event.is_echo():
moveDir = DIR_DOWN
animSprite.set_frame(67)
animPlayer.play("walk_T2B")
if event.is_action_pressed("up_move") && !event.is_echo():
moveDir = DIR_UP
animSprite.set_frame(50)
animPlayer.play("walk_B2T")
if event.is_action_pressed("left_move") && !event.is_echo():
moveDir = DIR_LEFT
animSprite.set_frame(57)
animPlayer.play("walk_R2L")
if event.is_action_pressed("right_move") && !event.is_echo():
moveDir = DIR_RIGHT
animSprite.set_frame(62)
animPlayer.play("walk_L2R")
# set moveDir as zero upon release and pause the animation
else (event.is_action_released("down_move") && moveDir == DIR_DOWN)
|| (event.is_action_released("up_move") && moveDir == DIR_UP)
|| (event.is_action_released("left_move") && moveDir == DIR_LEFT)
|| (event.is_action_released("right_move")&& moveDir == DIR_RIGHT):
moveDir = DIR_ZERO
# pause the animation
animPlayer.stop(true)
#controls for touch devices — to be expanded later
if event.type == InputEvent.SCREEN_TOUCH:
moveDir = DIR_RIGHT
Are you saying that somewhere in this script you have an error? Can you indicate what line?
kidscancode | 2020-01-05 19:02
It says at ‘func _input(event):’ line.
But i guess error comes from inside that function.
jymis | 2020-01-05 19:09