How to use the accelerometer?

Discussion in 'Jiggy Runtime Development' started by blackjak231, May 24, 2008.

  1. blackjak231

    blackjak231 Member

    Joined:
    Oct 1, 2007
    Messages:
    759
    Likes Received:
    6
    Device:
    iPhone 4 (White)
    Hey guys, i have a great app coming soon (secret), and i am trying to use the accelerometer so thar when i shake the ipod, something shows up. I knoa some people have use the $$.js file with the shaker function, but i cant use that....

    Thanks to anyone who will help!!!!
  2. PAEz

    PAEz New Member

    Joined:
    Jan 29, 2008
    Messages:
    201
    Likes Received:
    10
    Theres an example on Accelerometer here....
    http://jiggyapp.com/download/examples/
    ...just check for sudden erratic movement i guess.
    Oh and keep in mind if you do too much in the onAccelerate then the updates will slow down.....you cant get another onAccelerate event until its finished doing the last one. So to keep it smooth, use the onAccelerate to place the x,y,z in some global variables and accesse them from a Timer event, depending on what your doing offcourse. You could also keep adding the values to a global variable and then accesse all the values in another function, so you could like see everything that happened since the last check.

    And why cant you use $$.js, thats one great file.
    1 person likes this.
  3. blackjak231

    blackjak231 Member

    Joined:
    Oct 1, 2007
    Messages:
    759
    Likes Received:
    6
    Device:
    iPhone 4 (White)
    ok, thanks, but how do i use the code of the example to only make stuff appear on "sudden" moves??

    +rep anyway, it helped me!
  4. jakecigar

    jakecigar New Member

    Joined:
    Mar 7, 2008
    Messages:
    83
    Likes Received:
    6
    Device:
    iPhone 4 (Black)
    You can get the latest version of $$ from inside JGoogle, I use the shake code to refresh the browser.

    the important code is

    Code:
    if (!this.xyz) this.xyz = [0,0,0]
    	if (Math.abs(this.xyz[0]) - Math.abs(x) > 0.3 || Math.abs(this.xyz[1]) - Math.abs(y) > 0.3 )
    
    it's that little bit of code that I use to signal that the touch was shaked.

    of course the coolness of $$ is that you just make a window with the shaker in it

    Code:
    var window $$.window(null,{
    	shaker: myShakeFunction,
    	backgroundColor : WHITE
    })
    now you have a UIWindow (all set up) that will call your myShakeFunction when you shake!
    1 person likes this.
  5. blackjak231

    blackjak231 Member

    Joined:
    Oct 1, 2007
    Messages:
    759
    Likes Received:
    6
    Device:
    iPhone 4 (White)
    Thank you so much!!!!!
    But do i still need to include this?:
    Code:
    var xlabel = new UITextLabel( [ 10 , 0 , window.bounds[ 2 ] - 20 , 60 ] );
    var ylabel = new UITextLabel( [ 10 , 60 , window.bounds[ 2 ] - 20 , 60 ] );
    var zlabel = new UITextLabel( [ 10 , 120 , window.bounds[ 2 ] - 20 , 60 ] );
    
    Thanks for your help!!
  6. jakecigar

    jakecigar New Member

    Joined:
    Mar 7, 2008
    Messages:
    83
    Likes Received:
    6
    Device:
    iPhone 4 (Black)
    those text labels have nothing to do with anything more than displaying the values from the accelerometer ... not for using them

Share This Page