Hoping someone can help me set up a basic demo for a display ad.
I need to have a symbol ( containing image & text ) over the top of a background image. On tilting your mobile device it will move left or right only on the horizontal axis.
I would like full control over the speed it moves and how far left or right I can lock it to moving. I may also need to do some further versions where it moves vertically only. Is this possible using the physics engine in Hype 3?
I have left the borders in so that you can see them.
They all have static properties in the Physics inspector and the rectangle in the middle has a Dynamic property (full physics body) and then with the “control gravity with device tilt” checked you can move the rectangle about only within the boundaries of the elements (therefore restricting movement to left and right only.
*note haven’t tested as don’t have mobile devices to hand
This will allow the user to increase/decrease speed based on the tilt. If you just want to move the object a specific speed then you can get the device orientation by Javascript and then set the element’s left property based on the value returned by the motion
You can do something with Physics as far as manually controlling the distance or the speed I am not sure.
But you can preset the speed and distance in your Physics inspector.
You will need to make a scaffold of sorts of invisible static barriers in the screenshot they have blue borders.
Then create your moving piece as Dynamic
Set physics to control gravity with device tilt and you should be good to go.
here is a template. tilt ad.zip (15.0 KB)
If you want to control the speed and limit the distance without using Physics then (and this will only work in some browsers) then get the device’s orientation and move the element according to the gamma, beta or alpha data. Place this on scene load and give your element (symbol) and id.
var box = hypeDocument.getElementById('box');
var start = hypeDocument.getElementProperty(box, 'left')
orientationListener = function(ev){
if (Math.floor(start + ev.gamma) < start-2){
hypeDocument.setElementProperty(box, 'left', start-200, 0.4, 'easeinout')
} else if (Math.floor(start + ev.gamma) > start+2) {
hypeDocument.setElementProperty(box, 'left', start+200, 0.4, 'easeinout')
} else {
hypeDocument.setElementProperty(box, 'left', start, 0.4, 'easeinout')
}
}
if (window.DeviceOrientationEvent) {
window.addEventListener("deviceorientation", orientationListener); } else {
console.log("Sorry, your browser doesn't support Device Orientation");
}
The position is controlled by the start+number or start-number.
The speed would be the time property in the setElementProperty method. In this example 0.4 secs.
This approach only needs 1 element with an ID of ‘box’.