Skip to content

如何在你的游戏中移动对象

在屏幕上移动对象是任何游戏的重要组成部分。这包括:

  • 移动屏幕上的角色:玩家、非玩家角色(NPC)、车辆、太空飞船...
  • 动画物品:子弹、激光、障碍、背景...
  • 动画界面:按钮、光标、剩余生命...

每种情况都不同:本页解释了在使用GDevelop时改变对象位置的不同解决方案。

在GDevelop中有三种不同的改变对象位置的方式:

  • 使用操作来改变X和Y坐标,
  • 对对象施加力(适用于所有对象),
  • 使用行为(内部会改变坐标或使用力!)。

改变对象位置

有时你希望立即改变对象的位置,而不是逐渐移动或动画化它。在这种情况下,使用操作将对象设置到特定位置(通过指定新的X和Y坐标):

设置对象位置

在两个位置之间移动对象

可以使用tween行为来在一定时间内将对象移动到新位置,并使用各种速度模式。

将多个对象一起移动

如果希望一个对象完全跟随另一个对象,或固定在另一个对象的位置上,可以使用操作来改变对象位置,并使用表达式来获取要跟随对象的X和Y坐标。

在一个方向上使用力移动对象

移动对象的最简单方法是连续移动对象,或当发生一些事件时,比如玩家按下按钮时,使用

永久力

永久力会永远移动对象,除非使用停止对象的操作。这将清除作用在对象上的力(有效地停止它 - 除非使用某些行为或其他事件改变其位置)。例如,以下事件将在两个标记之间动画一个平台:

永久力动画平台

使用平台行为移动角色

如果你的对象是平台游戏中的角色,则平台行为可以让你在游戏中使用现成的平台引擎。

以圆形移动对象

可使用椭圆运动

使对象弹跳

可使用弹跳行为。After adding it to an object, add a condition to check if the object is in collision with an obstacle. Add an action to then make the object bounce from the obstacle.

Note

This behavior is working with forces! Make your object move by adding a force to it. Then add the behavior and the action to make it bounce in case of collisions

Find a path around obstacles

The Pathfinding behavior is an advanced algorithm that compute a path between an object position and a destination position. The path will be the shortest one avoiding other objects marked as obstacles.

It's useful for strategy games and any game where characters or units must move on their own and avoid obstacles.

Pin an object on screen edges when the window is resized

With the Anchor behavior, you can set your object to be pinned to the top, left, bottom or right of the game. If the game window is resized, and the game resolution is changed, the object will be automatically re-adjusted to be at the same distance from the window border as before.

This is very useful for visual controls for mobile games and for user interface/control panels to show on the screen.

Find other movement extensions

There are many more behaviors that move objects listed in the extensions page.