|
#21
|
|||
|
|||
|
We'll here's some more sample work which is just basically a test haven for the functions. One odd, and something that I would consider bad, is the fact that after calling os.sleep and exiting the application(pressing the home button here) I am unable to go back to the application without restarting. I figured out that once it hits os.sleep() again at the next app run it just quits. To me it seems that I'm not cleaning up correctly upon exit. EDIT: When experiencing this the sudoku app seems to fix the issue and runs just fine...
While screwing around in trying to fix this I tried out os.exit();. Now this is supposed to exit the lua interpreter and return back to the main program. When I call it here, the whole thing just dies and I have to push the reset button. Does this mean the entire x-fi2 menu is coded in lua?! Bouncing ball, backlight, circle, comment/block comment, and beep usage: Code:
color_black = color.new(0,0,0);
color_white = color.new(255,255,255);
x = 30;
y = 30;
speed = 3;
fps_delay = 2;
ball_size = 10;
direction = 1;
direction_change = 0;
--[[
1 = Right + Down
2 = Right + Up
3 = Left + Down
4 = Left + Up
--]]
--http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html
function circlePoints(cx,cy,x,y,pixColor)
if (x == 0) then
screen.drawline(cx,cy,cx,cy + y,pixColor);
screen.drawline(cx,cy,cx,cy - y,pixColor);
screen.drawline(cx,cy,cx + y,cy,pixColor);
screen.drawline(cx,cy,cx - y,cy,pixColor);
elseif (x == y) then
screen.drawline(cx,cy,cx + x,cy + y,pixColor);
screen.drawline(cx,cy,cx - x,cy + y,pixColor);
screen.drawline(cx,cy,cx + x,cy - y,pixColor);
screen.drawline(cx,cy,cx - x,cy - y,pixColor);
elseif (x < y) then
screen.drawline(cx,cy,cx + x,cy + y,pixColor);
screen.drawline(cx,cy,cx - x,cy + y,pixColor);
screen.drawline(cx,cy,cx + x,cy - y,pixColor);
screen.drawline(cx,cy,cx - x,cy - y,pixColor);
screen.drawline(cx,cy,cx + y,cy + x,pixColor);
screen.drawline(cx,cy,cx - y,cy + x,pixColor);
screen.drawline(cx,cy,cx + y,cy - x,pixColor);
screen.drawline(cx,cy,cx - y,cy - x,pixColor);
end;
end;
function circleMidpoint(xCenter,yCenter,radius,circleColor)
local x = 0;
local y = radius;
local p = (5 - (radius * 4)) / 4;
circlePoints(xCenter,yCenter,x,y,circleColor);
while (x < y) do
x = x + 1;
if (p < 0) then
p = p + ((2 * x) + 1);
else
y = y - 1;
p = p + ((2 * (x - y)) + 1);
end;
circlePoints(xCenter,yCenter,x,y,circleColor);
end;
end;
screen.clear();
screen.fillrect(0,0,400,240,color_black);
screen.fillrect(0,0,20,240,color_white);
screen.fillrect(0,0,380,20,color_white);
screen.fillrect(380,0,20,240,color_white);
screen.fillrect(0,220,400,20,color_white);
while true do
circleMidpoint(x,y,ball_size,color_black);
if direction_change > 1 then
direction_change = direction_change - 1;
elseif direction_change == 1 then
audio.beep(500,100);
screen.backlight(50);
direction_change = 0;
end;
if direction == 1 then
if ((x + speed) > 368) then
direction = 3;
direction_change = 3;
end;
if ((y + speed) > 206) then
direction = 2;
direction_change = 3;
end;
x = x + speed;
y = y + speed;
elseif direction == 2 then
if ((x + speed) > 368) then
direction = 4;
direction_change = 3;
end;
if ((y - speed) < 33) then
direction = 1;
direction_change = 3;
end;
x = x + speed;
y = y - speed;
elseif direction == 3 then
if ((x - speed) < 33) then
direction = 2;
direction_change = 3;
end;
if ((y + speed) > 206) then
direction = 4;
direction_change = 3;
end;
x = x - speed;
y = y + speed;
elseif direction == 4 then
if ((x - speed) < 33) then
direction = 2;
direction_change = 2;
end;
if ((y - speed) < 33) then
direction = 3;
direction_change = 2;
end;
x = x - speed;
y = y - speed;
end;
circleMidpoint(x,y,ball_size,color_white);
screen.update();
if (control.read() == 1) and (button.home() == 1) then
break;
end;
os.sleep(fps_delay);
end;
Last edited by ThievingSix; 12-02-2009 at 10:56 PM. |
|
|
|||
|
|
|
#22
|
|||
|
|||
|
ThievingSix, thanks, that's cool! I was thinking of writing something similar, as I've got a pong app in mind.
And yes, I've run into the same issue with using the home button and then the app always doing a quick dump after starting. That's why I added a close tap to my last script. But yeah, it looks like a cleanup issue. And that os.exit() thing? Scary. |
|
#23
|
|||
|
|||
|
pong!! that would be great.... space invaders would be awesome if the firmware could support it...
nice work |
|
#24
|
|||
|
|||
|
The only problem I see with something like pong is that the touch screen isn't that good for delicate tracking like that.
|
|
#25
|
|||
|
|||
|
OK this dofile test works OK:
MAIN.LUA Code:
dofile("x.lua");
init();
X.LUA Code:
function init() blk = color.new(0,0,0); screen.clear(); screen.fillrect(0,0,400,240,blk); screen.update(); os.sleep(100); end; |
|
#26
|
|||
|
|||
|
If I'm correct, I believe dofile() will actually execute X.LUA. In your case I believe you want to use loadfile() instead.
|
|
#27
|
|||
|
|||
|
Quote:
Also, a general note: it looks like functions MUST be defined in the code text before being called, as in: Code:
function x() blah end; x(); Code:
x(); function x() blah end; Last edited by rphunt2002; 12-04-2009 at 11:11 AM. |
|
#28
|
|||
|
|||
|
Oh wait, sorry, did you mean like as if the function was called? so that the follwing init(); line is redundant? That didn't occur to me at all. Hmm, I'll have to check. I'll report back how these things behave.Thanks.
|
|
#29
|
|||
|
|||
|
Yeh, LUA is brand new to me but I think I have grasp(This reminds me of mIRC scriping lol). Basically when we have a plain text lua file that hasn't been compiled dofile()/loadfile() will compile it and assign it to a function variable.
As such this: Code:
myfunc() = loadfile("x.lua");
dofile() will execute any non-function encased code. If I'm correct the entire shell that we use to browse music, videos, etc on our player is made in lua. If this is the case then when we click on our application it's calling dofile("MAIN.LUA"). |
|
#30
|
||||
|
||||
|
Hi
I found this thread over at Creative forum, thank you once again :-) for cracking this nut. I just found out that when using: (control.isButton() == 1) and (button.home() == 1) then break; to exit an application, then it can be run again and again. Or at least that's the behaviour i get from this little circle program. Click the screen to draw a random size and color expanding circle. Code:
function putpixel(x,y,pixelcolor)
i=1;
if ( x > 399 ) then i=0; end;
if ( x < 0 ) then i=0; end;
if ( y > 239 ) then i=0; end;
if ( y < 0 ) then i=0; end;
if ( i == 1 ) then
screen.drawpixel(x,y,pixelcolor);
end;
end;
-- modified source from http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html
function circle(xCenter,yCenter,radius,pxcolor)
r2 = radius * radius;
putpixel(xCenter, yCenter + radius,pxcolor );
putpixel(xCenter, yCenter - radius,pxcolor );
putpixel(xCenter + radius, yCenter,pxcolor );
putpixel(xCenter - radius, yCenter,pxcolor );
y = radius;
x = 1;
y = math.sqrt(r2 - 1) + 0.5;
while (x < y) do
putpixel(xCenter + x, yCenter + y,pxcolor );
putpixel(xCenter + x, yCenter - y,pxcolor );
putpixel(xCenter - x, yCenter + y,pxcolor );
putpixel(xCenter - x, yCenter - y,pxcolor );
putpixel(xCenter + y, yCenter + x,pxcolor );
putpixel(xCenter + y, yCenter - x,pxcolor );
putpixel(xCenter - y, yCenter + x,pxcolor );
putpixel(xCenter - y, yCenter - x,pxcolor );
x=x+1;
y = math.sqrt(r2 - x*x) + 0.5;
end;
if (x == y) then
putpixel(xCenter + x, yCenter + y,pxcolor );
putpixel(xCenter + x, yCenter - y,pxcolor );
putpixel(xCenter - x, yCenter + y,pxcolor );
putpixel(xCenter - x, yCenter - y,pxcolor );
end;
end;
function drawcircle(x, y)
RNDcolor_R = math.random(255);
RNDcolor_G = math.random(255);
RNDcolor_B = math.random(255);
RNDSize = math.random(300);
rndcolor = color.new(RNDcolor_R,RNDcolor_G,RNDcolor_B);
for i=2,RNDSize do
circle(x,y,i,rndcolor);
screen.update();
end;
end;
color_black = color.new(0,0,0);
screen.fillrect(0,0,400,240,color_black);
screen.update();
while true do
if (control.read() == 1) and (control.isTouch() == 1) then
x, y = touch.pos();
drawcircle(x, y);
end;
if (control.isButton() == 1) and (button.home() == 1) then
break;
end;
os.sleep(2);
end;
Last edited by Jan_DK; 12-04-2009 at 05:21 PM. |
|
#31
|
||||
|
||||
|
Try adding os.sleep(100) after the drawing process. I guess that should be okay.
|
|
#32
|
||||
|
||||
|
Thank you for the suggestion, but even os.sleep(500) don't work, it just increase the pause between the two circles.
Last edited by Jan_DK; 12-04-2009 at 06:05 PM. |
|
#33
|
|||
|
|||
|
Add this code in the code of waiting inputs, then worked well.
Quote:
is my english right?I'm Japanese and still learning english... Last edited by utubo_sk8er; 12-05-2009 at 04:54 AM. |
|
#34
|
||||
|
||||
|
Yep, utubo_sk8er is right. You have to test if touch.click() it true, otherwise it will draw circles even if you slide the finger over the display.
|
|
#35
|
|||
|
|||
|
OK...
dofile() will load and execute a file as lua code. loadfile() will allow loading a file as a function. To use dofile(): Code:
dofile("x.lua");
init();
Code:
function init(); text.draw(0,0,"hi"); end; Code:
init = loadfile("x.lua");
init();
Code:
text.draw(0,0,"hi"); Last edited by rphunt2002; 12-06-2009 at 09:26 AM. |
|
#36
|
|||
|
|||
|
Slight touch up of home button code:
Code:
if (control.read() == 1) and (control.isButton() == 1) and (button.home() == 1) and (button.up() == 1) then
break;
end;
EDIT: It's not perfect though, something is still missimg. Run your app, close with Home, run your app again, it's fine. But run your app, close with Home, run Sudoku, and Sudoku closes right off, but runs a second time. Last edited by rphunt2002; 12-06-2009 at 09:27 AM. |
|
#37
|
|||
|
|||
|
I just hope in the future firmware they switch to vertical view (like iTouch, S9) instead of horizontal...you need to hold the player with both your hands to make this horizontal angle work by the looks of it...so vertical would be heaps cool.
|
|
#38
|
|||
|
|||
|
Straight(almost) from the sudoku app itself:
Code:
if (control.read() == 1) and (control.isButton() == 1) and (button.home() == 1) and (button.click() == 1) then
break;
end;
Edit: Really disappointed that os.execute() is non-existent seeing as it's supposed to be a lua standard. Edit2: My guess as to why this fixes it is this: The control table accesses a queue. When pressing the home button we activate a down and up sent to the queue. First we see if there is anything to be read with control.read(). Next we see if the thing we read is a button. Then if it's the home button. Finally we remove the entire thing from the queue with button.click(). When we didn't add button.click() nothing got removed from the queue. When we added button.up() we only removed the first part. Last edited by ThievingSix; 12-06-2009 at 04:49 PM. |
|
#39
|
|||
|
|||
|
Quote:
|
|
#40
|
|||
|
|||
|
Quote:
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 12:43 AM.












is my english right?
Linear Mode
