![]() |
Common Code Fragments
Now that the SKD has been released and more people are getting into writing apps I thought it would be a good idea to have a thread devoted to useful reusable code functions that are not included in the standard Libraries.
I'll kick things off; :P Returns true if the point x,y is within the region at rx,ry with the width w and height h, else returns false: Code:
function withinRegion(x, y, rx, ry, w, h)Code:
function pointDistance(x,y,x1,y1) |
does zenlua support class structure?
can i say Quote:
var test= new one("test"); you'd get test1 if you went echo test.three() you get a result of 3 but if you went echo test.two() you'd get nothing or an error |
Nope - Lua doesn't support classes. You can try and emulate them but its still just functions :P (I'm doing this in the scrolling shooter to allow instances)
|
you mean like
function one() public function one( arg ) echo arg . 1; end; private function two() return 2; end; public function three( arg ) return two() + 1; end; end; ive done that in javascript how about private/public? does that work? |
You can't nest functions and there's no need for public/private stuff. There is just functions, that's it :P What you can do to emulate classes is to create a table which holds functions e.g. math.sqrt() is a function called sqrt in a table called math. This makes "math" like a class in other languages, but in Lua its just a table :P
|
Classes can be emulated as Modules. Private/Public is done via the local command. Check out Apeopex for an example of that.
|
TILED map viewer
1 Attachment(s)
I have implemented simple viewer of tile maps. I just wanted to test if it can be done on X-fi 2, and how much of memory i will waste by map.
Currently there is not culling of not visible tiles, but it is still pretty fluent with map slightly bigger than screen. Tiles are from TILED editor (free editor) If someone finds it usefull, go ahead and use/change code. |
Classes can best be simulated by metatables, e.g. something like this:
Code:
-- The class structure |
these may not be common, but they are probably helpful if you need them...
the distance between coordinates function can be shortened to Code:
function dist(x1,y1,x2,y2)Code:
function triarea(x1,y1,x2,y2,x3,y3)Code:
function pointintriangle(ax,ay,bx,by,cx,cy)Code:
function obsticalarea(xmin,xmax,ymin,ymax)http://forums.qj.net/psp-development...-snippets.html |
Quote:
|
A function to find out, whether the application is running on the Zen or the simulator, maybe good for debugging functions:
Code:
local function isZen()- region a with coords (aX, aY) and size (aW, aH) - region b with coords (bX, bY) and size (bW, bH) Code:
function intersects(aX, aY, aW, aH, bX, bY, bW, bH) |
Animation
A function for realtime animation:
Code:
--LeftAnim-Put a CurrentPlayerImg:draw(playerx,playery); in the draw screen function to make it update |
Want to compile your code like Creatives Sudoku game? If so read on:
Quote:
|
with the new firmware and everything, here is a script that creates a file "functions.lua" and lists all of the functions available.
Code:
local seen={} |
My internet is too slow to update right now, any new functions found with that dump function?
|
the accelerometer functions are the same as in the api. the only ones i have noticed are os.execute() and os.tmpname() i have not tested them yet though.
|
New functions as listed by cilmaviel;
Quote:
Quote:
Dump of output of cilmaviel's function, assess at your will; Code:
_G |
So accelerometer.set_senddatatype is now accelerometer.set_datatype. Same with the respective get().
I didn't notice os.execute() in the list? |
Quote:
|
Quote:
|
| All times are GMT -5. The time now is 10:28 AM. |