• 2D
  • Move the nodes or move the camera ?

Hi everyone,

I am kind of new to Godot and I am training myself by creating an Age-of-Empire-like game :)

To display the map in my game , I am using a lot of Sprites (something like 8000), and I don't use TileMap because I need each sprite to have a custom behaviour, described in a script. (Actually, each sprite is a scene)

The context is the following:

--> World (Node)
       --> Map (Area2D)
             --> Sprite1 (Sprite)
             --> Sprite2 (Sprite)
             --> etc...

I display more Sprites than the player can see, and I want him to be able to "drag" or "move" the map on click (as in Age of Empire)

+-------+--------+-------+
|       |        |       |
|       |   UP   |       |
+------------------------+
| LEFT  |player's| RIGHT |
|       | screen |       |
+------------------------+
|       |        |       |
|       |  DOWN  |       |
+-------+--------+-------+

I am wondering if I have to change the position of each Sprite, but it looks like an expensive process to me, or is there a way to deal with this by moving a Camera2D ? (but I don't understand how to use a Camera2D)

Thank you all in advance :)

Check out the Platformer demo, that has an example of a Camera 2D attached to a player node that basically moves around the scene as you do. I'd play around with coding some basic panning movement, just check to see if the mouse cursor is close to an edge and update the Camera's position to compensate. Alternatively add some GUI elements to perform the movement on click, but the first option is the most intuitive.

You shouldn't have to move the camera at all, actually took me a bit to figure it out as I'm so used to having to either write a custom camera or it's built into the engine like Game Maker.

Here's what you need to do. Create a scene and we'll save it as player.

Node hierarchy. KinematicBody (choose this for 99.9% of games) -- Camera2D (make sure to tick current) -- Sprite (just something to test with) ---CollisionShape2D (choose whatever shape you want) ---RayCast2D

Now we just need a basic script, this ought to do.

var curPos

func _ready():
	curPos = get_pos()
	set_process_input(true)
	set_process(true)
	pass

func _input(event):
	if(event.type == InputEvent.KEY):
		if(event.scancode == KEY_Z):
			curPos.x = curPos.x - 1
		if(event.scancode == KEY_X):
			curPos.x = curPos.x + 1
		if(event.scancode == KEY_A):
			curPos.y = curPos.y - 1
		if(event.scancode == KEY_S):
			curPos.y = curPos.y + 1

func _process(delta):
	set_pos(curPos)

Now on your main scene add your player scene and give it your player script, your sprite will move in 1 pixel per frame but the sprite shakes... can't say if that's a bug for just using x/y pixel movement or windows 10. Your sprite should stay centered within the camera which is what you want, it will also move the camera as well.