![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Li282 |
So I literally just started learning how to use Godot. I have zero experience with scripting and have no idea how to fix the error I keep running into.
During the tutorial, I am at the part where you need to add “game_over” and “new_game”. This is the script I have:
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
The line “score = 0” gets highlighted in red and I get an error that reads this:
The identifier “score” isn’t declared in the current scope.
I decided to move on thinking maybe something I input later would fix it, but then when I come to the end to check to make sure everything works, I am told to add a line under the _ready function as typed below:
func _ready():
randomize()
new_game()
The line with “new_game()” gets highlighted in red and I get a similar error that reads this:
The method “new_game()” isn’t declared in the current class.
I typed everything in as shown and I am not sure what it is I am doing wrong nor how to declare anything. Help would be much appreciated. Thank you.
Here is the whole script for Main.gd and Player.gd where the problems occur.
Main.gd
extends Node
export (PackedScene) var Mob
var score
func _ready():
randomize()
new_game()
func _on_StartTimer_timeout():
$MobTimer.start()
$ScoreTimer.start()
func _on_ScoreTimer_timeout():
score += 1
func _on_MobTimer_timeout():
$MobPath/MobSpawnLocation.offset = randi()
var mob = Mob.instance()
add_child(mob)
var direction = $MobPath/MobSpawnLocation.rotation + PI / 2
mob.position = $MobPath/MobSpawnLocation.position
direction += rand_range(-PI / 4, PI / 4)
mob.rotation = direction
mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
mob.linear_velocity = mob.linear_velocity.rotated(direction)
Player.gd
extends Area2D
signal hit
export var speed = 400
var screen_size
func _ready():
screen_size = get_viewport_rect().size
hide()
func _process(delta):
var velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
else:
$AnimatedSprite.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
if velocity.x != 0:
$AnimatedSprite.animation = "walk"
$AnimatedSprite.flip_v = false
$AnimatedSprite.flip_h = velocity.x < 0
elif velocity.y != 0:
$AnimatedSprite.animation = "up"
$AnimatedSprite.flip_v = velocity.y > 0
func _on_Player_body_entered(body):
hide()
emit_signal("hit")
$CollisionShape2D.set_deferred("diable", true)
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
Starting think maybe I put something in the wrong script or misread something.
Please post your whole script for “Main.gd”.
kidscancode | 2020-07-24 09:36
I edited my post to add the script for both main.gd and player.gd since that is where the problems occur.
Li282 | 2020-07-24 20:04