Hello there! From what I can see there it looks like you're doing a createSprite() and setSpriteImage() in the main game loop. This is causing the sprites to be overwritten and recreated each frame. We only want to do those two things once, outside of the loop in our setup (or, if you are generating enemies as the game continues, you will need this to be in a function which is called at certain intervals in time.
To get a bunch of rocks floating around the screen and bouncing off the walls, see below (although there are a bunch of things you might want to change here):
// load image, set scale and size global variables (very useful throughout)
img = loadImage("Untied Games/Asteroid A")
scale = 2
rsize = tileSize(img, 0) * scale
// create the array to store our rocks, each one is a structure comprising of a sprite handle, a position vector and a direction vector
array rocks[20] = [
.spr = -1, // temporary value to be overwritten with createSprite()
.pos = {},
.dir = {},
.active = true
]
// array of direction vectors, makes it easy to choose a random one - this could be much more varied,
// or you could use random() to calculate a really random direction
dirs = [
{1, 1},
{-1, -1},
{-1, 1},
{1, -1}
]
// for loop which populates the rocks array with data
for i = 0 to len(rocks) loop
rocks[i].spr = createSprite() // set a blank sprite in the .spr property (the sprite handle) for each rock
setSpriteImage(rocks[i].spr, img) // set the sprite image to the sprite handle
setSpriteAnimation(rocks[i].spr, 0, 39, 10 ) // start tile of 0, end tile of 39, 10 fps
rocks[i].pos = random(gwidth() - rsize.x / 2) + rsize.x, random(gheight() - rsize.y * 2) + rsize.y} // choose a start position at a random point on screen with a border to stop rocks being slightly off-screen
rocks[i].dir = dirs[random(len(dirs))] // choose a random direction from our dirs array
repeat
// main loop - we only want to draw the sprites and apply movement calculation etc here.
// now the setup is done, we don't want to do any more sprite creation.
loop
clear()
updateSprites()
// for loop to count over each rock
for i = 0 to len(rocks) loop
if rocks[i].active then // only do these things if a rock is active
setSpriteScale(rocks[i].spr, {1, 1} * scale)
rocks[i].pos += rocks[i].dir * 0.8 // this multiplier will speed up/slow down the rock movement
// now our wall collision if statements, first the x axis:
if rocks[i].pos.x + rsize.x / 2 > gwidth() or
rocks[i].pos.x - rsize.x / 2< 0
then
rocks[i].dir.x = -rocks[i].dir.x // reverse the x direction for the rock
endif
// then the y:
if rocks[i].pos.y + rsize.y / 2 > gheight() or
rocks[i].pos.y - rsize.y / 2< 0
then
rocks[i].dir.y = -rocks[i].dir.y // reverse the y direction for the rock
endif
// finally, apply movement
setSpriteLocation(rocks[i].spr, rocks[i].pos)
endif
repeat
drawSprites()
update()
repeat
This should give you 20 animated rocks flying around the screen, albeit with very fixed directions. You could certainly improve that very easily! Let me know if I've been unclear.
I think the problem is primarily with having the setup stuff in the main loop.