Hit a snag with tutorial game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By llama

Hello, I am new to Godot and have been reading through the documentation. I am now trying to follow the tutorial to create a game and I have hit a snag fairly early on…

This is the tutorial I am following HERE

The problem I am having is with the animation and movement. When I add the part to stop the animation, he no longer moves. The animation will play and stop when I press the arrow but it does not move at all. If take our the else statement (else: $AnimatedSprite.stop()) it moves around but as expected the animation keeps going when you stop moving.

Am I overlooking something? I found a video of this tutorial that is done the exact same way and it works in the video.

Thanks!

func process(delta):

	var velocity = Vector2() # the player's movement vector

	if Input.isactionpressed("uiright"):
		velocity.x += 1
		
	if Input.isactionpressed("uileft"):
		velocity.x -= 1
		
	if Input.isactionpressed("uidown"):
		velocity.y += 1
		
	if Input.isactionpressed("ui_up"):
		velocity.y -= 1
		
	if velocity.length() > 0:
		velocity = velocity.normalized() * SPEED
		$AnimatedSprite.play()
		
	else:
		$AnimatedSprite.stop()
		position += velocity * delta
		position.x = clamp(position.x, 0, screensize.x)
		position.y = clamp(position.y, 0, screensize.y)

It’s hard to tell if your code is correct when the formatting is all messed up. Indent the lines here (or use the “{}” button in the post editor) to format as code.

# like this

kidscancode | 2018-05-31 05:05

:bust_in_silhouette: Reply From: kidscancode

That made it clear. Your indentation is wrong. This part:

if velocity.length() > 0:
    velocity = velocity.normalized() * SPEED
    $AnimatedSprite.play()

else:
    $AnimatedSprite.stop()
    position += velocity * delta
    position.x = clamp(position.x, 0, screensize.x)
    position.y = clamp(position.y, 0, screensize.y)

should instead look like this:

if velocity.length() > 0:
    velocity = velocity.normalized() * SPEED
    $AnimatedSprite.play()

else:
    $AnimatedSprite.stop()

position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)

The way you had it, the velocity is only applied when it’s zero.

I can’t believe I overlooked the indentations!

Thank You!

llama | 2018-05-31 23:34