Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | aadyainfotainment |
I am stuck with this to create a 2d game similar to airline traffic.
similar to below image:
- If you touch the character, a line will be drawn from the touch/click point to the point where we release the touch/click.
- the airplanes follow the drawn path
- if you drag on the airplane again, the drawn path will get deleted and a new path will be drawn.
- When the airplane follows the line, it will erase the line.
Thanks in advance for your support
Which part are you having trouble with? Drawing the line or making the plane follow it?
Either way you need to use the PoolVector2Array type. If it’s drawing the path look at Line2D or if it’s making the plane follow it look at PathFollow2D.
Magso | 2020-04-09 17:42
I am trying to implement your given solution
My airplane script:
extends KinematicBody2D
var entered
var velocity = Vector2()
var speed = 20
# Called when the node enters the scene tree for the first time.
func _ready():
set_process_input(true)
#$planepath.global_position = Vector2(0,0)
func _physics_process(delta):
velocity = move_and_slide(velocity)
func start(pos, dir, speedx, speedy):
rotation = dir
position = pos
velocity = Vector2(speedx, speedy).rotated(rotation)
My Airport script
extends Node2D
var screen_size
var posArray = PoolVector2Array()
export (PackedScene) var bplane
var _dragging = false
var lineElement = Line2D.new()
var lineArray = PoolVector2Array()
# Called when the node enters the scene tree for the first time.
func _ready():
$planeTimer.start()
func _physics_process(delta):
if _dragging == false:
lineArray.resize(0)
func _on_startTimer_timeout():
$startTimer.stop()
$planeTimer.start()
func _on_planeTimer_timeout():
$planeTimer.stop()
spawn_planes()
$startTimer.start()
func spawn_planes():
var my_group_members = get_tree().get_nodes_in_group("startPos")
screen_size = get_viewport_rect().size
randomize()
var pl = bplane.instance()
var speedx
var speedy
var start_pos = my_group_members[randi() % my_group_members.size()]
if start_pos.is_in_group("leftG"):
var left_deg = [0, 45, 90]
speedx = 0
speedy = -20
pl.rotation = left_deg[randi() % left_deg.size()]
elif start_pos.is_in_group("rightG"):
var right_deg = [180, 225, 270]
speedx = 0
speedy = -20
pl.rotation = right_deg[randi() % right_deg.size()]
elif start_pos.is_in_group("topG"):
var top_deg = [90, 135, 180]
speedx = 0
speedy = -20
pl.rotation = top_deg[randi() % top_deg.size()]
elif start_pos.is_in_group("bottomG"):
var bot_deg = [315, 0, 45]
speedx = 0
speedy = -20
pl.rotation = bot_deg[randi() % bot_deg.size()]
pl.start(start_pos.global_position, pl.rotation, speedx, speedy)
add_child(pl)
func _input(event):
if event is InputEventMouseButton:
if event.is_action_pressed("click"):
print('mouse pressed')
_dragging = true
elif event.is_action_released("click"):
print('mouse released')
_dragging = false
if event is InputEventMouseMotion:
if _dragging:
_createArray(lineElement)
func _createArray(lineElement):
if lineArray.size() == 0:
lineArray.append(get_global_mouse_position())
else:
lineArray.append(get_global_mouse_position())
lineElement.set_points(lineArray)
It is spawning the planes but does not draw line when I click on the plane and drag.
I have also added Area2D named “detectTouch”
and
position2D in front of the plane named “directionStart”
I had earlier added the createArray function to the airplane script which was drawing a line but in weird places and also moved along with the plane so I decided to add line2D in the gameplay scene.
aadyainfotainment | 2020-04-10 16:50