![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | khai86 |
Hello, i encounter this problem while copy and paste a code that i found in here,
please take note i just started godot not even within 24 hour since this question is posted, what i am trying to dulpicate is a feature that will make the obeject (player) move to any location that is touch in the screen,
again here are the error:
Script inherits from native type ‘KinematicBody2D’, so it can’t be instanced in object of type ‘Node2D’.
and here are the script :
extends KinematicBody2D
const ACCELERATION = 20
const GRAVITY = 20
const MAX_SPEED = 200
const JUMP_HEIGHT = -500
const UP = Vector2(0,-1)
var motion = Vector2()
enum STATES {
MoveRight,
MoveLeft,
Jump
Idle
}
onready var state = STATES.Idle
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
if event.pressed:
var local_event = make_input_local(event)
if local_event.position.x > 20: #and local_event.position.y > -50:
state = STATES.MoveRight
elif local_event.position.x < -20: #and local_event.position.y > -50:
state = STATES.MoveLeft
elif round(local_event.position.y) in range(-400, -20):
state = STATES.Jump
else:
state = STATES.Idle
func _physics_process(delta):
motion.y += GRAVITY
var friction = false
match state:
STATES.MoveRight:
motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
$Sprite.flip_h = false
$Sprite.play(“Run”)
STATES.MoveLeft:
motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
$Sprite.flip_h = true
$Sprite.play(“Run”)
STATES.Idle:
$Sprite.play(“Idle”)
friction = true
STATES.Jump:
if is_on_floor():
motion.y = JUMP_HEIGHT
if friction == true:
motion.x = lerp(motion.x, 0, 0.2)
if motion.y < 0:
$Sprite.play(“Jump”)
else:
$Sprite.play(“Fall”)
if friction == true:
motion.x = lerp(motion.x, 0, 0.1)
motion = move_and_slide(motion, UP)
pass
Thank you for helping