At the moment, I'm drawing the stars as single-point line graphics, and animating them with the move command. I'm not sure if that's the best approach, or if should be using tiny pixel images instead. Is animating many objects at once smoothly a limitation of LiveCode, or am I doing something completely wrong?
This is the code that I put in the stop/start button of the card:
Code: Select all
local sContinue
on mouseUp
if label of me is "Start" then
put true into sContinue
set title of button "startStop" to "Stop"
startSim
else if label of me is "Stop" then
put false into sContinue
set title of button "startStop" to "Start"
end if
end mouseUp
on startSim
local kMAX_STARS, kSTAR_SPEED
local tStarsA, tContinue
// Set constants
put 15 into kMAX_STARS
put 4 into kSTAR_SPEED
// Create stars array and star objects
put initStarsArray(kMAX_STARS, kSTAR_SPEED) into tStarsA
initStarObjects tStarsA
repeat while sContinue is true
animateStars tStarsA, kMAX_STARS, kSTAR_SPEED
end repeat
end startSim
function initStarsArray pMaxStars pStarSpeed
local tStar, tStarsA
// Generate attributes for the star
repeat with starNum = 1 to pMaxStars
put random(the width of this card) - 1 into tStar["x"]
put random(the height of this card) - 1 into tStar["y"]
put randomInRange(2,pStarSpeed) into tStar["speed"]
// Store the star in the array
put tStar into tStarsA[starNum]
end repeat
return tStarsA
end initStarsArray
command initStarObjects pStarsA
local tStarNum, tStarName, tColor
put 1 into tStarNum
lock screen
repeat for each element thisStar in pStarsA
put "star" & tStarNum into tStarName
// Create the star as a grapic object,
// and make sure we're not making duplicate objects.
if exists(graphic tStarName) is false then
create graphic tStarName
set the style of graphic tStarName to "polygon"
set the layerMode of graphic tStarName to "dynamic"
--set the antialiased of graphic tStarName to false
end if
// Set its points and size
set the points of graphic tStarName to thisStar["x"] & comma & thisStar["y"]
set the lineSize of graphic tStarName to thisStar["speed"]
# Set the star color acording it's speed.
# The slower the star, the darker should be its color.
if thisStar["speed"] is 2 then
put "#646464" into tColor
else if thisStar["speed"] is 3 then
put "#BEBEBE" into tColor
else if thisStar["speed"] is 4 then
put "#FFFFFF" into tColor
end if
set the foregroundColor of graphic tStarName to tColor
// Reference the next star
add 1 to tStarNum
end repeat
unlock screen
end initStarObjects
command animateStars @pStarsA pMaxStars pStarSpeed
local tStarName, tColor
lock screen
--set the lockMoves to true
repeat with thisStar = 1 to pMaxStars
put "star" & thisStar into tStarName
// 'Move' the star's position in the array.
subtract pStarsA[thisStar]["speed"] from pStarsA[thisStar]["y"]
// Update stars array.
// If the star hits the top of the screen
// then move it to the top of the screen with a random X coordinate.
if pStarsA[thisStar]["y"] < -5 then
put the height of this card into pStarsA[thisStar]["y"]
put random(the width of this card - 1) into pStarsA[thisStar]["x"]
put randomInRange(1,pStarSpeed) into pStarsA[thisStar]["speed"]
# Adjust the star color acording to it's speed.
# The slower the star, the darker should be its color.
if pStarsA[thisStar]["speed"] is 2 then
put "#646464" into tColor
else if pStarsA[thisStar]["speed"] is 3 then
put "#BEBEBE" into tColor
else if pStarsA[thisStar]["speed"] is 4 then
put "#FFFFFF" into tColor
end if
set the foregroundColor of graphic tStarName to tColor
set the lineSize of graphic tStarName to pStarsA[thisStar]["speed"]
end if
// Move the star object.
move graphic tStarName to pStarsA[thisStar]["x"] & comma & pStarsA[thisStar]["y"] in 0 ticks
end repeat
--set the lockMoves to false
unlock screen
end animateStars
command deleteStars
local tStarName
lock screen
repeat with thisStar = 1 to the number of graphics of this card
put "star" & thisStar into tStarName
delete graphic tStarName
end repeat
unlock screen
end deleteStars
function randomInRange lowerLimit upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange