Home > Net >  Godot: Animation only plays when screen is being resized
Godot: Animation only plays when screen is being resized

Time:11-30

My game starts with a splash screen of a starfield flying towards the player. The first frame draws fine, but subsequent frames only draw while the window is actively being resized.

I just migrated from Godot 3.5.1 to 4.0.beta5 and had to do a bit of code refactoring just to get it semi-functional.

extends Node2D

const WIDTH = 1920
const HEIGHT = 1080
const STAR_COUNT = 400
const PLANET_COUNT = 5

var splash_stars = []
var theta
var offsetz
var x
var dx
var y
var dy
var radius
var alpha
var planet_scale
var color

var rng


func _ready():
    rng = RandomNumberGenerator.new()
    rng.randomize()
    
    # initialize the stars animation
    var Star = load("res://stars.gd")
    
    for _i in range(STAR_COUNT):
        theta = rng.randi()60
        offsetz = rng.randi()%(HEIGHT -1)   1
        x = offsetz * cos(theta)   WIDTH / 2.0
        dx = 2.5 * cos(theta)
        y = offsetz * sin(theta)   HEIGHT / 2.0
        dy = 2.5 * sin(theta)
        radius = 100 / offsetz
        alpha = 0
        var star = Star.new(theta, offsetz, x, dx, y, dy, radius, alpha)
        splash_stars.append(star)


func update_star_coords():
    for splash_star in splash_stars:
        splash_star._dx = splash_star._dx * 1.005
        splash_star._dy = splash_star._dy * 1.005
        splash_star._x  = splash_star._dx
        splash_star._y  = splash_star._dy
        splash_star._radius  = .025
        splash_star._alpha  = .005
        
        if 0 > splash_star._x or splash_star._x > WIDTH or 0 > splash_star._y or splash_star._y > HEIGHT or splash_star._radius > 8:
            splash_star._theta = rng.randi()60
            splash_star._theta = deg_to_rad(splash_star._theta)
            splash_star._offset = rng.randi()%(HEIGHT - 1)   1
            splash_star._x = splash_star._offset * cos(splash_star._theta)   WIDTH / 2.0
            splash_star._dx = 2.5 * cos(splash_star._theta)
            splash_star._y = splash_star._offset * sin(splash_star._theta)   HEIGHT / 2.0
            splash_star._dy = 2.5 * sin(splash_star._theta)
            splash_star._radius = 100 / splash_star._offset
            if splash_star._radius < 1:
                splash_star._radius = 1
            splash_star._alpha = 0


func _draw():
    for splash_star in splash_stars:
        var center = Vector2(splash_star._x, splash_star._y)
        color = Color(1, 1, 0, splash_star._alpha)
        draw_circle(center, splash_star._radius, color)
    

func _process(delta):
    update_star_coords()

And the 'stars' class:

extends Node2D

var _theta
var _offset
var _x
var _dx
var _y
var _dy
var _radius
var _alpha


func _init(theta,offset,x,dx,y,dy,radius,alpha):
    self._theta = theta
    self._offset = offset
    self._x = x
    self._dx = dx
    self._y = y
    self._dy = dy
    self._radius = radius
    self._alpha = alpha

CodePudding user response:

You need to call update (queue_redraw in Godot 4.0) to tell Godot that it needs to call _draw again.

You can read about it in Custom drawing in 2D.

  • Related