動作環境
Windows 10 Pro v21H1
Godot Engine v3.3.2.stable.official
Godotの学習歴 (今日開始: 2021-08-01)
処理の概要
マウスクリックごとに線分を描画する。
学習内容
- Array
- マウス押下処理
- 描画 (update()と_draw())
- _draw() : 一回だけ実行される。update()で再度実行される。
- 線分の描画 draw_line()
- for文
準備
- Area2Dのノードを作成する
- スクリプトのアタッチとしてmain.gdを定義する
実装
main.gd
extends Area2D
var poslist = []
var nowPos = null
# func _ready():
# pass # Replace with function body.
# func _process(delta):
# pass
func _draw():
if nowPos != null:
poslist.append(nowPos)
for idx in range(poslist.size() - 1):
draw_line(poslist[idx], poslist[idx+1], Color(255, 0, 0), 1)
func _input(event):
if event is InputEventMouseButton:
if event.pressed == false:
return
if event.button_index != 1:
return
nowPos = event.position
update()
実行例
参考
- GDScriptの基本
-
How to draw a line in 2D?
- updateと_draw()
-
マウスと入力座標
- event.positionなど
- 2Dカスタム描画
- ButtonList
- Array