0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

2021-08-01 Godot Engine > マウスクリックして線を描画する実装 (Array, update(), _draw(), draw_line(), for文)

Last updated at Posted at 2021-08-01

動作環境

Windows 10 Pro v21H1
Godot Engine v3.3.2.stable.official

Godotの学習歴 (今日開始: 2021-08-01)

処理の概要

マウスクリックごとに線分を描画する。

学習内容

  • Array
  • マウス押下処理
  • 描画 (update()と_draw())
    • _draw() : 一回だけ実行される。update()で再度実行される。
  • 線分の描画 draw_line()
  • for文

準備

  1. Area2Dのノードを作成する
  2. スクリプトのアタッチとして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()

実行例

image.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?