Lua is awesome because it has the ability to call a function inside itself. This is called recursion and is useful in instances where are large task can be broken down by repeatedly doing the same smaller task. This is seen in fractals and maybe FOR LOOPS!
So here's how you can use recursion to do a manual for loop.
Pretty sweet right?
All we do is pass on the original number and the if statement is there in order to prevent the function from counting endlessly upward. See if you can think of another situation this would be useful. Also, if you're curious for more, check out the wikipedia article because I've explained just about all I know about recursion.
Appa Juice Apps
Thursday, July 4, 2013
Friday, June 14, 2013
Moving an Object at a Constant Rate Corona SDK
This is a pretty annoying issue with the transition.to method that Corona SDK has provided. The issue, is that with transition.to you set a speed. A normal call to it might look like this.
transition.to (hero, {time = 500, x = event.x, y = event.y})
Now no matter where you tap, it will take a half second to get to the given point. If you click across the screen, it will speed across. Or if you click 5 pixels over, it will crawl toward that point.
So what if you want our hero to move at the same speed no matter where it's going?
The answer is an equation for the distance between two points.
So that's it.You calculate the distance between the position of your hero, and the position you want to move it to, and that is it's speed. Note, that I multiply our result by 2 in order to slow down the hero movement. Feel free to change the 2 to whatever you need. I can imagine a game with enemies of varying speed and this could come handy.
Example:
Original point = 0, 0
New point = 100,100
math.sqrt((0-100)^2+(0-100)^2) = 141
Therefore, the hero will move 141 pixels in 141 ms.
Not too difficult, but for someone who hates math, this is a pain.
transition.to (hero, {time = 500, x = event.x, y = event.y})
Now no matter where you tap, it will take a half second to get to the given point. If you click across the screen, it will speed across. Or if you click 5 pixels over, it will crawl toward that point.
So what if you want our hero to move at the same speed no matter where it's going?
The answer is an equation for the distance between two points.
So that's it.You calculate the distance between the position of your hero, and the position you want to move it to, and that is it's speed. Note, that I multiply our result by 2 in order to slow down the hero movement. Feel free to change the 2 to whatever you need. I can imagine a game with enemies of varying speed and this could come handy.
Example:
Original point = 0, 0
New point = 100,100
math.sqrt((0-100)^2+(0-100)^2) = 141
Therefore, the hero will move 141 pixels in 141 ms.
Not too difficult, but for someone who hates math, this is a pain.
Monday, June 10, 2013
The App Store Battle
I'm a little torn between app stores. My experience with the app stores has been an interesting one. I like Android more than I like iPhone for the one simple reason. Freedom. However, Apple has the apps. Here are the pros of each store.
-----Apple
-100x the downloads
-10x the profits
-More active users in Marketplace
-Quality control
------Google Play
-Ease of use
-Freedom
So there's my predicament. The apple app store kicks the Google play stores but, except for those two huge things. Freedom, and ease of use. There's also one more thing. Apple has a few devices. Android has HUNDREDS. Testing on that is very difficult. Therefore, for programmers it is so much more beneficial to program toward the Apple app store. However, I hope the market swings the other way. Freedom rocks.
-----Apple
-100x the downloads
-10x the profits
-More active users in Marketplace
-Quality control
------Google Play
-Ease of use
-Freedom
So there's my predicament. The apple app store kicks the Google play stores but, except for those two huge things. Freedom, and ease of use. There's also one more thing. Apple has a few devices. Android has HUNDREDS. Testing on that is very difficult. Therefore, for programmers it is so much more beneficial to program toward the Apple app store. However, I hope the market swings the other way. Freedom rocks.
Thursday, June 6, 2013
Random Movement Corona SDK
Want to know how to program some random movement?
--Innermost and outermost points for display.
local xMin = display.screenOriginX
local yMin = display.screenOriginY
local xMax = display.contentWidth - display.screenOriginX
local yMax = display.contentHeight - display.screenOriginY
--Pre-declare math function for SPEED
local mRand = math.random
--Pre-declare the points that will be generated each time move() is called
local newX
local newY
--Create our object that will fly around
local dot = display.newCircle(50,50,10)
--Function to move
local function move()
--Create new Random point to move to inside screen limits
newX = mRand(xMin,xMax)
newY = mRand(yMin,yMax)
--transition the dot to those points
transition.to(dot, {x = newX, y = newY})
end
--move dot every half second infinitely
timer.performWithDelay(500, move, 0)
So let's talk about what's going on here. We create a circle and tell it to move to a random point. We also delay it 500ms so we can see it move. Then with the 0 that means move() will be called infinitely with that delay. This could be useful for any sort of game that needs an easy AI.
Also note that by using the points around the edges of the screen, this program will work perfectly on every device.
--Innermost and outermost points for display.
local xMin = display.screenOriginX
local yMin = display.screenOriginY
local xMax = display.contentWidth - display.screenOriginX
local yMax = display.contentHeight - display.screenOriginY
--Pre-declare math function for SPEED
local mRand = math.random
--Pre-declare the points that will be generated each time move() is called
local newX
local newY
--Create our object that will fly around
local dot = display.newCircle(50,50,10)
--Function to move
local function move()
--Create new Random point to move to inside screen limits
newX = mRand(xMin,xMax)
newY = mRand(yMin,yMax)
--transition the dot to those points
transition.to(dot, {x = newX, y = newY})
end
--move dot every half second infinitely
timer.performWithDelay(500, move, 0)
So let's talk about what's going on here. We create a circle and tell it to move to a random point. We also delay it 500ms so we can see it move. Then with the 0 that means move() will be called infinitely with that delay. This could be useful for any sort of game that needs an easy AI.
Also note that by using the points around the edges of the screen, this program will work perfectly on every device.
Hello World
Hello World,
Colt Here.
I am a Lua programmer(scripter?) and use Corona SDK specifically. I'm planning for this blog to be a place to keep a portfolio of work, tutorials, and general thoughts. However, it will probably go in a completely different direction.
Appa Juice Apps is the name I chose just because I like the sound of it. I regret it immediately but I'm still chuckling at the quaint charm. So its staying for name.
Appa Juice Apps out.
Colt Here.
I am a Lua programmer(scripter?) and use Corona SDK specifically. I'm planning for this blog to be a place to keep a portfolio of work, tutorials, and general thoughts. However, it will probably go in a completely different direction.
Appa Juice Apps is the name I chose just because I like the sound of it. I regret it immediately but I'm still chuckling at the quaint charm. So its staying for name.
Appa Juice Apps out.
Subscribe to:
Comments (Atom)