Move object based on device motion

Get help using Construct 2

Post » Sun Dec 04, 2011 10:01 pm

I'm making an airplane shooter game for iPad in which you have a player who fires at other airplanes to destroy them. I am trying get my number of buttons down to ONE, and I want that button to be the "fire bullet" button. Therefore, I need a way to move my player without having to touch the screen. I would love if I could tilt the iPad to move the player along the X axis. Any help is appreciated. Thanks in advance!
B
3
G
1
Posts: 7
Reputation: 514

Post » Sun Dec 04, 2011 10:10 pm

I'm not too sure if that's possible, but you could try the "Touch" behaviour! :]
B
17
S
4
G
5
Posts: 226
Reputation: 4,042

Post » Sun Dec 04, 2011 10:13 pm

I tried that and found that a condition can be "Device motion supported," but once I have that I don't know what to do.
B
3
G
1
Posts: 7
Reputation: 514

Post » Mon Dec 05, 2011 3:34 am

Adam, I haven't worked much with the Touch plug-in, but try the following and you will see the actions and methods you need to use:

1. Create a new project
2. In the layout, add the Touch plug-in
3. In the layout, add a sprite
4. In the event sheet, add an event: Touch:Device orientation is supported. This is a condition which checks to make sure that device orientation is supported. If it is, then the action will be executed.
5. Add an action, and select your sprite
6. Under the Size & Position section, select Set X
7. When the prompt comes up, go to the Objects with expressions window, and select Touch

Under the Orientation & Motion section, you will see the Acceleration methods. This should get you started. There are many others on the forums who will be able to help you with the specifics regarding the methods and the math involved.

Good luck!
Wastrel2011-12-05 03:36:14
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Tue Dec 06, 2011 12:25 am

Thank you! I will try that and let you know how it works.
B
3
G
1
Posts: 7
Reputation: 514

Post » Fri Dec 06, 2013 6:18 am

Here is some simple psuedo-code for my game that moves a ball by tilting the phone (works for me on Android by exporting through CocoonJS):

If Touch {alpha + beta + gamma)orientations NOT = 0,
--- Sprite Set 8Direction Vector X to Touch.AccelerationYWithG * 32
--- Sprite Set 8Direction Vector Y to Touch.AccelerationXWithG * 32

(Sprite has 8Direction Behavior attached to it)

Note that X and Y values are interchanged - this is done by purpose as my game is in Landscape mode and I believe the values that are being passed are assuming Portrait mode. The value 32 could be replaced by any other number - the higher the number the faster the response speed.

Please let me know if this works for you, or if you have a better idea.exertia2013-12-06 06:27:10
Moving Mazes - Action Puzzle game with a twist!
Play it now: http://www.cre80ve.com
B
15
S
3
G
1
Posts: 17
Reputation: 2,616

Post » Wed Jan 15, 2014 11:30 pm

@exertia it worked for me, thanks!

how do i detect orientation of device? if the user flip device upside down, then it'll move to positive or negative value to fit their orientation
Doo Doo Diving App Store | Google Play

Donut Pirate App Store | Google Play
B
14
S
2
G
1
Posts: 193
Reputation: 1,483

Post » Thu Jan 16, 2014 12:09 am

You can lock the orientation from both the project properties within Construct 2 as well as in CocoonJS.

Otherwise, if you want to allow auto-rotation, you should be able to use these values of the Touch object to determine the orientation:

Alpha
Beta
Gamma
Return the device's orientation if supported, or 0 at all times if not supported. Alpha is the compass direction in degrees. Beta is the device front-to-back tilt in degrees (i.e. tilting forwards away from you if holding in front of you). A positive value indicates front tilt and a negative value indicates back tilt. Gamma is the device left-to-right tilt in degrees (i.e. twisting if holding in front of you). A positive value indicates right tilt and a negative value indicates left tilt.
Moving Mazes - Action Puzzle game with a twist!
Play it now: http://www.cre80ve.com
B
15
S
3
G
1
Posts: 17
Reputation: 2,616

Post » Thu Jan 16, 2014 5:01 am

guess i'll just lock it, save some unnecessary problems, thanks!
Doo Doo Diving App Store | Google Play

Donut Pirate App Store | Google Play
B
14
S
2
G
1
Posts: 193
Reputation: 1,483


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 26 guests