tilt and change?

Hi y’all,
Does anyone have a sample where you tilt (accelerometer) and change images(example, animated pointing up/left/right arrow .png)?

what I want, is that when user tilts their device (landscape orientation) to the left, an image(left arrow) with animation gets trigger, when device is tilt to the right, image(right arrow) with animation gets trigger, however, if no tilt, just have static image(up arrow) show.

does that make sense?

tilt left, show left image, tilt right, show right image…no tilt, static image.

Any ideas?

Thanks,
RD [import]uid: 7856 topic_id: 4104 reply_id: 304104[/import]

Hi RD,

It all depends on the accelerometer frequency you have set, because you have to detect what frequencies will cause an image switch.

It was VERY difficult to do with Dungeon Tap, and the solution I came up with might not be exactly what you’re looking for, because there is no such thing as ‘no tilt’. No matter how still you hold the device, it’ll be registering accelerometer events as long as you have a listener. Depending on your frequency you have set, you have to do a lot of testing and figure out what range means still image, which one means to switch to left-facing animation, right-facing animation, etc.

What I did was create a newText object that shows me the accelerometer data, then, I’d test on the device and then tilt slightly one way and figure out at what point (and what range) should trigger a change in direction.

Also, because of the ‘no such thing as no tilt’ thing, I never figured out how to do a ‘just standing’ position. If you pay close attention in Dungeon Tap, the character always appears to be walking, it’s just at a certain tilt he’ll change the direction he’s facing.

Good luck!

Jonatan [import]uid: 7849 topic_id: 4104 reply_id: 12958[/import]

Thanks jonbeebe,
I agree, there will be no way to get a ‘no tilt’ function, but I think the way Tap Dungeon is, is perfect for what I need.
could you please post your code or pseudo on how you accomplish this?

Thanks,
RD [import]uid: 7856 topic_id: 4104 reply_id: 12971[/import]

I’ve been using Corona since the alpha builds of Game Edition (when sprites were still pretty buggy), so I got used to using movieclips, which probably isn’t the most efficient way to do things right now but it’s working fine for me so far.

With that said, keep in mind that the code below is using the movieclip library instead of spritesheets. So if you use spritesheets, just modify lines that are movieclip-specific to their spritesheet code counterparts:

To explain the code a little bit, in Dungeon Tap, I have a never-ending timer handle the switching of frames for the player’s animation. The code below is pasted directly from the Dungeon Tap source code:

[blockcode]
local playerAnimation = function()
if gameIsActive then
if playerObject.isJumping == false and playerObject.isFalling == false then
if playerObject.framePosition == “left” then
if playerObject.frameNumber == 1 then
playerObject:stopAtFrame( playerObject.startWalkLeft )
playerObject.frameNumber = 2
elseif playerObject.frameNumber == 2 then
playerObject:stopAtFrame( playerObject.endWalkLeft )
playerObject.frameNumber = 1
end
elseif playerObject.framePosition == “right” then
if playerObject.frameNumber == 1 then
playerObject:stopAtFrame( playerObject.startWalkRight )
playerObject.frameNumber = 2
elseif playerObject.frameNumber == 2 then
playerObject:stopAtFrame( playerObject.endWalkRight )
playerObject.frameNumber = 1
end
end
elseif playerObject.isFalling == true then
if playerObject.framePosition == “right” then
playerObject:stopAtFrame( playerObject.fallRightFrame )
elseif playerObject.framePosition == “left” then
playerObject:stopAtFrame( playerObject.fallLeftFrame )
end
end
end
end

playerObject.animTimer = timer.performWithDelay( 250, playerAnimation, 0 ) --> 250 is default interval
[/blockcode]

So the above code is what keeps the player’s animation and frames updated. Below is my onTilt() function (my accelerometer interval is set to 45.0):

[blockcode]
local onTilt = function( event )
if gameIsActive then
if event.xGravity > 0.01 then
playerObject.framePosition = “right”
elseif event.xGravity < -0.01 then
playerObject.framePosition = “left”
end

playerObject.x = playerObject.x + (playerObject.speed * event.xGravity)
end
end
[/blockcode]

Some of the code above is Dungeon Tap-specific, so that obviously won’t apply to your project, but the code above should give you an idea of how to accomplish the whole thing.

Hope that helps!

Jonathan Beebe
http://jonbeebe.tumblr.com/
http://beebegamesonline.appspot.com/ [import]uid: 7849 topic_id: 4104 reply_id: 12998[/import]

Wow, I didn’t thought it was going to be this ‘simple’…LOL

Thanks for sharing…sent u an email.

RD [import]uid: 7856 topic_id: 4104 reply_id: 13039[/import]

Jonathan,

You used event.xGravity > 0.01 and event.xGravity < -0.01 – could you have set no walking to be any value between -0.01 and 0.01? Or are there not enough values returned in that range to really check?

In which case, if you bump the values to -.02 and .02 you’d have more room to work with for “stand around” but then maybe you’d have to tilt too much to move the guy?

I’m sure you tried everything under the sun – just thinking out loud. :slight_smile:

Jay
[import]uid: 9440 topic_id: 4104 reply_id: 13046[/import]

I did try that for Dungeon Tap, and yes, it would have been too much tilting for what I wanted. It really depends on the individual needs of your game. Because you’re encouraged to constantly move in Dungeon Tap, it wasn’t that big of a deal. I’m sure your solution would probably work great for games that wouldn’t hurt to tilt a little more before moving a specific direction. [import]uid: 7849 topic_id: 4104 reply_id: 13057[/import]

So I got the tilting working thanks to y’all.
Now my issue is, I have one object (dynamic) and two walls (static); however, when I tilt my device (landscape) too fast, the object goes through the walls (they are 10px wide and my object is about 16px radius).

Any ideas?

@jonbeebe,
If you could please post the settings/code for your ‘accelerometer’ that would be great. I love how smooth and easy tilt monster moves ^-^;

Thanks,
RD [import]uid: 7856 topic_id: 4104 reply_id: 14596[/import]