Skip to content

Instantly share code, notes, and snippets.

@nuno
Created January 12, 2014 00:45
Show Gist options
  • Save nuno/8379009 to your computer and use it in GitHub Desktop.
Save nuno/8379009 to your computer and use it in GitHub Desktop.
Titanium- Control Stuff With Accelerometer
var win = Ti.UI.createWindow();
var ball = Ti.UI.createView({
backgroundColor:'blue',
height:50,
width:50,
borderRadius:20
});
win.add(ball);
var label = Ti.UI.createLabel({
text:'',
top:5,
left:5,
width:'100%',
color:'#ffffff'
});
win.add(label);
var valueX;
var valueY;
Titanium.Accelerometer.addEventListener('update', function(e) {
valueX = e.x*100.0;
valueY = e.y*100.0;
var newX = ball.center.x +valueX;
if (newX > 320-(ball.width/2))
newX = 320-(ball.width/2);
if (newX < 0+(ball.width/2))
newX = 0+(ball.width/2);
var newY = ball.center.y -valueY;
if (newY > 460-(ball.width/2))
newY = 460-(ball.width/2);
if (newY < 0+(ball.width/2))
newY = 0+(ball.width/2);
label.text = 'x:'+newX+' y: '+newY;
ball.animate({center:{x:newX,y:newY},duration:0.5});
});
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment