admin管理员组文章数量:1024582
(ik i made a post similar to this, its just that i decided to switch to godot so i can make games more easier than in flash since its outdated)
i made a character that the player can control and it moves via tiles and tilemap, i wanna make it so that the player can move forever when an arrow key is pressed (and moving to the direction of said arrow key) until it goes into a walkable tile thats touching a non-walkable tile
you can give me any hints on how to do so, heres the code:
extends Sprite2D
class_name PacMan
@export var move_speed: float = 1
@onready var tile_map: TileMap = $"../TileMap"
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var animation_player: AnimationPlayer = $Sprite2D/AnimationPlayer
var is_moving = false
func _physics_process(delta: float) -> void:
if is_moving == false:
return
if global_position == sprite_2d.global_position:
is_moving = false
return
sprite_2d.global_position = sprite_2d.global_position.move_toward(global_position, move_speed)
func _ready() -> void:
animation_player.play("chomping")
func _process(delta: float) -> void:
if is_moving:
return
if Input.is_action_pressed("up"):
rotation_degrees = 90
move(Vector2.UP)
is_moving = true
elif Input.is_action_pressed("down"):
rotation_degrees = 270
move(Vector2.DOWN)
is_moving = true
elif Input.is_action_pressed("left"):
rotation_degrees = 0
move(Vector2.LEFT)
is_moving = true
elif Input.is_action_pressed("right"):
rotation_degrees = 180
move(Vector2.RIGHT)
is_moving = true
func move(direction: Vector2):
var current_tile: Vector2i = tile_map.local_to_map(global_position)
var target_tile: Vector2i = Vector2i(
current_tile.x + direction.x,
current_tile.y + direction.y,
)
var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)
if tile_data.get_custom_data("walk_tiles") == false:
return
#Player movement
is_moving = true
global_position = tile_map.map_to_local(target_tile)
sprite_2d.global_position = tile_map.map_to_local(current_tile)
(ik i made a post similar to this, its just that i decided to switch to godot so i can make games more easier than in flash since its outdated)
i made a character that the player can control and it moves via tiles and tilemap, i wanna make it so that the player can move forever when an arrow key is pressed (and moving to the direction of said arrow key) until it goes into a walkable tile thats touching a non-walkable tile
you can give me any hints on how to do so, heres the code:
extends Sprite2D
class_name PacMan
@export var move_speed: float = 1
@onready var tile_map: TileMap = $"../TileMap"
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var animation_player: AnimationPlayer = $Sprite2D/AnimationPlayer
var is_moving = false
func _physics_process(delta: float) -> void:
if is_moving == false:
return
if global_position == sprite_2d.global_position:
is_moving = false
return
sprite_2d.global_position = sprite_2d.global_position.move_toward(global_position, move_speed)
func _ready() -> void:
animation_player.play("chomping")
func _process(delta: float) -> void:
if is_moving:
return
if Input.is_action_pressed("up"):
rotation_degrees = 90
move(Vector2.UP)
is_moving = true
elif Input.is_action_pressed("down"):
rotation_degrees = 270
move(Vector2.DOWN)
is_moving = true
elif Input.is_action_pressed("left"):
rotation_degrees = 0
move(Vector2.LEFT)
is_moving = true
elif Input.is_action_pressed("right"):
rotation_degrees = 180
move(Vector2.RIGHT)
is_moving = true
func move(direction: Vector2):
var current_tile: Vector2i = tile_map.local_to_map(global_position)
var target_tile: Vector2i = Vector2i(
current_tile.x + direction.x,
current_tile.y + direction.y,
)
var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)
if tile_data.get_custom_data("walk_tiles") == false:
return
#Player movement
is_moving = true
global_position = tile_map.map_to_local(target_tile)
sprite_2d.global_position = tile_map.map_to_local(current_tile)
Share
Improve this question
asked Nov 18, 2024 at 20:33
SPONGEBOB RED YT channelSPONGEBOB RED YT channel
234 bronze badges
1 Answer
Reset to default 0Multiple possible solutions. My recommendation would be to create a global variable to store the current direction of the player. Then, instead of calling move(direction), change the player_direction variable and make a call to a direction-agnostic move() function.
Example semi-pseudocode:
var player_direction = Vector2.ZERO
func process(delta):
if Input.is_action_pressed("up"):
player_direction = Vector2.UP
#etc. for the other directions
move()
func move():
#exactly as you have it, but using player_direction instead of direction
(ik i made a post similar to this, its just that i decided to switch to godot so i can make games more easier than in flash since its outdated)
i made a character that the player can control and it moves via tiles and tilemap, i wanna make it so that the player can move forever when an arrow key is pressed (and moving to the direction of said arrow key) until it goes into a walkable tile thats touching a non-walkable tile
you can give me any hints on how to do so, heres the code:
extends Sprite2D
class_name PacMan
@export var move_speed: float = 1
@onready var tile_map: TileMap = $"../TileMap"
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var animation_player: AnimationPlayer = $Sprite2D/AnimationPlayer
var is_moving = false
func _physics_process(delta: float) -> void:
if is_moving == false:
return
if global_position == sprite_2d.global_position:
is_moving = false
return
sprite_2d.global_position = sprite_2d.global_position.move_toward(global_position, move_speed)
func _ready() -> void:
animation_player.play("chomping")
func _process(delta: float) -> void:
if is_moving:
return
if Input.is_action_pressed("up"):
rotation_degrees = 90
move(Vector2.UP)
is_moving = true
elif Input.is_action_pressed("down"):
rotation_degrees = 270
move(Vector2.DOWN)
is_moving = true
elif Input.is_action_pressed("left"):
rotation_degrees = 0
move(Vector2.LEFT)
is_moving = true
elif Input.is_action_pressed("right"):
rotation_degrees = 180
move(Vector2.RIGHT)
is_moving = true
func move(direction: Vector2):
var current_tile: Vector2i = tile_map.local_to_map(global_position)
var target_tile: Vector2i = Vector2i(
current_tile.x + direction.x,
current_tile.y + direction.y,
)
var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)
if tile_data.get_custom_data("walk_tiles") == false:
return
#Player movement
is_moving = true
global_position = tile_map.map_to_local(target_tile)
sprite_2d.global_position = tile_map.map_to_local(current_tile)
(ik i made a post similar to this, its just that i decided to switch to godot so i can make games more easier than in flash since its outdated)
i made a character that the player can control and it moves via tiles and tilemap, i wanna make it so that the player can move forever when an arrow key is pressed (and moving to the direction of said arrow key) until it goes into a walkable tile thats touching a non-walkable tile
you can give me any hints on how to do so, heres the code:
extends Sprite2D
class_name PacMan
@export var move_speed: float = 1
@onready var tile_map: TileMap = $"../TileMap"
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var animation_player: AnimationPlayer = $Sprite2D/AnimationPlayer
var is_moving = false
func _physics_process(delta: float) -> void:
if is_moving == false:
return
if global_position == sprite_2d.global_position:
is_moving = false
return
sprite_2d.global_position = sprite_2d.global_position.move_toward(global_position, move_speed)
func _ready() -> void:
animation_player.play("chomping")
func _process(delta: float) -> void:
if is_moving:
return
if Input.is_action_pressed("up"):
rotation_degrees = 90
move(Vector2.UP)
is_moving = true
elif Input.is_action_pressed("down"):
rotation_degrees = 270
move(Vector2.DOWN)
is_moving = true
elif Input.is_action_pressed("left"):
rotation_degrees = 0
move(Vector2.LEFT)
is_moving = true
elif Input.is_action_pressed("right"):
rotation_degrees = 180
move(Vector2.RIGHT)
is_moving = true
func move(direction: Vector2):
var current_tile: Vector2i = tile_map.local_to_map(global_position)
var target_tile: Vector2i = Vector2i(
current_tile.x + direction.x,
current_tile.y + direction.y,
)
var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)
if tile_data.get_custom_data("walk_tiles") == false:
return
#Player movement
is_moving = true
global_position = tile_map.map_to_local(target_tile)
sprite_2d.global_position = tile_map.map_to_local(current_tile)
Share
Improve this question
asked Nov 18, 2024 at 20:33
SPONGEBOB RED YT channelSPONGEBOB RED YT channel
234 bronze badges
1 Answer
Reset to default 0Multiple possible solutions. My recommendation would be to create a global variable to store the current direction of the player. Then, instead of calling move(direction), change the player_direction variable and make a call to a direction-agnostic move() function.
Example semi-pseudocode:
var player_direction = Vector2.ZERO
func process(delta):
if Input.is_action_pressed("up"):
player_direction = Vector2.UP
#etc. for the other directions
move()
func move():
#exactly as you have it, but using player_direction instead of direction
本文标签:
版权声明:本文标题:gdscript - (GODOT 4.3) how to make it so that the player that moves via tilemap move forever when an arrow key is pressed? - Sta 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745595912a2158167.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论