当前位置: 首页 > news >正文

godot.bk

 1.搜索godot国内镜像,直接安装,mono是csharp版本

2.直接解压,50m,无需安装,直接运行 

3.godot里分为场景,节点

主场景用control场景,下面挂textureact放背景图片,右键实例化子场景把角色场景加进来

角色场景用node2d场景,下面挂node2d节点,下面挂sprite节点放图片做player

子弹场景用node2d场景,下面挂label节点写一个“O”做子弹

如下:

4.sprite脚本

extends Sprite2D  # 继承自Node2D,或者如果你的角色是一个精灵,可以继承自Sprite  #var speed = 200  # 角色的移动速度,可以根据需要调整  
var bullet_speed = 100
var bullet_instance = null
@onready var bullet_scene = preload("res://botton.tscn")  
@onready var player_sprite = $player_sprite
var flag = 0;# 当节点首次进入场景树时被调用  
func _ready():  if(player_sprite == null):print("player_sprite null>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")flag = 1;pass  # 初始化代码可以放在这里,但在这个例子中我们不需要  func field_booton():bullet_instance = bullet_scene.instantiate() get_tree().root.add_child(bullet_instance)if(player_sprite):bullet_instance.position = player_sprite.positionelse:bullet_instance.position = position	func _process(delta):  var velocity = Vector2.ZERO  # 初始化速度为0  # 检测WSAD键并设置相应的速度  if Input.is_action_pressed("ui_up"):  #velocity.y -= 1  position = position + Vector2(0, -5)if Input.is_action_pressed("ui_down"):  #velocity.y += 1  position = position + Vector2(0, 5)if Input.is_action_pressed("ui_left"):  #velocity.x -= 1  position = position + Vector2(-5, 0)if Input.is_action_pressed("ui_right"):  #velocity.x += 1  position = position + Vector2(5, 0)if Input.is_action_just_pressed("ui_accept"):  field_booton()# 标准化速度向量(可选,取决于你是否想要对角移动速度保持一致)  #if velocity.length_squared() > 0:  #velocity = velocity.normalized() * speed  # 更新角色的位置  #position += velocity * deltaif bullet_instance:bullet_instance.position.x += bullet_speed * delta

注意:
1、应该用delta保持不同平台的移动是一样的

2、@onready var player_sprite = $player_sprite:这个拿到的是null,不知道为什么:为什么是null,因为$是在子节点里找child,我们加脚本应该在node2d里加,不应该在sprite2d里加

3、onready已经被废弃,使用@onready

4、instanse()已经被废弃,使用instansite()

5、不能直接add_child(bullet_instance),而是应该get_tree().root.add_child(bullet_instance),不然子弹和sprite其实是一个节点,上下移动都是相对的

 5.botton脚本

extends Label# Called when the node enters the scene tree for the first time.
func _ready():pass # Replace with function body.# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):position = position + Vector2(4, 0)pass

6.注意connect在godot4也被改了,改成两个参数了

func _ready():
# 假设我们有一个 Button 节点实例,并且我们想要连接其 'pressed' 信号到一个函数  connect("button_down", self._on_button_pressed)  # 然后定义处理按钮点击的函数  
func _on_button_pressed():  print("click the button")pass # Replace with function body.

7.注意button只能是contrl和node的子类,不能是node2d的子类

8.注意层级关系 

场景
main(contrl)
--background(textureact)
--player(node2d, this position add script)--player(sprite),preload(res//boom.tscn)
--A(contrl)--A(button), preload(res//menu.tscn)资源
menu.tscn(button)--get_tree().root.add_child()
boom.tscn(label or others)--get_tree().root.add_child()

9.lable不能放在node下面,应该放在node2的下面

http://www.lryc.cn/news/362028.html

相关文章:

  • 【C++修行之道】类和对象(三)拷贝构造函数
  • 校园外卖系统的技术架构与实现方案
  • AI的制作思维导图
  • Amazon云计算AWS(四)
  • 数据库(21)——数值函数
  • 【PB案例学习笔记】-15怎样限制应用程序运行次数?
  • Spring为什么不支持static字段注入
  • AI数据分析:用Kimi根据Excel表格数据绘制多条折线图
  • 高级 Go 程序设计:使用 net/http/httputil 包构建高效网络服务
  • Android11 AudioTrack 创建过程
  • 数学建模 —— 层次分析法(2)
  • Nvidia Jetson/Orin +FPGA+AI大算力边缘计算盒子:人工智能消防应用
  • Flutter 中的 KeepAlive 小部件:全面指南
  • C语言 恼人的结合性和优先级和副作用
  • Vue——初识组件
  • MQ消息丢失/重复/顺序/挤压
  • 利用Quarkus构建高效微服务——Java的云原生革新
  • python 批量ts合并成一个mp4
  • Java | Leetcode Java题解之第129题求根节点到叶节点数字之和
  • SpringBoot【注解 01】@Scheduled实现定时任务的串行和并行执行
  • 【工具】redis的安装使用
  • 汇编:数据定义数据填充
  • Python画图(多图展示在一个平面)
  • python-web应用程序-Django数据库-操作表中的数据
  • 绕过WAF(Web应用程序防火墙)--介绍、主要功能、部署模式、分类及注入绕过方式等
  • 11.7 堆排序
  • Patchwork++:基于点云的快速、稳健的地面分割方法
  • Llama改进之——分组查询注意力
  • 英伟达开源新利器NV-Embed向量模型,基于双向注意力的LLM嵌入模型,MTEB 56项任务排名第一
  • JVM之【GC-垃圾清除算法】