Related
Good day to everyone.
I want to create a "No Recoil" scenario with the ability to specify my own aiming sensitivity value.
At the moment, the code that you can see below works exclusively with one setting, namely with the dpi800 and the in-game sensitivity on the X and Y axes equal to 7, is it possible to make it so that I can change literally two digits, for example, not 7, but 9 or 10 and so on, and the script changed using some formula values in line 7(r_p["Weapon1"]) and 8(r_p["Weapon2"])
local r_p = {Weapon1}
local r_o = r_p[o_s_a] or {Weapon2}
local o_s_a = "Weapon2"
local n_o_s_a = {Weapon2 = "Weapon1", Weapon1 = "Weapon2"}
r_p["Weapon1"] = {0, 0, 0, 0, 25, 100, -1, 17, 600, -1, 20, 500, -1, 21, 800}
r_p["Weapon2"] = {0, 0, 0, 0, 22, 150, -1, 17, 400, -1, 20, 700, -1, 20, 300, -1, 20, 150, -2, 21, 400, -2, 21, 550, -2, 21, 300, -2, 21, 250, -2, 21, 100}
function Log()
if not IsKeyLockOn("scrolllock") then
ClearLog()
OutputLogMessage("Current mode: List of weapons | Scroll lock is OFF\n\n")
OutputLogMessage("Selected: %s\n\n", o_s_a)
OutputLogMessage(" (%s) | Weapon1 (%s) | Weapon2\n\n", o_s_a == "Weapon1", o_s_a == "Weapon2")
end
end
function OnEvent(event, arg)
EnablePrimaryMouseButtonEvents(true)
if event == "MOUSE_BUTTON_PRESSED" and arg == 5 and IsModifierPressed("lctrl") and not IsKeyLockOn("scrolllock") then
o_s_a = n_o_s_a[o_s_a]
r_o = r_p[o_s_a] or {}
Log()
else if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsMouseButtonPressed(3) and not IsKeyLockOn("capslock") then
for xy = 3, #r_o, 3 do
local c_t = GetRunningTime()
local h_r = r_o[xy-2]
local v_r = r_o[xy-1]
local r_d = r_o[xy]
repeat
local d_t = GetRunningTime() - c_t, r_d
MoveMouseRelative(h_r, v_r)
Sleep(10)
until d_t >= r_d or not IsMouseButtonPressed(1) or not IsMouseButtonPressed(3)
if not IsMouseButtonPressed(1) or not IsMouseButtonPressed(3) then break end
end
end
end
end
I tried using multiplayer, but in this version you can use only 2 values, where sensitivity 12 is standard, when you activate multiplayer, all your sensitivity settings are divided by 2 and as a result you get sensitivity 6, but this is a bit not the option that I need.
Thank you very much for any help if all.
You can modify the values on-the-fly instead of changing values in lines 7(r_p["Weapon1"]) and 8(r_p["Weapon2"]).
Replace the following lines
local h_r = r_o[xy-2]
local v_r = r_o[xy-1]
with
local h_r = round(r_o[xy-2] * sens / 6)
local v_r = round(r_o[xy-1] * sens / 6)
where sens is a variable with default value 6, but you can modify it.
round is a function, define it before OnEvent:
function round(x)
return math.floor(x + 0.5)
end
sens = 6
UPDATE:
The solution above has a drawback: due to rounding errors, the cursor gradually shifts to the left and/or to the top.
The solution below accumulates fractional parts to avoid such shift.
Define the function MoveMouseRelativeFractional which accepts fractional arguments and use it instead of the standard MoveMouseRelative.
The definition should be inserted at the very beginning of the whole script:
local remainder_fractional_x, remainder_fractional_y = 0, 0
local function MoveMouseRelativeFractional(x, y)
x = remainder_fractional_x + x
y = remainder_fractional_y + y
local x_int = math.floor(x + 0.5)
local y_int = math.floor(y + 0.5)
remainder_fractional_x = x - x_int
remainder_fractional_y = y - y_int
MoveMouseRelative(x_int, y_int)
end
Remove round:
local h_r = r_o[xy-2] * sens / 13
local v_r = r_o[xy-1] * sens / 13
Replace MoveMouseRelative with MoveMouseRelativeFractional:
MoveMouseRelativeFractional(h_r, v_r)
I wanted to create a simple game with my girlfriend, so I choose a lang that I don't even know. I like a challenge.
I am following this tutorial. (it's in portuguese bc it's my native idiom)
Well. I have no idea what's going on.
My code is that below. Thanks for everyone who can helps me <3
local anim = require 'anim8'
local image, inimation
local posX = 100
local direction = true
function love.load()
image = love.graphics.newImage('images/stickman-spritesheet.png')
local g = anim.newGrid(180, 340, image:getWidth(), image:getHeight())
animation = anim.newAnimation(g('1-9', 1, '1-9', 2, '1-9', 3, '1-9', 4, '1-9', 5, '1-9', 6, '1-9', 7, '1-7', 8), 0.01)
end
function love.update(dt)
if love.keyborad.isDown('left') then
posX = posX - 150 * dt
direction = false
animation:update(dt)
end
if love.keyborad.isDown('right') then
posX = posX + 150 * dt
direction = true
animation:update(dt)
end
end
function love.draw()
love.graphics.setBackgroundColor(255, 255, 255)
if direction then
animation:draw(image, posX, 50, 0, 1, 1, 90, 0)
elseif not direction then
animation:draw(image, posX, 50, 0, -1, -1, 90, 0)
end
end
There is no frame for x=5, y=1 happens when a frame is outside the provided image.
anim.newGrid(180, 340, image:getWidth(), image:getHeight()) means, that you have frames of size 180 by 340. Now if your image has the size 360 by 340 for example, you would have 2 by 1 frames.
In your case you expect a x of up to 9.
Double check the frame size and animation provided in g(...)
Read https://github.com/kikito/anim8 for more details.
--Okay, basically I've set up pretty much everything i want in the game. All i have to do now is create the enemies. I've set up the spawning and movement but when the player hits the enemies, nothing happens. When the red ball hits the blocks i'd like the client to close. (i will obviously change this at a later date but its only temporary) I'd be very grateful if you could program the code that would interpret this within the game. I'm new to this programming language and I'm only 15 still learning how to code. I am more than happy to give you credit in the game.
Thank you - Olee
function love.load()
love.graphics.setBackgroundColor( 110, 110, 110 ) --Sets background colour
love.graphics.rectangle("fill",400,300,0,230,230) --Draws game background
print("Olee's Game") -- prints into the console
x=140 --x position of sprite
y=320 --y position of the sprite
swidth=20 --sprite width
sheight=20 --sprite height
evil=900 --red rectangular blocks x position
evilheight=64 --red rectangular block height
evilwidth=256 --red rectangular block width
points=0 --point system created variable
random1y=math.random(120,480) --creates y position of the first enemy block
random2y=math.random(120,480) --creates y position of the block
random3y=math.random(120,480) --creates y position of the block
random4y=math.random(120,480) --creates y position of the block
random5y=math.random(120,480) --creates y position of the block
random6y=math.random(120,480) --creates y position of the block
end
function love.update(dt)
--BOUNDRIES--(makes the sprite not go off the page)
if y<120 then
y=y+20
end
if y>520 then
y=y-20
end
if x<20 then
x=x+20
end
if x>780 then
x=x-20
end
--AI (sends the blocks back to the start)
evil=evil-400*dt
if evil<=(-400) then
evil=1100
random1y=math.random(120,480)
random2y=math.random(120,480)
random3y=math.random(120,480)
random4y=math.random(120,480)
random5y=math.random(120,480)
random6y=math.random(120,480)
end
--Points
points = love.timer.getTime()
points=math.floor(points)
end
function love.focus(bool)
end
function love.keypressed( key, unicode )
print("You just pressed "..key)
print("x="..x.."\ny="..y.."\n#########")
if key=="escape"then
print("Bye!")
os.exit(0)
end
if key == "down" then
y=y+20
end
if key == "left" then --A KEY (LEFT)
x=x-20
end
if key == "right" then --D KEY (RIGHT)
x=x+20
end
if key == "up" then --W KEY (UP)
y=y-20
end
end
function love.draw()
--Floor
love.graphics.setColor(127, 127, 127)
love.graphics.rectangle("fill",0,540,5000,100)
--Ceiling
love.graphics.setColor(127, 127, 127)
love.graphics.rectangle("fill", 0, 0, 5000, 100)
--Welcome Message
love.graphics.setColor(191, 0, 52)
love.graphics.print("Bonjourno and Welcome to Olee's Game",32,32,0,1,1)
--Welcome Message HUD Box
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("line",16,18,284,48)
--Circle (sprite)
love.graphics.setColor(191, 0, 52)
love.graphics.circle("fill",x,y,swidth,sheight)
--SCOREBOARD
love.graphics.setColor(191, 0, 52)
love.graphics.print("Score: "..points.."",620, 35)
--Evil 1
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random1y,evilwidth,evilheight)
--Evil 2
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random2y,evilwidth,evilheight)
--Evil 3
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random3y,evilwidth,evilheight)
--Evil 4
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random4y,evilwidth,evilheight)
--Evil 5
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random5y,evilwidth,evilheight)
--Evil 6
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random6y,evilwidth,evilheight)
--FPS
love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 735, 5)
end
function love.quit()
end
Just to let you guys know, I HAVE set up a conf.lua file. My game works perfectly but i would like to add this! :)
and i have a play.bat
conf.lua:
function love.conf(t)
t.modules.joystick = true -- Enable the joystick module (boolean)
t.modules.audio = true -- Enable the audio module (boolean)
t.modules.keyboard = true -- Enable the keyboard module (boolean)
t.modules.event = true -- Enable the event module (boolean)
t.modules.image = true -- Enable the image module (boolean)
t.modules.graphics = true -- Enable the graphics module (boolean)
t.modules.timer = true -- Enable the timer module (boolean)
t.modules.mouse = true -- Enable the mouse module (boolean)
t.modules.sound = true -- Enable the sound module (boolean)
t.modules.timer = true -- Enable the timer module (boolean)
t.modules.thread = true
t.modules.math = true -- Enable the math module (boolean)
t.modules.physics = true -- Enable the physics module (boolean)
t.console = true -- Attach a console (boolean, Windows only)
t.title = "Olee's Game" -- The title of the window the game is in (string)
t.author = "Olee" -- The author of the game (string)
t.screen.fullscreen = false -- Enable fullscreen (boolean)
t.screen.vsync = false -- Enable vertical sync (boolean)
t.screen.fsaa = 0 -- The number of FSAA-buffers (number)
t.screen.height = 600 -- The window height (number)
t.screen.width = 800 -- The window width (number)
end
play.bat:
#ECHO OFF
start "" "C:\Program Files (x86)\LOVE\love.exe" .
Well, you have two approaches here:
You can do advanced collision
You can do simple collision.
The second is simpler, but the first is much better for this sort of game.
First approach:
First, you need to know this code:
function circleAndRectangleOverlap( circleX, circleY, circleRadius, rectangleX, rectangleY, rectangleWidth, rectangleHeight )
local distanceX = math.abs( circleX - rectangleX - rectangleWidth / 2 )
local distanceY = math.abs( circleY - rectangleY - rectangleHeight / 2 )
if distanceX > ( rectangleWidth / 2 + circleRadius ) or distanceY > ( rectangleHeight /2 + circleRadius ) then
return false
elseif distanceX <= ( rectangleWidth / 2 ) or distanceY <= ( rectangleHeight / 2 ) then
return true
end
return ( math.pow( distanceX - rectangleWidth / 2, 2 ) + math.pow( distanceY - rectangleHeight / 2, 2 ) ) <= math.pow( circleRadius, 2 )
end
Then you can add this to the rest of this code.
function love.load()
love.graphics.setBackgroundColor( 110, 110, 110 ) --Sets background colour
love.graphics.rectangle("fill",400,300,0,230,230) --Draws game background
print("Olee's Game") -- prints into the console
x=140 --x position of sprite
y=320 --y position of the sprite
sradius=20 --sprite radius
evil=900 --red rectangular blocks x position
evilheight=64 --red rectangular block height
evilwidth=256 --red rectangular block width
points=0 --point system created variable
random1y=math.random(120,480) --creates y position of the first enemy block
random2y=math.random(120,480) --creates y position of the block
random3y=math.random(120,480) --creates y position of the block
random4y=math.random(120,480) --creates y position of the block
random5y=math.random(120,480) --creates y position of the block
random6y=math.random(120,480) --creates y position of the block
end
function love.update(dt)
--BOUNDRIES--(makes the sprite not go off the page)
if y<120 then
y=y+20
end
if y>520 then
y=y-20
end
if x<20 then
x=x+20
end
if x>780 then
x=x-20
end
--AI (sends the blocks back to the start)
evil=evil-400*dt
if evil<=(-400) then
evil=1100
random1y=math.random(120,480)
random2y=math.random(120,480)
random3y=math.random(120,480)
random4y=math.random(120,480)
random5y=math.random(120,480)
random6y=math.random(120,480)
end
--Points
points = love.timer.getTime()
points=math.floor(points)
-- Check collisions
if circleAndRectangleOverlap( x, y, sradius, evil, random1y, evilwidth, evilheight )
or circleAndRectangleOverlap( x, y, sradius, evil, random2y, evilwidth, evilheight )
or circleAndRectangleOverlap( x, y, sradius, evil, random3y, evilwidth, evilheight )
or circleAndRectangleOverlap( x, y, sradius, evil, random4y, evilwidth, evilheight )
or circleAndRectangleOverlap( x, y, sradius, evil, random5y, evilwidth, evilheight )
or circleAndRectangleOverlap( x, y, sradius, evil, random6y, evilwidth, evilheight ) then
love.event.quit()
end
end
function love.focus(bool)
end
function love.keypressed( key, unicode )
print("You just pressed "..key)
print("x="..x.."\ny="..y.."\n#########")
if key=="escape"then
print("Bye!")
love.event.quit()
end
if key == "down" then
y=y+20
end
if key == "left" then --A KEY (LEFT)
x=x-20
end
if key == "right" then --D KEY (RIGHT)
x=x+20
end
if key == "up" then --W KEY (UP)
y=y-20
end
end
function love.draw()
--Floor
love.graphics.setColor(127, 127, 127)
love.graphics.rectangle("fill",0,540,5000,100)
--Ceiling
love.graphics.setColor(127, 127, 127)
love.graphics.rectangle("fill", 0, 0, 5000, 100)
--Welcome Message
love.graphics.setColor(191, 0, 52)
love.graphics.print("Bonjourno and Welcome to Olee's Game",32,32,0,1,1)
--Welcome Message HUD Box
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("line",16,18,284,48)
--Circle (sprite)
love.graphics.setColor(191, 0, 52)
love.graphics.circle("fill",x,y,sradius)
--SCOREBOARD
love.graphics.setColor(191, 0, 52)
love.graphics.print("Score: "..points.."",620, 35)
--Evil 1
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random1y,evilwidth,evilheight)
--Evil 2
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random2y,evilwidth,evilheight)
--Evil 3
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random3y,evilwidth,evilheight)
--Evil 4
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random4y,evilwidth,evilheight)
--Evil 5
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random5y,evilwidth,evilheight)
--Evil 6
love.graphics.setColor(191, 0, 52)
love.graphics.rectangle("fill",evil,random6y,evilwidth,evilheight)
--FPS
love.graphics.print("FPS: "..tostring(love.timer.getFPS( )), 735, 5)
end
function love.quit()
end
Second Approach
This uses the AABB aproach, which will be more useful if you use actual sprites in the future.
Just plug this in for the circleAndRectangleOverlap with this (and width and height arguments instead of radius):
function checkAABB( spriteX, spriteY, spriteWidth, spriteHeight, rectangleX, rectangleY, rectangleWidth, rectangleHeight )
if ( ( spriteX >= rectangleX + rectangleWidth)
or ( spriteX + spriteWidth <= rectangleX )
or ( spriteY >= rectangleY + rectangleHeight )
or ( spriteY + spriteHeight <= rectangleY ) ) then
return false
else return true
end
end
end
I have the following binary clock that I grabbed from this wiki article (the one that's for v1.5.*) for the awesome WM:
binClock = wibox.widget.base.make_widget()
binClock.radius = 1.5
binClock.shift = 1.8
binClock.farShift = 2
binClock.border = 1
binClock.lineWidth = 1
binClock.colorActive = beautiful.bg_focus
binClock.fit = function(binClock, width, height)
local size = math.min(width, height)
return 6 * 2 * binClock.radius + 5 * binClock.shift + 2 * binClock.farShift + 2 * binClock.border + 2 * binClock.border, size
end
binClock.draw = function(binClock, wibox, cr, width, height)
local curTime = os.date("*t")
local column = {}
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.hour), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.hour), 2, 2))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.min), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.min), 2, 2))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.sec), 1, 1))))
table.insert(column, string.format("%04d", binClock:dec_bin(string.sub(string.format("%02d", curTime.sec), 2, 2))))
local bigColumn = 0
for i = 0, 5 do
if math.floor(i / 2) > bigColumn then
bigColumn = bigColumn + 1
end
for j = 0, 3 do
if string.sub(column[i + 1], j + 1, j + 1) == "0" then
active = false
else
active = true
end
binClock:draw_point(cr, bigColumn, i, j, active)
end
end
end
binClock.dec_bin = function(binClock, inNum)
inNum = tonumber(inNum)
local base, enum, outNum, rem = 2, "01", "", 0
while inNum > (base - 1) do
inNum, rem = math.floor(inNum / base), math.fmod(inNum, base)
outNum = string.sub(enum, rem + 1, rem + 1) .. outNum
end
outNum = inNum .. outNum
return outNum
end
binClock.draw_point = function(binClock, cr, bigColumn, column, row, active)
cr:arc(binClock.border + column * (2 * binClock.radius + binClock.shift) + bigColumn * binClock.farShift + binClock.radius,
binClock.border + row * (2 * binClock.radius + binClock.shift) + binClock.radius, 2, 0, 2 * math.pi)
if active then
cr:set_source_rgba(0, 0.5, 0, 1)
else
cr:set_source_rgba(0.5, 0.5, 0.5, 1)
end
cr:fill()
end
binClocktimer = timer { timeout = 1 }
binClocktimer:connect_signal("timeout", function() binClock:emit_signal("widget::updated") end)
binClocktimer:start()
First, if something isn't by default already in Lua that's because this is to be used in the config file for awesome. :)
OK, so what I need is some guidance actually. I am not very familiar with Lua currently, so some guidance is all I ask so I can learn. :)
OK, so first, this code outputs a normal binary clock, but every column has 4 dots (44,44,44), instead of a 23,34,34 setup for the dots, as it would be in a normal binary clock. What's controlling that in this code? So that I can pay around with it.
Next, what controls the color? Right now it's gray background and quite a dark green, I want to brighten both of those up.
And what controls the smoothing? Right now it's outputting circles, would like to see what it's like for it to output squares instead.
That's all I need help with, if you can point me to the code and some documentation for what I need, that should be more than enough. :)
Also, if somebody would be nice enough to add some comments, that also would be awesome. Don't have to be very detailed comments, but at least to the point where it gives an idea of what each thing does. :)
EDIT:
Found what modifies the colors, so figured that out. None of the first variables control if it's a square or circle BTW. :)
The draw_point function draws the dots.
The two loops in the draw function are what create the output and is where the columns come from. To do a 23/34/34 layout you would need to modify the inner loop skip the first X points based on the counter of the outer loop I believe.
we are doing a game with moving objects around frame-by-frame and also using accelerometer.
We have hooked on two events - about drawing the frame and for the acc.
The problem is, after we receive the acc event, we immediately put the x value in a variable.
Then we use this variable to move an object on the screen, but there is CONSIDERABLE slow down. ( I turn the phone, and after a second the object is moving properly, but a second is just way too much for a game, I expect immediate response).
What am I doing wrong? Is there another workaround to do this, or can I give some params to the accelerometer?
Unfortunately this is a serious problem - a real blocker. If this does not work, I have to find another solution (not Corona) for implementing the game.
Thanks in advance!!!
Danail
PS: here's some source:
local lastXGravity = 0
local function move(event)
eventTime=event.time
elapsedTime = eventTime - lastDrawTime
lastDrawTime = eventTime
xSpeed = lastXGravity
local xMoved = xSpeed * elapsedTime
object.x= object.x + xMoved
end
function acc(event)
lastXGravity = event.xGravity
end
Runtime:addEventListener("accelerometer", acc)
Runtime:addEventListener( "enterFrame", move )
I don't know anything about Corona development, but there are some general issues. First what is gravity containing? Just the gravity vector or total acceleration = gravity + userAcceleration? You will need to get userAcceleration = totalAcceleration - gravity or some member from event providing it directly, otherwise there is no chance.
If you have user acceleration, you need to integrate twice to get the position. See Equations of motion. In your case the code will be like:
velocity = userAcceleration * elapsedTime
position = 0.5*userAcceleration * elapsedTime^2
In general precise position detection by accelerometer and gyroscope is still an unresolved problem, so don't expect precise results. But if you are interested in just evaluating that there is an impulse in one direction, it might work. See for example Getting displacement from accelerometer data with Core Motion
The guys at Ansca's forum just got this out:
system.setAccelerometerInterval( 50 )
This didn't quite actually did the trick, but
system.setAccelerometerInterval( 100 ) -- warning - battery drainer!!
did it :)
I open-sourced my first Corona SDK-made game (which actually did really well) which uses Tilting in the same manner you describe (the more the tilt, the faster the movement and vice-versa).
It's called 'Tilt Monster' and you can download it here:
http://developer.anscamobile.com/code/tilt-monster
local isSimulator = "simulator" == system.getInfo("environment")
-- Accelerator is not supported on Simulator
if isSimulator then
-- Please display an Alert Box
end
-- Text parameters
local labelx = 50
local x = 220
local y = 95
local fontSize = 24
local frameUpdate = false
local xglabel = display.newText( "gravity x = ", labelx, y, native.systemFont, fontSize )
xglabel:setTextColor(255,255,255)
local xg = display.newText( "0.0", x, y, native.systemFont, fontSize )
xg:setTextColor(255,255,255)
y = y + 25
local yglabel = display.newText( "gravity y = ", labelx, y, native.systemFont, fontSize )
local yg = display.newText( "0.0", x, y, native.systemFont, fontSize )
yglabel:setTextColor(255,255,255)
yg:setTextColor(255,255,255)
y = y + 25
local zglabel = display.newText( "gravity z = ", labelx, y, native.systemFont, fontSize )
local zg = display.newText( "0.0", x, y, native.systemFont, fontSize )
zglabel:setTextColor(255,255,255)
zg:setTextColor(255,255,255)
y = y + 50
local xilabel = display.newText( "instant x = ", labelx, y, native.systemFont, fontSize )
local xi = display.newText( "0.0", x, y, native.systemFont, fontSize )
xilabel:setTextColor(255,255,255)
xi:setTextColor(255,255,255)
y = y + 25
local yilabel = display.newText( "instant y = ", labelx, y, native.systemFont, fontSize )
local yi = display.newText( "0.0", x, y, native.systemFont, fontSize )
yilabel:setTextColor(255,255,255)
yi:setTextColor(255,255,255)
y = y + 25
local zilabel = display.newText( "instant z = ", labelx, y, native.systemFont, fontSize )
local zi = display.newText( "0.0", x, y, native.systemFont, fontSize )
zilabel:setTextColor(255,255,255)
zi:setTextColor(255,255,255)
-- Create a circle that moves with Accelerator events
local centerX = display.contentWidth / 2
local centerY = display.contentHeight / 2
Circle = display.newCircle(0, 0, 20)
Circle.x = centerX
Circle.y = centerY
Circle:setFillColor( 0, 0, 255 ) -- blue
local textMessage = function( str, location, scrTime, size, color, font )
local x, t
size = tonumber(size) or 24
color = color or {255, 255, 255}
font = font or "Helvetica"
if "string" == type(location) then
if "Top" == location then
x = display.contentHeight/4
elseif "Bottom" == location then
x = (display.contentHeight/4)*3
else
-- Assume middle location
x = display.contentHeight/2
end
else
-- Assume it's a number -- default to Middle if not
x = tonumber(location) or display.contentHeight/2
end
scrTime = (tonumber(scrTime) or 3) * 1000 -- default to 3 seconds (3000) if no time given
t = display.newText(str, 0, 0, font, size )
t.x = display.contentWidth/2
t.y = x
t:setTextColor( color[1], color[2], color[3] )
-- Time of 0 = keeps on screen forever (unless removed by calling routine)
if scrTime ~= 0 then
-- Function called after screen delay to fade out and remove text message object
local textMsgTimerEnd = function()
transition.to( t, {time = 500, alpha = 0},
function() t.removeSelf() end )
end
-- Keep the message on the screen for the specified time delay
timer.performWithDelay( scrTime, textMsgTimerEnd )
end
return t -- return our text object in case it's needed
end -- textMessage()
local function xyzFormat( obj, value)
obj.text = string.format( "%1.3f", value )
-- Exit if not time to update text color
if not frameUpdate then return end
if value < 0.0 then
-- Only update the text color if the value has changed
if obj.positive ~= false then
obj:setTextColor( 255, 0, 0 ) -- red if negative
obj.positive = false
print("[---]")
end
else
if obj.positive ~= true then
obj:setTextColor( 255, 255, 255) -- white if postive
obj.positive = true
print("+++")
end
end
end
local function onAccelerate( event )
xyzFormat( xg, event.xGravity)
xyzFormat( yg, event.yGravity)
xyzFormat( zg, event.zGravity)
xyzFormat( xi, event.xInstant)
xyzFormat( yi, event.yInstant)
xyzFormat( zi, event.zInstant)
frameUpdate = false -- update done
-- Move our object based on the accelerator values
Circle.x = centerX + (centerX * event.xGravity)
Circle.y = centerY + (centerY * event.yGravity * -1)
-- Display message and sound beep if Shake'n
if event.isShake == true then
-- str, location, scrTime, size, color, font
textMessage( "Shake!", 400, 3, 52, {255, 255, 0} )
end
end
local function onFrame()
frameUpdate = true
end
-- Add runtime listeners
Runtime:addEventListener ("accelerometer", onAccelerate);
Runtime:addEventListener ("enterFrame", onFrame);
I hope, this code will help you.