so I have this peice of code and it doesn`t work can someone please help?
extends CharacterBody2D
var picked = get(“metadata/picked”)
var empire = get(“metadata/empire”)
var t_p = Vector2()
var MoveSpeed = 100
var Move_mode = true
var sprite = Image.load_from_file(“res://newmap.png”)
func _ready():
t_p = position
if empire == null:
empire = 0
if picked == null:
picked = false
func _process(delta):
if Input.is_action_just_pressed(“left_click”):
var t_g = get_global_mouse_position()
if picked == true:
if Move_mode == true:
t_p = get_global_mouse_position()
else:
picked = false
else:
if position.x - sprite.get_width()/2 < get_global_mouse_position().x and get_global_mouse_position().x < position.x + sprite.get_width()/2:
if position.y - sprite.get_height()/2 < get_global_mouse_position().y and get_global_mouse_position().y < position.y + sprite.get_height()/2:
picked = true
Move(delta)
func Move(delta):
var t_p_n = (t_p-position)
var t_p_n_y
var t_p_n_x
var t_p_n_y_m
var t_p_n_x_m
for i in range(0,int(MoveSpeed*delta)):
t_p_n_x = t_p_n.x/(abs(t_p_n.x)+abs(t_p_n.y))
t_p_n_y = t_p_n.y/(abs(t_p_n.x)+abs(t_p_n.y))
if t_p_n_x>=0.5:
t_p_n_x_m = 1
if t_p_n_x<0.5 and t_p_n_x>-0.5:
t_p_n_x_m = 0
else:
t_p_n_x_m = -1
if t_p_n_y>=0.5:
t_p_n_y_m = 1
if t_p_n_y<0.5 and t_p_n_y>-0.5:
t_p_n_y_m = 0
else:
t_p_n_y_m = -1
if sprite.get_pixel(position.x+t_p_n_x_m+3000,position.y+t_p_n_y_m+1500) != Color(153,217,234):
move_local_x(t_p_n_x_m,1)
move_local_y(t_p_n_y_m,1)
I know that I can use velocity and stuff, but I need to check the map which I have only a png of, so include that when you give advice.
So basicaly there are two problems firstly it doesn’t move when I use move local x and doesn’t even go to move_local_y and here is the proper version
extends CharacterBody2D
var picked = get("metadata/picked")
var empire = get("metadata/empire")
var t_p = Vector2()
var MoveSpeed = 100
var Move_mode = true
var sprite = Image.load_from_file("res://newmap.png")
func _ready():
t_p = position
if empire == null:
empire = 0
if picked == null:
picked = false
func _process(delta):
if Input.is_action_just_pressed("left_click"):
var t_g = get_global_mouse_position()
if picked == true:
if Move_mode == true:
t_p = get_global_mouse_position()
else:
picked = false
else:
if position.x - sprite.get_width()/2 < get_global_mouse_position().x and get_global_mouse_position().x < position.x + sprite.get_width()/2:
if position.y - sprite.get_height()/2 < get_global_mouse_position().y and get_global_mouse_position().y < position.y + sprite.get_height()/2:
picked = true
Move(delta)
func Move(delta):
var t_p_n = (t_p-position)
var t_p_n_y
var t_p_n_x
var t_p_n_y_m
var t_p_n_x_m
for i in range(0,int(MoveSpeed*delta)):
t_p_n_x = t_p_n.x/(abs(t_p_n.x)+abs(t_p_n.y))
t_p_n_y = t_p_n.y/(abs(t_p_n.x)+abs(t_p_n.y))
if t_p_n_x>=0.5:
t_p_n_x_m = 1
if t_p_n_x<0.5 and t_p_n_x>-0.5:
t_p_n_x_m = 0
else:
t_p_n_x_m = -1
if t_p_n_y>=0.5:
t_p_n_y_m = 1
if t_p_n_y<0.5 and t_p_n_y>-0.5:
t_p_n_y_m = 0
else:
t_p_n_y_m = -1
if sprite.get_pixel(position.x+t_p_n_x_m+3000,position.y+t_p_n_y_m+1500) != Color(153,217,234):
move_local_x(t_p_n_x_m,1)
move_local_y(t_p_n_y_m,1)
Move local functions are a little strange. As they take the basis/scale of the node2d in question. And since you pass 1 (which equals true), it tries to scale the movement. So if your node2d is scaled in anyway it will scale the movement.