random images?

I need to generate random images for my game.

I have a parallax scrolling background and I need lasers to generate randomly because I am making a game very similar to jet pack joyride. How is this done?

Not sure I understand what you mean but to get random images from your project files you can create a folder with all your photos in it listed as “photo1.png”, “photo2.png” etc. Basically have the same base but each is numbered (photo1, photo2). So now create a random number between 1 and however many photos you have. It’s kind of hard to explain so here’s the code:
[lua]

–set to however many photos there are

local numberOfPhotos = 6

–create a random number for which photo to get

local randomNumber = math.random(1, numberOfPhotos)

–display the image

local image = display.newImage(localGroup, “Images/photo”…randomNumber…".png", 0, 0)

[/lua]

To display images at a random location you can create a timer in the enterframe function and spawn an image in a random location every time it hits 0. You can then move them with physics. Here is the code:
[lua]

local spawnTime = 20

local spawnCooldown = spawnTime

local function spawnNewImage() 

    local imageHeight = 50 – change this to actual image height

    local imageWidth = 50 –  change to actuall image width

    local randomY = math.random(imageHeight, display.contentHeight - imageHeight ) – we use image height to make sure

                                                                                   – it doesn’t spawn off the screen

    local newImage = display.newImage( localGroup, “image.png”, display.contentWidth + imageWidth, randomY  )

    physics.addBody( newImage, “dynamic” )

    newImage:setLinearVelocity( -5, 0 ) – change speed here

end    

local function onEnterFrame()

    if (spawnCooldown > 0) then 

        spawnCooldown = spawnCooldown - 1

    elseif (spawnCooldown <= 0) then 

        spawnNewImage()

        spawnCooldown = spawnTime

    end

end

[/lua]

If there are any errors or complications I’m glad to help fix them :slight_smile:

Thanks for the help! I couldnt manage to get it to work. go download jet pack joyride. I am trying to spawn random images for the obstacles like in jet pack joyride there are yellow lasers I need to spawn images randomly like that. thanks!

What exactly didn’t work about the code? Were there any errors or was it not what you wanted? If there were errors could you post them?

they all spawned but not randomly. I want them to spawn and remove them selves

and I want them to spawn so many pixels to the right

Not sure I understand what you mean but to get random images from your project files you can create a folder with all your photos in it listed as “photo1.png”, “photo2.png” etc. Basically have the same base but each is numbered (photo1, photo2). So now create a random number between 1 and however many photos you have. It’s kind of hard to explain so here’s the code:
[lua]

–set to however many photos there are

local numberOfPhotos = 6

–create a random number for which photo to get

local randomNumber = math.random(1, numberOfPhotos)

–display the image

local image = display.newImage(localGroup, “Images/photo”…randomNumber…".png", 0, 0)

[/lua]

To display images at a random location you can create a timer in the enterframe function and spawn an image in a random location every time it hits 0. You can then move them with physics. Here is the code:
[lua]

local spawnTime = 20

local spawnCooldown = spawnTime

local function spawnNewImage() 

    local imageHeight = 50 – change this to actual image height

    local imageWidth = 50 –  change to actuall image width

    local randomY = math.random(imageHeight, display.contentHeight - imageHeight ) – we use image height to make sure

                                                                                   – it doesn’t spawn off the screen

    local newImage = display.newImage( localGroup, “image.png”, display.contentWidth + imageWidth, randomY  )

    physics.addBody( newImage, “dynamic” )

    newImage:setLinearVelocity( -5, 0 ) – change speed here

end    

local function onEnterFrame()

    if (spawnCooldown > 0) then 

        spawnCooldown = spawnCooldown - 1

    elseif (spawnCooldown <= 0) then 

        spawnNewImage()

        spawnCooldown = spawnTime

    end

end

[/lua]

If there are any errors or complications I’m glad to help fix them :slight_smile:

Thanks for the help! I couldnt manage to get it to work. go download jet pack joyride. I am trying to spawn random images for the obstacles like in jet pack joyride there are yellow lasers I need to spawn images randomly like that. thanks!

What exactly didn’t work about the code? Were there any errors or was it not what you wanted? If there were errors could you post them?

they all spawned but not randomly. I want them to spawn and remove them selves

and I want them to spawn so many pixels to the right

sorry for the interrupting , this problem is connected to my problem that can display the image in Y but the X is random can anyone how to do that ? my image is set in array . thank in advance sir’s … 

Sorry if this sounds harsh, but you guys really need to learn the basics before you start trying to replicate Jetpack Joyride etc.

Break the problem down into its parts and figure out how to solve each part by looking through the API or searching on google. You’ll need a basic grasp of tables, display groups, object properties, timers, loops, random numbers, enterFrame listeners for this sort of thing.

A basic framework could be:

All ‘obstacles’ and background objects will be added to a ‘camera’ display group which is moved a certain fraction of pixels left every frame. Depending on the speed you want this could be 0.01, 0.005, 0.0001, just experiment until it feels right.

The ‘player’ will also be added to the camera group, but will be moved the same number of pixels to the right every frame so it stays on the same relative position on the screen. 

Spawn obstacles off-screen by adding say 1000 (depending on your resolution set in config.lua) to the negative of your camera group’s X position, and setting your obstacle’s X position to this.

Add each object to a table that you can then loop through every second or so and check whether obstacles are now off screen to the left.

Store the value of the camera group’s X position as a property of the obstacle when it is spawned, so you can compare this against the current camera group X position. Because the camera is moving and not the object, this will tell you how far the obstacle has travelled. How far it has to travel before it moves off screen to the left is down to the dimensions you set in config.lua.

Each obstacle has a ‘probability’ of being spawned each frame. So if you wanted a laser spawned on average every second, you would give it a probability of 60 (based on 60 fps). For every five seconds it would be 300.

Every frame, generate a random number between 1 and the probability, and if it is 1, call the function to spawn the object.

sorry for the interrupting , this problem is connected to my problem that can display the image in Y but the X is random can anyone how to do that ? my image is set in array . thank in advance sir’s … 

Sorry if this sounds harsh, but you guys really need to learn the basics before you start trying to replicate Jetpack Joyride etc.

Break the problem down into its parts and figure out how to solve each part by looking through the API or searching on google. You’ll need a basic grasp of tables, display groups, object properties, timers, loops, random numbers, enterFrame listeners for this sort of thing.

A basic framework could be:

All ‘obstacles’ and background objects will be added to a ‘camera’ display group which is moved a certain fraction of pixels left every frame. Depending on the speed you want this could be 0.01, 0.005, 0.0001, just experiment until it feels right.

The ‘player’ will also be added to the camera group, but will be moved the same number of pixels to the right every frame so it stays on the same relative position on the screen. 

Spawn obstacles off-screen by adding say 1000 (depending on your resolution set in config.lua) to the negative of your camera group’s X position, and setting your obstacle’s X position to this.

Add each object to a table that you can then loop through every second or so and check whether obstacles are now off screen to the left.

Store the value of the camera group’s X position as a property of the obstacle when it is spawned, so you can compare this against the current camera group X position. Because the camera is moving and not the object, this will tell you how far the obstacle has travelled. How far it has to travel before it moves off screen to the left is down to the dimensions you set in config.lua.

Each obstacle has a ‘probability’ of being spawned each frame. So if you wanted a laser spawned on average every second, you would give it a probability of 60 (based on 60 fps). For every five seconds it would be 300.

Every frame, generate a random number between 1 and the probability, and if it is 1, call the function to spawn the object.