我正在做一个非常简单的2D平台游戏项目,你可以在这里看到到目前为止的情况:https://master.d3kjckivyd1c76.amplifyapp.com/src/Games/PlatformGame2D101/index.html
但我无法察觉敌人(电锯)和玩家之间的碰撞。
我有这个代码来检测两个物体是否碰撞,但它只在玩家移动时打印Enemy
:
func _physics_process(delta: float) -> void:
velocity = move_and_slide(velocity, Vector2.UP)
for i in get_slide_count():
var collision = get_slide_collision(i)
if collision.collider.is_in_group("Enemy"): print("Enemy")
- 原始文件
- 我将项目上传到了Bitbucket
感谢任何帮助:)
我用pygame做了很多2d游戏,我用了这两个。
-
您可以使用sprite.sspritecollabotany(),它接受2个参数。你想要碰撞的精灵,在你的例子中是链锯和另一组精灵。这需要一个循环,看起来像这样:
for swordman in swordsmen1_army: # Swordsman1 attackin Swordsman2 colliding_sword2 = pygame.sprite.spritecollideany(swordman, swordsmen2_army) if colliding_sword2: swordman.attacking = True swordman.unleash = False if swordman.index == 3: colliding_sword2.health -= SWORD_HIT
-
如果你只想碰撞两个精灵,你可以使用pygame。Rect.collestrect(),用于检查是否检测到两个精灵。这个不需要循环,看起来像这样:
if pygame.Rect.colliderect(ball.rect, border1.rect): ball.down_collision = True ball_y_speed *= -1
它们都将返回一个布尔值,从那里,您可以使用if语句并继续执行代码。
答案:https://www.reddit.com/r/godot/comments/yojmz8/detect_collision_between_two_2d_kinematics_bodies/
但基本上玩家,它没有移动,所以它没有碰撞
以下是我为修复所做的更改:https://bitbucket.org/201flaviosilva-labs/platform-game-2d-101-godot/commits/61e37823eb9758fdafd222f2cdecf3e0668a3808