Skip to main content
gamedev.net
🔒 Locked

Lua arithmetic

Started by The_Game May 21, 2011 at 8:00 PM 0 replies 1.8k views
Original Post
The_Game
The_Game
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:


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)


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?
Nanoha
Nanoha
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
Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.