在屏幕上时敌人会停止移动,但会在屏幕外起作用

时间:2019-01-31 15:53:40

标签: lag godot gdscript

在godot 3.0中,我设置了平台游戏。我意识到滞后是一个问题,因此我设计了滞后问题,以便在离开屏幕左侧后将其破坏。在大多数情况下,敌人会起作用。除了最后一个,他会发射弹丸直到它出现在屏幕上,然后它会完全静止。玩家可以推动它们前进,但不会做任何事情。当所有敌人,街区等掉落到左侧或底部太深时(游戏向右移动),都可以使用queue_free()方法。

很抱歉,我无法获得有关该问题的视频,我的计算机无法同时运行屏幕录制软件和游戏,而游戏又不会太笨拙而无法使用。

如果将敌人移到更靠近中心的位置以使其开始发动,因此较早遇到,它将起作用。我尝试过让项目落在屏幕左侧或屏幕下方后自行删除,这解决了第1级而不是第2级的相同问题。

#Here is my code for my enemies, all pound signs for comments are replaced with // so that stack overflow wouldn't bold them:
extends KinematicBody2D

var playerpos = Vector2(0, 0)
var left = true

var baton = load("res://Items/Baton/Baton.tscn")
var BatPick = load("res://Items/Baton/BatonPickup.tscn")

var shoot_timer = 0.0

var walk_speed = 425
var CollidingWithPlayer = false
var velocity = Vector2(0, 100)

var HP = 10

func _ready():
    //Flip player checking raycast back and forth
    get_node("Raycast").add_exception(get_node("Area"))
    set_physics_process(true)
    set_process(true)
    pass

//Raycast both sides
func _process(delta):
    //die if HP is less than zero and drop the baton
    get_node("Raycast").rotate(180)
    if HP <= 0:
        var pickup = BatPick.instance()
        get_tree().get_root().add_child(pickup)
        pickup.global_position = global_position
        queue_free()
    pass

//Check for player in raycast, throw if possible. Move towards it if wouldn't fall
func _physics_process(delta):
    shoot_timer = shoot_timer + delta
    //Check for player, throw baton if player is in range and 5 seconds or more has passed since it was thrown last
    if get_node("Raycast").is_colliding():
        var obj = get_node("Raycast").get_collider()
        if obj != null and obj.get_name() == "Area":
            obj = obj.get_parent()
        if obj != null and obj.get_name() == "Player":
            CollidingWithPlayer = true
            playerpos = obj.global_position
            //Throw baton and walk in player's direction if I wouldn't fall
            if playerpos.x < global_position.x and shoot_timer >= 1:
                left = true
                throw_baton()
                shoot_timer = 0
                get_node("AnimatedSprite").play("WalkingL")
                if not (left == true and (not get_node("CheckLeft").is_colliding())) or (left == false and (not get_node("CheckRight").is_colliding())):
                    velocity.x = -300
                move_and_slide(velocity, Vector2(0, -50))
            elif playerpos.x > global_position.x and shoot_timer >= 1:
                left = false
                throw_baton()
                shoot_timer = 0
                get_node("AnimatedSprite").play("WalkingR")
                if not(left == true and (not get_node("CheckLeft").is_colliding())) or (left == false and (not get_node("CheckRight").is_colliding())):
                    velocity.x = 300
                move_and_slide(velocity, Vector2(0, -50))
        else:
            CollidingWithPlayer = false

            get_node("AnimatedSprite").stop()
            get_node("AnimatedSprite").frame = 0
    else:
        CollidingWithPlayer = false
        get_node("AnimatedSprite").stop()
        get_node("AnimatedSprite").frame = 0

    get_node("CheckLeft").force_raycast_update()
    get_node("CheckRight").force_raycast_update()
    if (left == true and (not get_node("CheckLeft").is_colliding())) or (left == false and (not get_node("CheckRight").is_colliding())):
        velocity.x = 0

    //delete if fallen
    if global_position.y >= 650:
        queue_free()

    move_and_slide(velocity, Vector2(0, -500))
    pass
//Throw baton
func throw_baton():
    var projectile = baton.instance()
    get_tree().get_root().add_child(projectile)
    if left == true:
        projectile.global_position = global_position + Vector2(-60, 0)
        projectile.velocity = Vector2(-500, 0)
        get_tree().get_root().get_node("Baton").rotation_degrees = 180
    if left == false:
        projectile.global_position = global_position + Vector2(60, 0)
        projectile.velocity = Vector2(500, 0)
    pass

func take_damage(damage):
    HP -= damage
    pass

#Here is my code for the parent node that slides the blocks, items, enemies, etc across the board (the camera doesn't move and the player stays within the same "box" as everything moves towards the player):

extends Node2D

const endpos = 99999
var speed = -2.5


func _ready():
    get_node("True_Player").endpos = endpos
    pass

func _process(delta):
    get_node("ALL THE STUFF").move_and_collide(Vector2(speed, 0))
    //set if statement to close and execute corresponding file when endpos is reached
    pass

#The following code is in everything except the player and enemies so that they are deleted when they fall too far left, the disappear to avoid lag:

if global_position.x < -50:
        queue_free()

如果玩家足够靠近并越过射线投射并且敌人不会掉落,则敌人应朝玩家移动。然后,他们应该投掷弹丸。

最终发生的事情是,当玩家越过光线投射时,它们确实会投掷弹丸,但直到敌人出现在屏幕上为止。这仅适用于玩家在等级下降约一分钟后遇到的敌人,然后敌人才能按预期发挥作用。

1 个答案:

答案 0 :(得分:0)

由于您意识到这是一个滞后问题,所以让我给您一个小小的性能提示:

每次要获取节点时,

请勿不要使用get_node,因为它将每次都进行搜索。 相反,您可以在func _ready()上初始化变量并在其中保留节点引用,也可以直接使用方便的语法onready var check_left = get_node("CheckLeft")来做到这一点。