1

我能够很好地组装正在发生的事情并杀死我几个星期,我真的很想知道这是我的错误还是只是按键按下得太快,或者渲染延迟了?来解决这个问题,因为我没有找到这样做的罪魁祸首。

例如,当我从对角线向下向右移动时,就会发生这种情况,然后我又往下走,我做出这样的动作,速度很快,它并不总是发生,但足以扰乱游戏。

在此处输入图像描述

运动脚本:

extends KinematicBody2D

var velocity = 150
var direction = Vector2()

func _ready():
    set_fixed_process(true)

func _fixed_process(delta):
#-----------MOVIMENT-------------------------------
    direction = Vector2()
    #LEFT
    if Input.is_action_pressed("left"):
        direction += Vector2(-1, 0)
    #RIGHT
    elif Input.is_action_pressed("right"):
        direction += Vector2(1, 0)
    #UṔ
    if Input.is_action_pressed("up"):
        direction += Vector2(0, -1)
    #DOWN
    elif Input.is_action_pressed("down"):
        direction += Vector2(0, 1)

    if direction.x != 0 || direction.y != 0:
        direction = direction.normalized()*velocity
        move(direction*delta)

绘制脚本:

extends Control

var map_size = 64
var preScriptUtils = preload("res://scripts/utils_fuctions.gd")
var utils_functions

onready var player = $"../Player"
onready var tile_map = $"../TileMap"
var posTileMap = Vector2()
var posWorld = Vector2()
var prevRect = Rect2()
var newRect = Rect2()

var distance_gen = 2048
var positionMap_rect = 0
var size_rectMap = 0

func _ready():
    utils_functions = preScriptUtils.utils_functions.new()
    set_fixed_process(true)
    posWorld = Vector2(0, 0)
    positionMap_rect = ((distance_gen/32)/2)
    size_rectMap = distance_gen/32
    surround_map(posWorld)

func _fixed_process(delta):
    var posPlayer = player.get_position()

    posTileMap = tile_map.world_to_map(posPlayer) - Vector2(positionMap_rect, positionMap_rect)
    newRect = Rect2(tile_map.map_to_world(posTileMap), Vector2(distance_gen, distance_gen))
    if prevRect.position != newRect.position:
        load_newMap()

func gen_data(var _posWorld=Vector2(0, 0), var _posTileMap=Vector2(0, 0), var size=Vector2(0, 0)):
    var x = 0
    var y = 0
    var pos_modx = 0
    var pos_mody = 0
    while  x < size.x:
        y = 0
        pos_modx = utils_functions.mod(_posWorld.x + x, map_size)
            #NOISE
            var result_noise
        while y < size.y:
            pos_mody = utils_functions.mod(_posWorld.y + y, map_size)
            if (pos_modx >= 0 && pos_mody >= 0) && (pos_modx < map_size && pos_mody < map_size):
                tile_map.set_cell(_posTileMap.x + x, _posTileMap.y + y, biomes(result_noise))
            y += 1
        x += 1

func load_newMap():
    var rectIntersection = newRect.clip(prevRect)
    var _x = 0
    var _y = 0
    #LEFT
    if player.direction.x < 0:
        if int((newRect.size.x - rectIntersection.size.x)/32) == 1:
            _x = int(newRect.position.x/32)
            _y = int(rectIntersection.position.y/32)
            posWorld.x -= 1
            gen_data(Vector2(posWorld.x, posWorld.y), Vector2(_x, _y), Vector2(1, size_rectMap))
    #RIGHT
    elif player.direction.x > 0:
        if int((newRect.size.x - rectIntersection.size.x)/32) == 1:
            _x = int(rectIntersection.end.x/32)
            _y = int(rectIntersection.position.y/32)
            posWorld.x += 1
            Vector2(_x, _y)
            gen_data(Vector2(posWorld.x - 1, posWorld.y), Vector2(_x, _y), Vector2(1, size_rectMap))
    #UP
    if player.direction.y < 0:
        if int((newRect.size.y - rectIntersection.size.y)/32) == 1:
            _x = int(newRect.position.x/32)
            _y = int(newRect.position.y/32)
            posWorld.y -= 1
            gen_data(Vector2(posWorld.x, posWorld.y), Vector2(_x, _y), Vector2(size_rectMap, 1))
    #DOWN
    elif player.direction.y > 0:
        if int((newRect.size.y - rectIntersection.size.y)/32) == 1:
            _x = int(rectIntersection.position.x/32)
            _y = int(rectIntersection.end.y/32)
            posWorld.y += 1
            gen_data(Vector2(posWorld.x, posWorld.y - 1), Vector2(_x, _y), Vector2(size_rectMap, 1))
    prevRect = newRect

func surround_map(var _posWorld=Vector2(0, 0)):
    posTileMap = - Vector2(positionMap_rect, positionMap_rect)
    prevRect = Rect2(tile_map.map_to_world(posTileMap), Vector2(distance_gen, distance_gen))
    gen_data(_posWorld, Vector2(-32, -32), Vector2(64, 64))

不要对代码大惊小怪,我想做的很简单,我想在生成我的世界的同时移动玩家,它在 4 个方向上完美运行,但是当它涉及对角线时,它不会去正如预期的那样。我是这样画的,我有两个矩形与每一帧进行比较,我取截取的 retagunlo 并计算我应该在哪里绘制或擦除。

在此处输入图像描述

正在发生的事情的视频:

http://www.dailymotion.com/video/x6286px

但是有这些缺陷不仅是按得太快,它也发生在慢动作中,而对角线右下然后左或左右然后再右,对角线移动似乎是问题,我'我尝试了很多形状,我几乎有一个笔记本都在试图弄清楚这一点,我也尝试过对角线检查,我得到了相同的结果,今天我又输了一天,它并没有离开这个地方,我像这样的几个星期,当我认为我修复它时,不是真的。

我正在使用godot。

上 1:

通过@Ryan1729 的回复,我意识到我真的在想错误的方式,但是我尝试通过执行以下操作来修复我的逻辑并且错误仍在继续,我很困惑:

if direction.x != 0 || direction.y != 0:
        var vetor_normalized = direction.normalized()
        if abs(vetor_normalized.x) < 1 && abs(vetor_normalized.y) < 1:
            var vx = Vector2(direction.x, 0)
            var vy = Vector2(0, direction.y)
            vx = vx.normalized()*velocity
            move(vx*delta)
            vy = vy.normalized()*velocity
            move(vy*delta)

        else:
            direction = direction.normalized()*velocity
            move(direction*delta)

上2:

现在我这样做了,似乎解决了缺陷,但出现了其他缺陷,我仍然不明白,这个0的问题让我太困惑了。

  var oldPosWorld = Vector2(0, 0)
  var rect_tileMap = Vector2(-32, -32) 

func load_newMap():
 var posPlayer = Vector2(int(player.get_position().x/32), int(player.get_position().y/32))
    #LEFT
    if posPlayer.x < oldPosWorld.x:
            pos_tileMap.x -= 1
            posWorld.x -= 1
            gen_data(Vector2(posWorld.x, posWorld.y), Vector2(pos_tileMap.x, pos_tileMap.y), Vector2(1, size_rectMap))
    #RIGHT
    elif posPlayer.x > oldPosWorld.x:
            pos_tileMap.x += 1
            posWorld.x += 1
            gen_data(Vector2(posWorld.x - 1, posWorld.y), Vector2(pos_tileMap.x + 63, pos_tileMap.y), Vector2(1, size_rectMap))
    #UP
    if posPlayer.y < oldPosWorld.y:
            pos_tileMap.y -= 1
            posWorld.y -= 1
            gen_data(Vector2(posWorld.x, posWorld.y), Vector2(pos_tileMap.x, pos_tileMap.y), Vector2(size_rectMap, 1))
    #DOWN
    elif posPlayer.y > oldPosWorld.y:
            pos_tileMap.y += 1
            posWorld.y += 1
            gen_data(Vector2(posWorld.x, posWorld.y - 1), Vector2(pos_tileMap.x, pos_tileMap.y + 63), Vector2(size_rectMap, 1))

oldPosWorld = posPlayer

在此处输入图像描述

在此处输入图像描述

右边和底部的运动是正确的,问题是左边和上面的运动是留在这个空白区域(即处理底片)。

如果我把 x - 1 放在左边来解决问题,这发生在我身上:

在此处输入图像描述

我通过更改解决了这个问题:

var posPlayer = Vector2(int(player.get_position().x/32), int(player.get_position().y/32))

通过这样的检查:

if ((player.get_position().x)/32)

但现在我通过走到两边然后向上来得到这个:

在此处输入图像描述

上升 3:

我不会全神贯注,以免在我弄错时感到沮丧,但是通过测试,我停止了失败。通过将 int (player.get_position ().X / 32) 分别分配给 oldPosWorld.x,代码看起来像这样,这也分别为 y,因为在我将位置分配给玩家之前,只有一次 postPlayer 变量,这导致错误,当你进入 up-down if 时,因为它仍然在 x 的先前位置,这样做会在正确的时间更新所有内容。

仍然如此向左和向上:

在此处输入图像描述

我不太明白为什么,坦率地说我不会很快知道,我太累了,看不到它。但我想明白不要犯错。除此之外,它似乎正在按预期发生。

#LEFT
if int(player.get_position().x/32) < oldPosWorld.x:
            rect_tileMap.x -= 1
            posWorld.x -= 1
            gen_data(Vector2(posWorld.x, posWorld.y), Vector2(rect_tileMap.x, rect_tileMap.y), Vector2(1, size_rectMap))
            oldPosWorld.x = int(player.get_position().x/32)
4

2 回答 2

1

我认为问题在于您想要离散移动(向上移动和向右移动 1 向上和向右移动 1)但是您在第一个代码片段中将向量标准化为长度 1。在非对角线的情况下,这没有任何作用,因为向量的长度已经为 1。但是在对角线的情况下,向量是sqrt(2)标准化之前的长度,(你可以使用勾股定理来证明这一点。)所以你的向量被缩小到类似Vector2(sqrt(2)/2, sqrt(2)/2)),(sqrt(2)/2大约是 0.707,)这会导致绘画偏移。

编辑:我现在最好的猜测是问题是gen_data当玩家对角移动时你会跟注两次。我想你想弄清楚 x 和 y 偏移然后重绘地图。

于 2017-09-28T00:56:16.577 回答
0

总结错误是在更新中(在这种情况下是oldPosWorld),因为它们是两个ifs块,我会一个接一个地输入,并且不更新位置,绘制错误的方式,因为它处于后期位置:

视频工作:

http://www.dailymotion.com/video/x62e6l9

于 2017-09-28T17:37:28.830 回答