D
D
Di Da2018-05-04 05:46:06
Algorithms
Di Da, 2018-05-04 05:46:06

How to make twinkling stars?

Someone please help with the code. That is, write me an algorithm please. For the stars to twinkle. I do this:
I create white circles (50 pieces) and throw them randomly across the screen. But how can I make them all flicker, not all at the same time, but randomly each separately? I would like an algorithm.
P.S. For flicker there is a function "fx.alpha"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alex_ak1, 2018-05-04
@alex_ak1

For each star, keep a counter.
Each frame the counter decreases.
If it is less than 5, the star increases by 5. If less than 10, by 10, if less than 15 - again by 5.
If less than 0 - the counter becomes equal to a random number (100) + 100. In
total, each star will increase 15 frames and this will be repeated for each after 100-200 frames.

A
alexalexes, 2018-05-04
@alexalexes

The most realistic option is to make a large prefabricated starry sky sprite in Photoshop and display the animation frame by frame.
https://docs.coronalabs.com/guide/media/spriteAnim...
If you analytically display stars, then it’s not enough just to scatter them randomly around the scene, you need to store the position and degree of glow of each star in a list or array and redraw the frame after the change any parameter.

Q
Qugurun, 2018-07-23
@Qugurun

local star = {}
for i=1,50 do
  star[i] = display.newCircle(math.random(0,display.contentWidth),math.random(0,display.contentHeight), math.random(1,3))
  star[i].anchorX =0.5
  star[i].anchorY =0.5
  star[i].time = math.random(3,20)
  star[i].animateUp = function ()
    star[i].x = math.random(0,display.contentWidth)	
    star[i].y = math.random(0,display.contentHeight)
    transition.to( star[i], { time= star[i].time * 100, alpha = 1, xScale = 1, yScale = 1, onComplete=star[i].animateDown } )
  end
  star[i].animateDown = function ()
    transition.to( star[i], { time= star[i].time * 500, alpha = 0, xScale = 0.01, yScale = 0.01, onComplete=star[i].animateUp } )
  end
  star[i].animateDown()
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question