- Godot Engine Game Development Projects
- Chris Bradfield
- 323字
- 2021-06-18 18:51:27
Scripting the Player
Now, you're ready to add a script. Scripts allow you to add additional functionality that isn't provided by the built-in nodes. Click the Player node and click the Add Script button:

In the Script Settings window, you can leave the default settings as they are. If you've remembered to save the scene (see the preceding screenshot), the script will automatically be named to match the scene's name. Click Create and you'll be taken to the script window. Your script will contain some default comments and hints. You can remove the comments (lines starting with #). Refer to the following code snippet:
extends Area2D
# class member variables go here, for example:
# var a = 2
# var b = "textvar"
func _ready():
# Called every time the node is added to the scene.
# Initialization here
pass
#func _process(delta):
# # Called every frame. Delta is time since last frame.
# # Update game logic here.
# pass
The first line of every script will describe what type of node it is attached to. Next, you'll define your class variables:
extends Area2D
export (int) var speed
var velocity = Vector2()
var screensize = Vector2(480, 720)
Using the export keyword on the speed variable allows you to set its value in the Inspector, as well as letting the Inspector know what type of data the variable should contain. This can be very handy for values that you want to be able to adjust, just like you adjust a node's built-in properties. Click on the Player node and set the Speed property to 350, as shown in the following screenshot:

velocity will contain the character's current movement speed and direction, and screensize will be used to set the limits of the player's movement. Later, the game's main scene will set this variable, but for now you will set it manually so you can test.
- 7天精通Dreamweaver CS5網(wǎng)頁設(shè)計與制作
- 計算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ)
- Implementing Splunk 7(Third Edition)
- Apache Superset Quick Start Guide
- Microsoft System Center Confi guration Manager
- HTML5 Canvas Cookbook
- 啊哈C!思考快你一步
- Mastering Geospatial Analysis with Python
- 學(xué)練一本通:51單片機(jī)應(yīng)用技術(shù)
- 大數(shù)據(jù)案例精析
- Mastering MongoDB 3.x
- INSTANT VMware vCloud Starter
- Photoshop CS4數(shù)碼攝影處理50例
- Xilinx FPGA高級設(shè)計及應(yīng)用
- Cisco UCS Cookbook