Can someone explain, or provide the code to randomly spawn the other two asteroid images from the gameObjects sheet? I figured out how to get them to load individually, but I would like them all to load on the screen at the same time.
Hi @kswalker,
It’s pretty easy. In the “createAsteroid()” function, you create new asteroids. The 3rd parameter to the “display.newImageRect()” call is the number of the frame from the sheet, and the 4th and 5th parameters are the width and height of that frame. So, you just need to randomize the selection with variables, having it pick 1, 2, or 3 for the frame, and also store the proper width/height for the chosen asteroid (in local variables) based on which one is randomly chosen (notice that the asteroid frames aren’t exactly the same size). Once you have those three bits of information stored, you can just pass them to the “display.newImageRect()” call and everything should work.
Take care,
Brent
I’m at a loss… Would it look something like this?
local function createAsteroid ()
local newAsteroid = math.random.random (3)
local newAsteroid = display.newImageRect (mainGroup, objectSheet, 1, 102, 85 )
local newAsteroid = display.newImageRect (mainGroup, objectSheet, 2, 190, 83 )
local newAsteroid = display.newImageRect (mainGroup, objectSheet, 3, 100, 97 )
You are almost there… (I have never seen the tutorial just explaining some basics). The code should make sense
local function createAsteroid() --set a random size local size = math.random(70, 100) --make a random asteroid local aNewAsteroid = display.newImageRect (mainGroup, objectSheet, math.random(1,3), size, size) --return it return aNewAsteroid end local myNewAsteroid1 = createAsteroid() local myNewAsteroid2 = createAsteroid() local myNewAsteroid3 = createAsteroid()
Hi @kswalker,
It’s pretty easy. In the “createAsteroid()” function, you create new asteroids. The 3rd parameter to the “display.newImageRect()” call is the number of the frame from the sheet, and the 4th and 5th parameters are the width and height of that frame. So, you just need to randomize the selection with variables, having it pick 1, 2, or 3 for the frame, and also store the proper width/height for the chosen asteroid (in local variables) based on which one is randomly chosen (notice that the asteroid frames aren’t exactly the same size). Once you have those three bits of information stored, you can just pass them to the “display.newImageRect()” call and everything should work.
Take care,
Brent
I’m at a loss… Would it look something like this?
local function createAsteroid ()
local newAsteroid = math.random.random (3)
local newAsteroid = display.newImageRect (mainGroup, objectSheet, 1, 102, 85 )
local newAsteroid = display.newImageRect (mainGroup, objectSheet, 2, 190, 83 )
local newAsteroid = display.newImageRect (mainGroup, objectSheet, 3, 100, 97 )
You are almost there… (I have never seen the tutorial just explaining some basics). The code should make sense
local function createAsteroid() --set a random size local size = math.random(70, 100) --make a random asteroid local aNewAsteroid = display.newImageRect (mainGroup, objectSheet, math.random(1,3), size, size) --return it return aNewAsteroid end local myNewAsteroid1 = createAsteroid() local myNewAsteroid2 = createAsteroid() local myNewAsteroid3 = createAsteroid()