Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Siddharth Pushkar |
I am making a 2D platformer game for a game jam and later on publishing it on Google Play Store.
In this game, we control a Game Boy character jumping around platforms dodging enemies and entering a portal to finish the level. I have made 5 types of enemies in which 4 of them are ground enemies, and one is a flying one. I decided to make this flying one to follow the player.
I watched this video and done this so far.
But when I run the scene in the “Level” Node(Where everything is), it would show an error.
Invalid get index ‘global_postion’(on base:KinematicBody2D(Memo.gd))
PLEASE SOLVE MY PROBLEM
Main Player script (Memo)
var velocity = Vector2.ZERO
var coin = 0
const gravity = 35
const jump = -1070
const SPEED = 290
#Movement of Memo
func _physics_process(delta):
if Input.is_action_pressed("right"):
velocity.x = SPEED
$Sprite.play("Walk")
$Sprite.flip_h = false
elif Input.is_action_pressed("left"):
velocity.x = -SPEED
$Sprite.play("Walk")
$Sprite.flip_h = true
else:
$Sprite.play("Idle")
if not is_on_floor():
$Sprite.play("Airjump")
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump
velocity.y = velocity.y + gravity
velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.2)
func _on_Area2D_body_entered(body):
get_tree().change_scene("res://Level1.tscn")
Player Detection Zone
var player = null
func can_see_player():
return player != null
func _on_PlayerDetectionZone_body_entered(body):
player = body
func _on_PlayerDetectionZone_body_exited(body):
player = null
Flying enemy script (Flying Eye)
export var direction = 1
export var ACCELERATION = 300
export var MAX_SPEED = 50
export var FRICTION = 200
onready var PlayerDetectionZone = $PlayerDetectionZone
var velocity = Vector2.ZERO
enum{
IDLE,
WANDER,
CHASE
}
var state = CHASE
func _ready():
if direction == -1:
$AnimatedSprite.flip_h = true
func _physics_process(delta):
if is_on_wall():
direction = direction * -1
$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
match state:
IDLE:
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
seek_player()
WANDER:
pass
CHASE:
var player = PlayerDetectionZone.player
if player != null:
var direction = (player.global_postion - global_position).normalized()
velocity = velocity.move_toward(direction * MAX_SPEED,
ACCELERATION * delta)
velocity = move_and_slide(velocity)
func seek_player():
if PlayerDetectionZone.can_see_player():
state = CHASE
what line of code is the error referring to?
Millard | 2020-10-19 05:08
When I run the main level where everything is like the player, enemies, tiles, etc
it shows me an error of
Invalid get index ‘global_postion’(on base:KinematicBody2D(Memo.gd))
in this line of code
Flying enemy script (Flying Eye)
CHASE:
var player = PlayerDetectionZone.player
if player != null:
var direction = (player.global_postion - global_position).normalized() # IN THIS LINE OF CODE
velocity = velocity.move_toward(direction * MAX_SPEED,
ACCELERATION * delta)
Siddharth Pushkar | 2020-10-19 05:33
Please reply
Siddharth Pushkar | 2020-10-19 09:56