math.random help! [resolved]

I first setup a global [lua]local rand = math.random[/lua]

But I am not getting how to control it so that its not off the screen

this is the code to make a single apple image appear in random places
[lua]function _apple.new()
local aApple = display.newImageRect(“apple.png”, 39, 44)
aApple.x = rand(780 - aApple.contentWidth)
aApple.y = rand(900 / 2, screenH - aApple.contentHeight)

–aApple.x = 100; aApple.y = 550
aApple.ID = “Item_” … counter
return aApple
end[/lua]

Can please help me keep the apples on the screen!! Thanks

Jake

[import]uid: 51459 topic_id: 16625 reply_id: 316625[/import]

try this: (assuming center reference points)

[lua]minX = aApple.contentWidth / 2
maxX = display.contentWidth - minX
minY = aApple.contentHeight / 2
maxY = display.contentHeight - minY

aApple.x = rand(minX, maxX)
aApple.y = rand(minY, maxY)[/lua]
[import]uid: 19626 topic_id: 16625 reply_id: 62079[/import]

Ok well thanks Rob but that makes them show up all over the screen how do I get it to where it only show up from yCord 300 and down so that its just on the bottom half of the screen without going off the screen? [import]uid: 51459 topic_id: 16625 reply_id: 62083[/import]

Thanks Rob!

I think I got it!!

Jake [import]uid: 51459 topic_id: 16625 reply_id: 62087[/import]

You didn’t ask that. Think about what your asking? How can I make it 300+ on the why? Look at my example.

What could you change minY to so that it is 300 + half the apple size?

[import]uid: 19626 topic_id: 16625 reply_id: 62090[/import]

change aApple.contentHeight to 300? [import]uid: 51459 topic_id: 16625 reply_id: 62094[/import]

this seems to work though… but some apples are a little off the screen

[lua]function _apple.new()
local aApple = display.newImageRect(“apple.png”, 39, 44)
aApple.x = rand(780 - aApple.contentWidth)
aApple.y = rand(900 / 2, screenH - aApple.contentHeight)
aApple.rotation = rand(360)
–aApple.x = 100; aApple.y = 550
aApple.ID = “Item_” … counter
return aApple
end[/lua] [import]uid: 51459 topic_id: 16625 reply_id: 62095[/import]

or change the / to a - and to 2 to 300? [import]uid: 51459 topic_id: 16625 reply_id: 62096[/import]

ok Rob, I am close but I just need to lower it about 200 pixels… How do I do that?

CODE:
[lua]function _apple.new()
local aApple = display.newImageRect(“apple.png”, 39, 44)

minX = 300 - aApple.contentHeight
maxX = display.contentWidth - minX
minY = 300 - aApple.contentHeight
maxY = display.contentHeight - minY

aApple.x = rand(minX, maxX)
aApple.y = rand(minY, maxY)

–aApple.x = rand(780 - aApple.contentWidth)
–aApple.y = rand(900 / 2, screenH - aApple.contentHeight)
aApple.rotation = rand(360)
aApple.ID = “Item_” … counter
return aApple
end[/lua] [import]uid: 51459 topic_id: 16625 reply_id: 62099[/import]

In that example, you are also forcing the minimumX to be 300 - the hight of the apple (should be width when dealing with X)

Also when you changed minY to be 300- aApple.contentHeight, you removed the ability to calculate maxY correctly since I was using minY to be half the height of the apple.

Try this instead:

[lua] minX = aApple.contentWidth
maxX = display.contentWidth - aApple.contentWidth
minY = 300 + aApple.contentHeight
maxY = display.contentHeight - aApple.contentHeight[/lua]

[import]uid: 19626 topic_id: 16625 reply_id: 62103[/import]

That looks good Rob!. How do I make them closer to the center now so that it appears that they have fallin from the tree I have placed there? :slight_smile:

[import]uid: 51459 topic_id: 16625 reply_id: 62110[/import]

Think about it. Your tree has a X position, its center, lets say on an ipad, that = 512 (1024 / 2). The leaves on the tree go out so far in each direction. Lets say the tree takes up half the screen (I haven’t seen your art, so I’m speculating). So the left edge of the tree is around say 256 and the right edge is around 768.

When apples fall from a tree, they bounce and roll a bit, so its logical they might be wider than the tree, so for fun lets just say your landing zone for the apples is x=400 to x=800.

You would simply change minX to 400 and maxX to 800 (or whatever numbers work for your actual tree) [import]uid: 19626 topic_id: 16625 reply_id: 62150[/import]

Ok! Yea that makes sense thanks Rob! [import]uid: 51459 topic_id: 16625 reply_id: 62152[/import]

This seem to work GREAT!

[lua]function _apple.new()
local aApple = display.newImageRect(“apple.png”, 39, 44)

minX = 250
maxX = 650
minY = 420 + aApple.contentHeight
maxY = display.contentHeight - aApple.contentHeight

aApple.x = rand(minX, maxX)
aApple.y = rand(minY, maxY)

–aApple.x = rand(780 - aApple.contentWidth)
–aApple.y = rand(900 / 2, screenH - aApple.contentHeight)
aApple.rotation = rand(360)
aApple.ID = “Item_” … counter
return aApple
end[/lua] [import]uid: 51459 topic_id: 16625 reply_id: 62154[/import]