I’m trying to write a game in which the players movement differs depending on whether or not the screen is tapped or held…how could I best accomplish this?
@Invad3rZIM I’m not sure if there is an easier way, but I think you could do something like this.
function touched(t)
If t.state == BEGAN then
timer = 0
timer = timer + 1
end
If t.state == ENDED and timer < 30 then
--If tapped do
else
--If held do
end
It’s important how it differs, but as Staples said using a timer should do it (a bit different to the example posted above) although try t.tapCount == 1, this might only occur if the touch is less than a certain time, but I’m not sure.
@Invad3rZIM You could also do it this way. The variable t1 contains the duration of the screen being touched. You can set a limit to determine what’s considered a tap or a hold.
function setup()
t1=0
end
function draw()
background(40,40,50)
fill(255)
text("touch screen then release for duration",WIDTH/2,HEIGHT-100)
text(t1,WIDTH/2,HEIGHT/2)
end
function touched(t)
if t.state==BEGAN then
tm=os.time()
t1=0
end
if t.state==ENDED then
t1=os.time()-tm
end
end
@invad3sZIM if you want you can see how i manage the various cases of touch actions in the library XFC i posted recently. The cases are check in the Xtouch tab. In his context you want to know onTap and onBegan/onFinished. In screen9 i show a ‘fire button’ that reAct to button being held, with onEnter and onLeave events.
Alright. I figured it out…basically I have a table of touches that runs like this…
function setup()
touches = {}
mode = TheStory()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(110, 110, 117, 255)
-- print(mode.player.ball.x)
mode:draw()
drawTouches()
-- Do your drawing here
end
function drawTouches()
--draws the current touches..used for debugging/making videos
for a,b in pairs(touches) do
b:draw()
end
end
function touched(t)
--adds a new touch to the touches table or modifies current empty index
if t.state == BEGAN then
local sentinel = 0
for a,b in pairs(touches) do
if b.id == 0 then
b.id = t.id
b.startingX = t.x
b.startingY = t.y
b.currentX = t.x
b.currentY = t.y
sentinel = 1
break
end
end
if sentinel == 0 then
table.insert(touches, Touch(t.id,t.x,t.y))
end
end
for a,b in pairs(touches) do
b:touched(t)
end
end
As my main touch function…then the touch class…
Touch = class()
function Touch:init(id, x, y)
-- you can accept and set parameters here
self.id = id
self.startingX = x
self.currentX = x
self.startingY = y
self.currentY = y
self.touchTimeStart = os.time()
self.deltaTouchTime = os.time() - self.touchTimeStart
end
function Touch:draw()
-- Codea does not automatically call this method
if self.id ~= 0 then
ellipse(self.startingX,self.startingY, 25)
end
end
function Touch:touched(t)
if t.id == self.id then
if t.state == BEGAN then
self.touchTimeStart = os.time()
end
mode:touched(t, self.touchTimeStart)
if t.state == ENDED then
self.id = 0
end
end
end
And then the touches are passed into another class “TheStory” which is the story mode for a game…the start time for the touch is passed as well, so ill process the timing (going off @dave1707 suggestion to use os.time()
Thanks guys
suggestions:
use elapsedtime instead of os.time.
reset touch also when t.state == CANCELLED
Why elapsed time?
Under what conditions would the touch be CANCELLED?
(Just trying to learn)
I think CANCELLED is when the ipad STEALS the touch(it is a multitouch gesture that switches app or something. I may be wrong though
I thought cancelled was when the app was closed or multitasked or your finger wnt off the screen.
@Jmv38 ElapsedTime isn’t reliable under certain circumstances. Run this to see an example. The first print is approx 2 sec after 120 draw() calls. The second print is the time of the loop in the 130th draw() call.
function setup()
count=0
end
function draw()
background(40,40,50)
count=count+1
if count==120 then
print("seconds from start to now",ElapsedTime)
end
if count==130 then
a=ElapsedTime -- start of loop
for z=1,10000000 do
x=math.sqrt(z)
end
b=ElapsedTime -- end of loop
print("seconds of loop time",b-a)
end
end
ok
@dave1707 If you process it one frame after the lag it works.
function setup()
count=0
end
function draw()
background(40,40,50)
count=count+1
if count==120 then
print("seconds from start to now",ElapsedTime)
end
if count==130 then
a=ElapsedTime -- start of loop
for z=1,10000000 do
x=math.sqrt(z)
end
end
if count==131 then
b=ElapsedTime -- end of loop
print("seconds of loop time",b-a)
end
end
You didn’t give any time for Codea to update, the ElapsedTime variable (and any other) only update once per frame.