Sign in to follow this  
The_Game

Lua arithmetic

Recommended Posts

I'm not 100% sure if this is the right area to post this, but I'm trying to come up with an equation in Lua.

The problem is that I want a circle to appear randomly within a range of numbers. The idea being that it reappears elsewhere on screen randomly, but I want the whole button to be on screen. I don 't have any part of the image to be cropped off by the screen. Here's the relevant code that I came up with:

[code]
randomButton = display.newCircle(0, 0, 40)

-- Makes the circle red (R, G, B)
randomButton:setFillColor(200, 0, 0)

-- Gets the height and width of the screen on the current platform
local _H = display.contentHeight; local _W = display.contentWidth

-- Puts the randomButton at the center of the screen to start
randomButton.x = _W/2; randomButton.y = _H/2;

-- Sets a reference point point for the randomButton in the middle of the circle
randomButton:setReferencePoint(display.CenterReferencePoint)

randomButton:setFillColor (math.random(255) , math.random(255), math.random(255))
randomButton.x = math.random(_W) randomButton.y = math.random(_H)
[/code]

Now I've figured out that the most I want the width to be between 40 and 280. I also want to the height to be between 40 and 440. So what equation would I have to put in on the last line of code to get that result?

Share this post


Link to post
Share on other sites
The closest to the origin the button can be will be the circle radius. The furthest it can be is the screen size minus the radius.

randomButton.x = math.random(circleRadius, _H - circleRadius)
randomButton.y = math.random(circleRadius, _V - circleRadius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this