<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with touch - Codea</title>
      <link>http://twolivesleft.com/Codea/Talk/discussions/tagged/touch/feed.rss</link>
      <pubDate>Sat, 25 May 13 07:19:48 -0400</pubDate>
         <description>Tagged with touch - Codea</description>
   <language>en-CA</language>
   <atom:link href="/Codea/Talk/discussions/taggedtouch/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Long-Press Event Detection</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/2806/long-press-event-detection</link>
      <pubDate>Sat, 18 May 2013 16:52:00 -0400</pubDate>
      <dc:creator>BrianH</dc:creator>
      <guid isPermaLink="false">2806@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hi all, my current project was in need of a way to detect when the user long-presses an object on the screen.  I didn't see much in the forums and wanted to share the following code in the hopes it saves others the time of recreating this functionality.  Please, if anyone has ANY suggestions on improving my code, please share!  I'm always interested in the way others solve similar problems.</p>

<p>In my code I break out the various touch states (BEGAN, MOVING, ENDED) which is why the touched() method forwards calls to one of the following:</p>

<pre><code>    touchesBegan(),  touchesMoved()  and   touchesEnded()
</code></pre>

<p>For my project I needed an onLongPress() method to be called every time the user touches and holds the screen for a given time.  My code uses a global 'longPressDuration' which by default is set to 1/2 a second (0.5).  This is the only variable that should be changed if you need longer or shorter long-press durations.  All of the variables that begin with 2 underscores '__' are used internally and should not be changed as they're used to maintain the state of the long-press event detection logic.</p>

<p>Basically the algorithm works like this:  when the user first touches the screen (touch.state == BEGAN), we store the current ElapsedTime so we can later check to see how long it's been since the user began touching the screen.  If the user moves their finger, then the long-press is cancelled.  Since the only method Codea calls regualrly is draw(), I placed all the logic for detecting a long press at the END of draw().  This code first checks to see if we've detected a touch BEGAN event.  If so, and we've yet to detect a long-press then we check the current time with the stored time when the touch BEGAN.  If this result is greater than the long-press hold time (longPressDuration) then we trigger the onLongPress() method.  You might notice the extra flags &amp; checking, these ensure that once the system has detected a long-press, it doesn't keep sending out the onLongPress() method on every draw() cycle.</p>

<p>Enough talk, here's the code.  Share &amp; enjoy</p>

<pre lang="lua">
--[[ 
  LongPress Event Detection

  Sample code to detect when a user long-presses/touches the screen.

  Since Codea doesn't provide timers or multiple threads, long-press
  detection is done in the only method that's called regularly, the
  'draw()' method.

  Also, Once a long-press has been detected, no more movement of the touch
  will be forward (touch.state == MOVING or ENDED).  Only after the user
  releases their finger from the screen will the long-press be cleared and
  touches will begin to be recognized again.

]]--



-- ----------------------------------------------------------------------------------
-- GLOBALs
-- ----------------------------------------------------------------------------------

-- how long does it take before a long-press event is triggered.
longPressDuration = 0.5


-- Sensitivity of the touch movement in points.
-- The number of points a touch can move to still allow a long-press.
-- users tend to 'jiggle' their fingers when long-pressing the screen,
-- this helps to make a long-press more accurate.
longPressSensitivity = 10




-- !! THE FOLLOWING ARE USED FOR STATE MANAGEMENT AND DO NOT NEED TO BE MODIFIED
-- touch start time
__longPressStartTime = 0

-- did we detect a touch.state == BEGAN?
__longPressStarted = false

-- has a long-press been detected?
__longPressDetected = false


-- store the touch BEGAN location
-- these are used to calculate the distance between
-- the touch start and the longPressSensitivity value
__longPressTouchX = 0
__longPressTouchY = 0




-- ----------------------------------------------------------------------------------
-- draw()
-- ----------------------------------------------------------------------------------
function draw()
    
    -- &lt;&lt;&lt; ADD YOUR DRAW CODE HERE &gt;&gt;&gt;
    
    
    -- The following code goes AFTER all drawing is completed.
    -- Basically, if we've detected a BEGAN touch event and
    -- a long-press hasn't been detected yet, check how long it's
    -- been since the touch started.  If it's longer than the long-press
    -- duration then trigger a long-press event.
    if __longPressStarted then
        if not __longPressDetected then
            if (ElapsedTime - __longPressStartTime) &gt; longPressDuration then
                __longPressDetected = true
                onLongPress()
            end
        end
    end
end



-- ----------------------------------------------------------------------------------
-- touched()
--
-- When the user touches the screen, this method processes the touch event
-- and routes the different touch types to their own method.  If a touch BEGAN
-- event is received, then setup a long-press/touch state.  If a MOVING touch
-- state is received, and the distance between the start of a long-press and
-- the current 'moving' touch is less than the long-press sensitivity then
-- ignore the MOVING touch, otherwise cancel the long-press and just trigger a
-- MOVING event. 

-- Once a long-press/touch has been detected, ALL future touch events are IGNORED
-- until the user removes their finger from the screen, which triggers an ENDED
-- touch state.  Once this happens, the long-press state is reset and touches
-- are enabled once again.
-- ----------------------------------------------------------------------------------
function touched(touch)
    if touch.state == BEGAN then
        __longPressStartTime = ElapsedTime
        __longPressStarted = true
        __longPressTouchX = touch.x
        __longPressTouchY = touch.y
        touchesBegan(touch)

    elseif touch.state == MOVING then
        if not __longPressDetected then
            -- has the touch moved outside of the long-press sensitivty range?
            if not distanceWithinRange(touch.x, touch.y, __longPressTouchX, __longPressTouchY, longPressSensitivity) then
                -- cancel the long-press state...
                __longPressStarted = false
                -- send a touchesMoved() event...
                touchesMoved(touch)
            end
        end    
    else
        -- This is for touch.state == ENDED 
        if not __longPressDetected then
            touchesEnded(touch)
        end
        
        -- clear the long-press flags
        __longPressDetected = false
        __longPressStarted = false
    end
end


-- ----------------------------------------------------------------------------------
-- distanceWithinRange()
--
-- Given 2 points calculate their distance for both X and Y.
-- If the distance of BOTH X and Y are less than distanceRange
-- then return 'true', otherwise return 'false'
--
-- Params:
--            p1x, p1y       First point to check
--            p2x, p2y       Second point to check
--            distanceRange  Minimum distance between p1 and p2 to check for
--
-- Return:
--            true        Distance between p1 AND p2 is less than or equal to'distanceRange'
--            false       Distance between p1 AND p2 is greater than 'distanceRange'
-- ----------------------------------------------------------------------------------
function distanceWithinRange(p1x, p1y, p2x, p2y, distanceRange)
    local result = false
    
    -- calculate the distance between p1 and p2
    local dx = math.abs(p2x-p1x)
    local dy = math.abs(p2y-p1y)
    
    -- if the distance between BOTH p1 and p2 is less than or equal todistanceRange
    -- then set result to 'true', otherwise result will be the default value
    -- of 'false'
    if dx </pre>
]]></description>
   </item>
   <item>
      <title>Codea Wiki tutorial #3 fix</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/2684/codea-wiki-tutorial-3-fix</link>
      <pubDate>Tue, 30 Apr 2013 14:33:02 -0400</pubDate>
      <dc:creator>evanlws12</dc:creator>
      <guid isPermaLink="false">2684@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I'm trying to make a simple game in codea and looking at the tutorial (under getting started) I've recreated the ball example that has the circle bounce around the screen. However, when the user flicks the ball near the edge of the screen the ball gets stuck to the side of the screen. Is there a way to fix this?</p>

<p>Here is the code from the tutorial that I need in my program:</p>

<pre><code>function setup()
    x = WIDTH/2
    y = HEIGHT/2
    bc = color(248, 250, 13, 255)
    tc = color(195, 17, 17, 255)
    c = bc
    inTouch = false
    dx = 0
    dy = 0
    r = 30
end

function draw()
    background(0, 0, 0, 255)
    fill(c)
    ellipse(x,y,r)
    if inTouch then
        x = tx
        y = ty
        c = tc
    else
        x = x +  dx
        y = y +  dy
        c = bc
    end
    if y &gt; HEIGHT - r then
        dy = - dy
    end
    if y &lt; r then
        dy = - dy
    end
    if x &gt; WIDTH - r then
        dx = - dx
    end
    if x &lt; r then
        dx = - dx
    end
end

function touched(touch)
    tx = touch.x
    ty = touch.y
    if touch.state == BEGAN then
        inTouch = true
        velocity = {}
    end
    if touch.state == MOVING then
        table.insert(velocity,1,vec2(touch.deltaX,touch.deltaY))
    end
    if touch.state == ENDED then
        dx = 0
        dy = 0
        n = 0
        for i = 1,10 do
            if velocity[i] then
                n = n + 1
            dx = dx + velocity[i].x
            dy = dy + velocity[i].y
            end
        end
        if n &gt;0 then
        dx = dx / n
        dy = dy / n
        end
        inTouch = false
    end
end
</code></pre>
]]></description>
   </item>
   <item>
      <title>An Exploration of tween</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/2686/an-exploration-of-tween</link>
      <pubDate>Tue, 30 Apr 2013 20:34:17 -0400</pubDate>
      <dc:creator>Brucewe</dc:creator>
      <guid isPermaLink="false">2686@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Four programs that examine tweens.  Code 12a - tween is a starting point, Code 12b - tween Experiments takes the investigation a little further, Code 12c - tween pause demonstrates pausing a tween with a touch and changing the destination with the same touch, Code 12d - tween path takes a look at using paths.
Code 12a: <a href="http://pastebin.com/TMXQqNgJ" target="_blank" rel="nofollow">http://pastebin.com/TMXQqNgJ</a>
Code 12b: <a href="http://pastebin.com/8qu81aSe" target="_blank" rel="nofollow">http://pastebin.com/8qu81aSe</a>
Code 12c: <a href="http://pastebin.com/kN4ermGM" target="_blank" rel="nofollow">http://pastebin.com/kN4ermGM</a>
Code 12d: <a href="http://pastebin.com/XvcR7YPb" target="_blank" rel="nofollow">http://pastebin.com/XvcR7YPb</a></p>
]]></description>
   </item>
   <item>
      <title>help with a simple info button to display new window or screen?</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/2425/help-with-a-simple-info-button-to-display-new-window-or-screen</link>
      <pubDate>Tue, 12 Mar 2013 19:23:06 -0400</pubDate>
      <dc:creator>jasl</dc:creator>
      <guid isPermaLink="false">2425@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I have been looking through the examples in Codea - specifically the 'Spritely' example because it uses an info button to display instructions - however, I am having difficulty reading through the various tabs trying to decipher and pick out the relevant code to help me do this. I must not be fully understanding the touch class with regard to this? I have tried examples and I get output that tells me the button has been activated.. (using a sprite as a button - thanks ChrisF!).. also that a single or double tap has occurred and swiping too but I just can't work out how to make the action bring up a window / new screen. I am new to coding in general so sorry in advance if this is a dumb question but is there anyone that could explain this in simple terms to me and help me out please?</p>
]]></description>
   </item>
   <item>
      <title>Touch events for mesh buttons</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/2293/touch-events-for-mesh-buttons</link>
      <pubDate>Mon, 25 Feb 2013 03:18:06 -0500</pubDate>
      <dc:creator>Bitsandnumbers</dc:creator>
      <guid isPermaLink="false">2293@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I'm totally new to Codea and until now I only learned markup languages. So I'm trying to get into lua and followed the tutorials in the wiki (wish there'd be more!). I'm trying to create a UI first, as I think it would be the most easy part. I fully understand the mesh concept, but am stuck with TouchEvents. Right now, I would like to initiate an action when pushing a button (actually rectangle meshes with texture).</p>

<p>Here's the code I've come up with:</p>

<hr />

<pre><code><br />supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
    --Set the viewer to fullscreen
    displayMode( FULLSCREEN )

    c1 = color(31, 57, 84, 255)
    c2 = color(36, 115, 196, 255)

    by = 50
    ty = 100
    iw = 120
    i2w = 100
    ih = 30

    buttonframe = Frame(865, 50, WIDTH - 5, HEIGHT - 55)

    topbar = mesh()
    id1 = topbar:addRect(WIDTH/2,HEIGHT-ty/2, WIDTH,ty)
    topbar:setRectColor(id1, 218, 75, 75, 255)

    bottombar = mesh()
    bottombar.vertices = {vec2(0,0), vec2(WIDTH,0), vec2(0,by), vec2(0,by), vec2(WIDTH,by), vec2(WIDTH,0)}
    bottombar.colors = {c1, c1, c2, c2, c2, c1}

    menu_img = readImage("Cargo Bot:Menu Button")
    icone = mesh()
    icone.texture = menu_img
    icone:addRect(iw/2+20, by/2, iw, ih)
    icone:setRectTex(1,0,0,1,1)

    exit_img = readImage("Cargo Bot:Clear Button")
    ico_exit = mesh()
    ico_exit.texture = exit_img
    ico_exit:addRect(WIDTH - i2w/2 - 20, by/2, i2w, ih)
    ico_exit:setRectTex(1,0,0,1,1)
end

function touched(touch)
    sound(SOUND_HIT, 33733)
    if ico_exit:touched(touch) then
        close()
    end
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    topbar:draw()
    bottombar:draw()
    icone:draw()
    ico_exit:draw()
end
</code></pre>

<hr />

<p>Obviously, TouchEvents are not that easy! I understood that there are two methods discussed on this forum: physics with collide events and touch events depending on coordinates. The later seem to be not so resource friendly as I understood, if there are a lot of tappable shapes/buttons.</p>

<p>I would like to know what you think would be the best practice in my situation to handle touch events. And am I in the right direction for the UI design (using meshes, etc...).</p>

<p>Thanks a lot :)</p>
]]></description>
   </item>
   <item>
      <title>Problem with the touch state</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1997/problem-with-the-touch-state</link>
      <pubDate>Tue, 18 Dec 2012 08:26:25 -0500</pubDate>
      <dc:creator>edwin809</dc:creator>
      <guid isPermaLink="false">1997@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I created a class for handling sprites the purpose is to choose symbols from a menu and drag them to positions on the screen. But it seems to not work all the time, or theres got to be a better way to do this.
here is the touch part of the class.</p>

<pre><code>    ScaledSprite = class()
    function ScaledSprite:init(mySprite,Ssize,x,y)
        self.pos = vec2(x,y)
        self.mySprite = mySprite
        self.Ssize = Ssize
        self.Sw,self.Sh = spriteSize(self.mySprite)
        self.action = nil
        self.selected = false
    end

    function ScaledSprite:draw()
        -- Codea does not automatically call this method
        factor = self.Ssize / 100
        sprite(self.mySprite,self.pos.x,self.pos.y,self.Sw * factor,self.Sh * factor)
    end

    function ScaledSprite:touched(touch)
        -- Codea does not automatically call this method
        if touch.state == MOVING then
           if (math.abs(self.pos.x - touch.x) &lt;= (self.Sw/2)
           and math.abs(self.pos.y - touch.y) &lt;= (self.Sh/2)) then
            if flying ~= "none" then
                self.selected = true
             else
                self.selected = false
            end
        elseif touch.state == ENDED then
            if self.selected == true and self.action then
                self.selected = false
                self.action()
            end
        end
    end
</code></pre>

<p>As I explained sometimes it gets the ENDED condition but sometimes it doesn't.</p>

<p>From Main this were I called the class action: (Fliying is a control variable to indicate that I hit symbol from the menu)</p>

<pre><code>    if (flying == "P1") then
        if CurrentTouch.y &gt; 680 then
            fixedy = 680
        elseif CurrentTouch.y &lt; 100 then
            fixedy = 100
        else
            fixedy = CurrentTouch.y   
        end

        ssP1 = ScaledSprite("Dropbox:symbol-P1",symbSize * vsmFactor,CurrentTouch.x,fixedy)
        ssP1.action = function() btnSymbPRO1pressed() end

        ssP1:draw()
        dragging = true     

    end
</code></pre>

<p>Action function that's is called after the touch ENDED is detected</p>

<pre><code>    function btnSymbPRO1pressed()
            iSymbol = iSymbol + 1
            flying = "none"
            dragging = false
            aSx[iSymbol] = CurrentTouch.x
            aSy[iSymbol] = CurrentTouch.y
            fw, fh = spriteSize("Dropbox:symbol-P1")
            aSw[iSymbol] = fw * (symbSize / 100) * vsmFactor
            aSh[iSymbol] = fh * (symbSize / 100) * vsmFactor
    end
</code></pre>
]]></description>
   </item>
   <item>
      <title>Keyboard quick keys show show sub-menu on tap begin, and buttons on tap ended.</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1830/keyboard-quick-keys-show-show-sub-menu-on-tap-begin-and-buttons-on-tap-ended</link>
      <pubDate>Sun, 21 Oct 2012 18:55:20 -0400</pubDate>
      <dc:creator>ljdp</dc:creator>
      <guid isPermaLink="false">1830@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I think it would feel a lot smoother if the keyboard quick keys, such as the one that pops up "[]{}-+*/..." should appear on tap begin so we can just drag our finger to the item we want and release, this means we can select an item without lifting our finger off the screen.</p>
]]></description>
   </item>
   <item>
      <title>Enhance your video recordings</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/598/enhance-your-video-recordings</link>
      <pubDate>Wed, 01 Feb 2012 20:59:18 -0500</pubDate>
      <dc:creator>Zoyt</dc:creator>
      <guid isPermaLink="false">598@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hello,<br />
When I upgraded to 1.3, I noticed that the recodings do not include recording of the keyboard and touches. So I went a little farther and created this library to enhance your recordings. It's a little sloppy right now, but it will do.</p>

<pre><code>function setup()
    touches = {}
    showKeyboard()
end

function draw()
    background(0, 0, 0, 255)
    enchanceRecording(CurrentTouch.x, CurrentTouch.y,1)
end

function enchanceRecording(gravx,gravy,shoingkeyboard)
    local buffer = keyboardBuffer()
    local _, gravh = textSize(Gravity.x)
    if isRecording() then
        font("HelveticaNeue")
        fill(179, 179, 179, 127)
        stroke(255, 255, 255, 127)
        strokeWidth(3)
        for k,touch in pairs(touches) do
            ellipse(touch.x, touch.y, 100, 100)
        end
        if shoingkeyboard == 1 then
            if buffer == nil then buffer = " " end
            textWrapWidth(WIDTH)
            if WIDTH == 1024 and HEIGHT == 768 or WIDTH == 750 and HEIGHT == 768 then
                rect(0,0,WIDTH,HEIGHT/2-30)
                fill(255, 0, 0, 255)
                text("Typed text:".. buffer,WIDTH/2,(HEIGHT/2-30)/2)
            elseif WIDTH == 768 and HEIGHT == 1024 or WIDTH == 494 and HEIGHT == 1024 then
                rect(0,0,WIDTH,HEIGHT/3-75)
                fill(255, 0, 0, 255)
                text("Typed text:".. buffer,WIDTH/2,(HEIGHT/3-75)/2)
            end
            fill(30, 255, 0, 255)
            text("Gravity X: ".. Gravity.x,gravx,gravy)
            text("Gravity Y: ".. Gravity.y,gravx,gravy-gravh)
            text("Gravity Z: ".. Gravity.z,gravx,gravy-gravh*2)
            text("User Acceleration X: ".. UserAcceleration.x,gravx,gravy-gravh*4)
            text("User Acceleration Y: ".. UserAcceleration.y,gravx,gravy-gravh*5)
            text("User Acceleration Z: ".. UserAcceleration.z,gravx,gravy-gravh*6)
        end
    end 
end


function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        touches[touch.id] = touch
    end
end
</code></pre>

<p>That turns out like this:<br />
<object width="320" height="180"><param name="movie" value="http://flurry.name/zoyt/downloads/files/IMG_0856.MOV" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed src="http://flurry.name/zoyt/downloads/files/IMG_0856.MOV" type="application/x-shockwave-flash" width="320" height="180" /></object><br />
If the video doesn't work, click <a rel="nofollow" href="http://flurry.name/zoyt/downloads/files/IMG_0856.MOV">here.</a></p>

<p><a rel="nofollow" href="http://flurry.name/zoyt/downloads/files/IMG_0856.MOV">Here is the link.</a></p>

<p>What's left to do? This:<br />
1.  Get rid of all the unnecessary numbers in the user acceleration and gravity.<br />
2.  And a few other small things I don't feel like listing right now.<br />
Have fun!</p>
]]></description>
   </item>
   <item>
      <title>Draggable physics objects.</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1731/draggable-physics-objects</link>
      <pubDate>Fri, 28 Sep 2012 15:16:46 -0400</pubDate>
      <dc:creator>kilobyte</dc:creator>
      <guid isPermaLink="false">1731@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>How do I write a physics body that can be dragged with touch. Like the hockey pucks in touch hockey.</p>
]]></description>
   </item>
   <item>
      <title>Please help - Touch</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1536/please-help-touch</link>
      <pubDate>Thu, 16 Aug 2012 20:10:28 -0400</pubDate>
      <dc:creator>ilankess</dc:creator>
      <guid isPermaLink="false">1536@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hello, 
I'm very new to codea and at the moment I'm trying to understand how the touch verbs work.
For the most part I've been using currentTouch and I was wondering if there was another way of doing it.
I was wondering if someone could please explain to me how I could make the algorithm know if the user was to touch an ellipse for example.
Thanks, Ilan</p>
]]></description>
   </item>
   <item>
      <title>Physics.ray...crash</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1168/physics-ray-crash</link>
      <pubDate>Sat, 09 Jun 2012 07:46:55 -0400</pubDate>
      <dc:creator>Orso</dc:creator>
      <guid isPermaLink="false">1168@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Each time I use physics.raycast it crashes my program here's the code I'm using</p>

<pre><code>if physics.raycast(touch.x,rx) and physics.raycast(touch.y, ry) then
print("c = "..(math.pi *  (rx + 250)))
end
</code></pre>

<p>I put that near the beginning of my touched function and im kicked out of codea as soon as I touch the screen.</p>
]]></description>
   </item>
   <item>
      <title>Dealing with Touch</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1431/dealing-with-touch</link>
      <pubDate>Sat, 28 Jul 2012 16:55:03 -0400</pubDate>
      <dc:creator>veeeralp</dc:creator>
      <guid isPermaLink="false">1431@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>In my game right now, im controlling the gravity just by tapping. i want to change the controls so that when i swipe up the physics.gravity is -physics.gravity() and when i swipe down its regular gravity. i will post my code if anyone asks for more help. i just can't get the touch.deltaY down. any help? i tried referrring to <a rel="nofollow" href="/Codea/Talk/profile/ruilov">@ruilov</a>'s pacman example but couldn't find that code snippet</p>
]]></description>
   </item>
   <item>
      <title>self.tapped command</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1440/self-tapped-command</link>
      <pubDate>Sun, 29 Jul 2012 12:02:49 -0400</pubDate>
      <dc:creator>Punker2000</dc:creator>
      <guid isPermaLink="false">1440@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>In Codea 1.4.4 there should be a self.tapped command to easily make buttons. Like this:</p>

<pre><code>function Button:touched(touch)
    if self.tapped then
           print("Tapped Button")
     else
           print("Tapped Elsewhere")
end
end
</code></pre>
]]></description>
   </item>
   <item>
      <title>Detecting a touch on a mesh</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1258/detecting-a-touch-on-a-mesh</link>
      <pubDate>Mon, 25 Jun 2012 05:25:22 -0400</pubDate>
      <dc:creator>SteveH</dc:creator>
      <guid isPermaLink="false">1258@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I've got a project that draws a lot of triangles. I have found that using a mesh gives me a nice triangle, but I can't figure out how to detect a touch on an individual triangle. I found this page, <a href="http://www.blackpawn.com/texts/pointinpoly/default.html" target="_blank" rel="nofollow">http://www.blackpawn.com/texts/pointinpoly/default.html</a> , and I'm trying to implement the algorithm at the end, but it's not working.</p>

<p>At first, I had A, B, C and P set up as vec2, but that didn't work, so I Googled some more and I think I need to use vec3. Because I'm translating my matrix when I draw the triangle, I'm trying to add the position to the vertices as well.</p>

<p>These are new concepts for me. I would be grateful for any assistance. I'd also be happy if there was some method I've overlooked that can do this simply.</p>

<pre><code>
function Pyramid:init(x, y)
    self.position = vec2(x, y)
    self.mesh = mesh()
    self.mesh.vertices = {vec2(0, 0), vec2(100, 0), vec2(50, 100)}
    self.size = SMALL
    self.color = GREEN
    self.selected = false
end

function Pyramid:hit(touch)
    local pos = vec3(self.position.x, self.position.y, 0)
    local A = vec3(self.mesh.vertices[1].x, self.mesh.vertices[1].y, 0) + pos
    local B = vec3(self.mesh.vertices[2].x, self.mesh.vertices[2].y, 0) + pos
    local C = vec3(self.mesh.vertices[3].x, self.mesh.vertices[3].y, 0) + pos
    local P = vec3(touch.x, touch.y, 0)
    
    local v0 = C - A
    local v1 = B - A
    local v2 = P - A
        
    local dot00 = v0:dot(v0)
    local dot01 = v0:dot(v1)
    local dot02 = v0:dot(v2)
    local dot11 = v1:dot(v1)
    local dot12 = v1:dot(v2)
    
    local invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
    local u = (dot11 * dot02 - dot01 * dot12) * invDenom
    local v = (dot00 * dot12 - dot01 * dot02) * invDenom
    
    return (u &gt;= 0) and (v &gt;= 0) and (u + v &lt; 1)
end
</code></pre>
]]></description>
   </item>
   <item>
      <title>Touch help, making a cursor follow the finger</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1196/touch-help-making-a-cursor-follow-the-finger</link>
      <pubDate>Sat, 16 Jun 2012 11:56:12 -0400</pubDate>
      <dc:creator>Finchy92</dc:creator>
      <guid isPermaLink="false">1196@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hi, I'm new to lua and all this coding business and starting to learn via Codea.
Just a quick question, I am trying to create a cursor that follows your finger when you touch the screen, and when you remove your finger the cursor disappears.</p>

<p>So far this is my code, it is simple but Im not sure how to get the image to go after the touch ha been removed.</p>

<pre><code>function touch()
    
    if touch.state == ENDED then touching = false
    else touching = true
    end
end

function setup()
    
    
end

function draw()
    background(0, 0, 0, 255)
    fill(127, 122, 132, 255)
    ellipse( CurrentTouch.x, CurrentTouch.y, 15)
    if touching == true then draw = ellipse( CurrentTouch.x, CurrentTouch.y, 15)
    end
    
end
</code></pre>

<p>Thanks for your help</p>
]]></description>
   </item>
   <item>
      <title>Detecting if a sprite is touched</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1124/detecting-if-a-sprite-is-touched</link>
      <pubDate>Fri, 01 Jun 2012 15:32:54 -0400</pubDate>
      <dc:creator>fglette</dc:creator>
      <guid isPermaLink="false">1124@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I'm working on a simple game as I learn Codea. I'm placing a random number of sprites on the screen. When a sprite is touched, I want it to disappear. I'm looking for a simple way to detect if a sprite is touched. Currently, it looks like I need to write a lot of conditional code. Is there and easy way to handle this in Codea?</p>
]]></description>
   </item>
   <item>
      <title>Touch Questions</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1219/touch-questions</link>
      <pubDate>Wed, 20 Jun 2012 12:13:15 -0400</pubDate>
      <dc:creator>tharkad</dc:creator>
      <guid isPermaLink="false">1219@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hi. I'm developing a board game that has a small amount of hidden information. I'm trying to make some areas on the screen where if the user places the edge of their hand down, the information will appear and then be hidden when they lift their hand. My first idea was to store the id of touches that BEGIN in the zone and then when that touch ends remove that touch from the table. Then when there are no touches in the table I'll hide the information. This works very well for discreet fingertip touches but when I put the edge of my hand down 15 or so touches get registered rapidly and when I lift my hand many of them do not get ENDED messages. Also every so often the four-finger app switch will be triggered with seems to stop Codea event processing and when it comes back the touches are gone.</p>

<p>So first any ideas about this or a better way.</p>

<p>Second, it would probably be sufficient to just hide the data if there are no current touches on the screen. Is there a way to determine this? The CurrentTouch always has a value even if there are no touches going on.</p>

<p>Thanks for any ideas.</p>
]]></description>
   </item>
   <item>
      <title>Touch and Go</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/1051/touch-and-go</link>
      <pubDate>Tue, 22 May 2012 23:51:46 -0400</pubDate>
      <dc:creator>LadyJayne</dc:creator>
      <guid isPermaLink="false">1051@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I am starting a new thread to address a specific question. I searched the forums and didn't find anything. If I missed it, let me know, and I will follow that lead. But, here is my situation:
I am working on a small project, and would like to have a sprite move in the direction of where I am touching on the screen. Below is my code (that does not work). I made comments what I think the code should be doing.</p>

<pre><code>-- #Main

--displayMode(FULLSCREEN)
supportedOrientations(CurrentOrientation)

-- Use this function to perform your initial setup
function setup()
    m = Mine()
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(142, 88, 38, 255)
    mineTouch()
    -- Do your drawing here
    m:draw()
   
    
end

function mineTouch()
    if CurrentTouch.state == BEGAN and MOVING then
       if CurrentTouch.x &lt; WIDTH then -- if touching inside the screen
        if CurrentTouch.y &lt; HEIGHT then
            -- move the sprite position towards the current touch
            m.pos = m.pos + vec2(CurrentTouch.x,CurrentTouch.y)
            end
        end
    end
    if CurrentTouch.state == ENDED then
     -- stop the sprite at its location
        m.pos = m.pos
    end
end

-- #Mine

Mine = class()

function Mine:init(x)
    -- you can accept and set parameters here
    self.pos = vec2(WIDTH/2, HEIGHT/2)
end

function Mine:draw()
    -- Codea does not automatically call this method
    sprite("Tyrian Remastered:Mine Spiked Huge",self.pos.x,self.pos.y)
end

function Mine:touched(touch)
    -- Codea does not automatically call this method
end
</code></pre>

<p>I don't want the sprite to "jump" to the CurrentTouch. I want it to be drawn every frame moving towards the CurrentTouch. Thanks.</p>
]]></description>
   </item>
   <item>
      <title>Text-based button</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/934/text-based-button</link>
      <pubDate>Fri, 04 May 2012 20:30:08 -0400</pubDate>
      <dc:creator>haloteen</dc:creator>
      <guid isPermaLink="false">934@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hello!
I have text that I want to turn into a button, that when pressed will take you to a new screen where you will start the game.
Here is what I have so far:</p>

<pre><code>background(0, 208, 255, 255)
font("Copperplate")
fill(255, 0, 6, 255)
fontSize(75)
text("Play Now!", WIDTH/2, HEIGHT/3)
</code></pre>
]]></description>
   </item>
   <item>
      <title>TouchLine</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/678/touchline</link>
      <pubDate>Sat, 18 Feb 2012 20:46:53 -0500</pubDate>
      <dc:creator>frosty</dc:creator>
      <guid isPermaLink="false">678@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I thought I'd do a quick extra post to show people my competition game. It's called TouchLine.</p>

<p><img src="http://www.jamesfrost.co.uk/images/codea/IMG_0218.JPG" alt="TouchLine title screen" /></p>

<p>The basic idea is that you move a line around the screen using two fingers (it's best to play with two hands to get the best view of the screen), and avoid obstacles for as long as you can. Take one hit, remove either finger, or make the line too short, and it's game over.</p>

<p><img src="http://www.jamesfrost.co.uk/images/codea/IMG_0219.JPG" alt="TouchLine gameplay" /></p>

<p>There are a number of power ups (would've liked to add more, but ran out of time):</p>

<ul>
<li>Slowdown (slows down all obstacles for 10 seconds)</li>
<li>Shield (allows you to take a hit and keep playing)</li>
<li>Zapper (lets you destroy obstacles for 10 seconds, for extra points)</li>
</ul>

<p>Here's a YouTube video showing it in action: <a rel="nofollow" href="http://www.youtube.com/watch?v=ih6IE2bZOQI">http://www.youtube.com/watch?v=ih6IE2bZOQI</a></p>

<p>I'll post code soon. I also want to extend and separate out the Tween class I started to put together for this. It basically allows you to pass the name of a property, a starting value, a finishing value, and a timespan, and will then animate the property between the provided value for you. Hopefully other people will find it useful! I also created a starfield backdrop - I know there was a discussion abut that sort of thing on here the other day.</p>
]]></description>
   </item>
   <item>
      <title>checking touched area using physics.body</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/902/checking-touched-area-using-physics-body</link>
      <pubDate>Mon, 30 Apr 2012 16:11:34 -0400</pubDate>
      <dc:creator>lupo</dc:creator>
      <guid isPermaLink="false">902@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>im new to this forum, and have played with Codea for only a few days now. Im trying to make a simple touch-controller class to decide the direction of movement in a game.</p>

<p>My approach is to divied the whole screen into four triangles. when the touch position is within the upper triangle, i set a variable "up" to true, similar for left, right and down..</p>

<p>To determine which triangle has been touched i decided to go with the physics api because of the nice collision checks.</p>

<p>However, the check works only when the game is freshly started. After a few seconds the collision checks start failing. Im already printing the triangles to check if they somehow move automagically, but everything seems ok.</p>

<p>It would be nice if someone can give any hint, thanks in advance.</p>

<p>here's the code of the controller class:</p>

<pre><code>Controller = class()

function Controller:init()
    
    self.ptTopLeft = vec2(0, HEIGHT)
    self.ptTopRight = vec2(WIDTH, HEIGHT)
    self.ptBtLeft = vec2(0,0)
    self.ptBtRight = vec2(WIDTH, 0)
    self.ptCenter = vec2(WIDTH/2, HEIGHT/2)
    
    self.areaTop = physics.body(POLYGON, unpack({self.ptTopLeft, self.ptTopRight, self.ptCenter}))
    self.areaBottom = physics.body(POLYGON, unpack({self.ptBtLeft, self.ptBtRight, self.ptCenter}))
    self.areaLeft = physics.body(POLYGON, unpack({self.ptBtLeft, self.ptTopLeft, self.ptCenter}))
    self.areaRight = physics.body(POLYGON, unpack({self.ptBtRight, self.ptTopRight, self.ptCenter}))
    
    self.left = false
    self.right = false
    self.up = false
    self.down = false
    
    --local poly = physics.body(POLYGON, unpack(points))
    
end

function Controller:touched(touch)
    local touchVec
    touchVec = vec2(touch.x, touch.y)
    print(touch.state)
    if(touch.state == ENDED) then 
        self.up = false
        self.down = false
        self.left = false
        self.right = false
    elseif (self.areaTop:testPoint(touchVec) == true) then
        self.up = true
    elseif (self.areaBottom:testPoint(touchVec) == true) then
        self.down = true
    elseif (self.areaLeft:testPoint(touchVec) == true) then
        self.left = true
    elseif (self.areaRight:testPoint(touchVec) == true) then
        self.right = true
    end
end 

function Controller:draw()
    drawBody(self.areaRight)
    drawBody(self.areaLeft)
end

function drawBody(body)
    pushStyle()
    stroke(255, 0, 0, 255)
    strokeWidth(7)
    local points = body.points
        for j = 1,#points do
            a = points[j]
            b = points[(j % #points)+1]
            line(a.x, a.y, b.x, b.y)
        end
    popStyle()
end
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to best colourise an object when touched</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/696/how-to-best-colourise-an-object-when-touched</link>
      <pubDate>Wed, 22 Feb 2012 21:58:51 -0500</pubDate>
      <dc:creator>mattbastin</dc:creator>
      <guid isPermaLink="false">696@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Button type things in most applications look best when they change colour or tone (and make a sound like a click - thats the easy bit I think) when they are touched, particularly while they are touched - to make it look like they really have been depressed as a real button. My question is how best to do this in Codea. If I use a rectangle or ellipse how can apply a colour change only to the touched shape. If I use a sprite am I best make a copy of the sprite in two different colour schemes.</p>
]]></description>
   </item>
   <item>
      <title>Translating touches?</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/497/translating-touches</link>
      <pubDate>Sat, 14 Jan 2012 21:23:04 -0500</pubDate>
      <dc:creator>Mark</dc:creator>
      <guid isPermaLink="false">497@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>I'm reworking some UI pieces so that they correctly support various display modes. The neatest way I found to do this was to use translate. That way I can just change the coordinates on one framing element and draw everything else against that boundary. It turns my UI into movable, resizable tiles that I can arrange depending on screen space.</p>

<p>It works great for drawing BUT, the elements and sub-elements of the UI are heavily dependent on passing around a touch element and letting each piece respond appropriately. And I can't seem to translate touch.</p>

<p>Even if I copy CurrentTouch into another touch instance, it won't let me alter the values of x and y.</p>

<p>Is there any way out of this short of breaking down touch into it's values and passing them all individually?</p>
]]></description>
   </item>
   <item>
      <title>Biochar app</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/370/biochar-app</link>
      <pubDate>Thu, 22 Dec 2011 22:31:22 -0500</pubDate>
      <dc:creator>Ipad41001</dc:creator>
      <guid isPermaLink="false">370@/Codea/Talk/discussions</guid>
      <description><![CDATA[<pre>Touch screen to light. 
Move flame to center log. 
It will heat to 100C and then blow steam. 
Keep heating until the moisture content goes down to zero. 
Steaming will stop as self ignition starts. 
You can stop touching now. 
Heat continues to rise as wood slowly burns away leaving a bit of ash.
</pre>

<p>It actually doesn't do biochar ... Yet</p>

<p>I have to think of oxygen detection and better heat detection</p>

<p><a href="http://ipad41001.posterous.com/biochar-app-via-codea-which-is-on-sale-now" target="_blank" rel="nofollow">http://ipad41001.posterous.com/biochar-app-via-codea-which-is-on-sale-now</a></p>

<p>App</p>

<p><a href="http://ipad41001.posterous.com/biochar-app-003" target="_blank" rel="nofollow">http://ipad41001.posterous.com/biochar-app-003</a></p>
]]></description>
   </item>
   <item>
      <title>API for gestures</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/180/api-for-gestures</link>
      <pubDate>Sun, 20 Nov 2011 06:02:09 -0500</pubDate>
      <dc:creator>bee</dc:creator>
      <guid isPermaLink="false">180@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p>Is there a plan to provide APIs for gestures i.e. tap-n-hold for moving, two-fingers pinch for zooming, two-fingers rotation, multi-fingers swipes, drag-n-drop, etc. for Box2D physical objects? I think that would be a great addition to physics API.</p>

<p>For example, if user want to give an object a user controlled (default) rotation capability, s/he could simple enable it by calling i.e. <code>circle:gestureRotation = true</code>, or <code>rect:gesturePosition = true</code> to enable (default) user controlled movement capability, or <code>poly:gestureZoom = true</code> to enable (default) user controlled resize capability, etc. To override the default behavior, user could override the function that is used for the gesture callback. These APIs could save Codea's users coding time for basic/default gestures for object manipulation. Great for quick game prototyping.</p>

<p>What do you think? :)</p>
]]></description>
   </item>
   <item>
      <title>Zoom In/Out using pinch gesture with touches</title>
      <link>http://twolivesleft.com/Codea/Talk/discussion/185/zoom-in-out-using-pinch-gesture-with-touches</link>
      <pubDate>Sun, 20 Nov 2011 16:16:27 -0500</pubDate>
      <dc:creator>Ipad41001</dc:creator>
      <guid isPermaLink="false">185@/Codea/Talk/discussions</guid>
      <description><![CDATA[<p><a href="http://pastebin.com/3ACzCjd3" target="_blank" rel="nofollow">http://pastebin.com/3ACzCjd3</a></p>

<p>Demostrates a way to implement a pinch zoom gesture and how to shuffle touch information around.</p>
]]></description>
   </item>
   </channel>
</rss>