Hi! Im creating a treasure hunt game and have placed out a couple of items. I used the method distanceTo to get the distance between myGeoLocation and the different objects. When the user is in a specific area the objects get visible and when not they disappear. So far everything works fine, but when i want to autoplay a sound to a specific area the sound is played more than once. This is because of the onLocationChange function, that call every functions when i change position.
var myGeoLocation = null;
function locationChanged(lat, lon, alt, acc) {
myGeoLocation = new AR.GeoLocation(lat,lon); //My coordinates right now
var geoLocationStation = new AR.GeoLocation(XX,XX); //XX is the coordinates
loadingStepDone();
}
AR.context.onLocationChanged = locationChanged;
function loadingStepDone() {
var dist = myGeoLocation.distanceTo(geoLocationStation);
if (dist < 500) {
var audio = document.createElement('audio'); //create audio element
audio.setAttribute('src', drumSounds); //we set src to drumSounds
audio.setAttribute('autostart', true); //set autostart to true
audio.load(); //load the sound
audio.play(); //play the sound
body.appendChild(audio); //put the audio element to the body
}
}
I would like to have two variables myGeolocation and the distance updated all the time when i change position, but i don't want the sound to play more than once. This problem also occur if I add a onclick trigger so the object should disaapear. but because i call the functions again a new object appear. Is there anyone who can help me with this?
W
Wolfgang Damm
said
about 11 years ago
I'm guessing you could introduce a variable that tells you if the sound was loaded already. But in general I would think about the structure a bit more. Currently each time a GeoLocation is created, thus might end up with a lot of unecessary objects. You can reuse a GeoLocation by setting its latitude, longitude property. If you don't want to display an GeoObject that is at a certain GeoLocation you can set it's enabled property to false.
You might also want to have a look at AR.ActionRange which allows you to set up the scene once and automatically get notifications when the user enters/exits such an area.
E
Emma
said
about 11 years ago
Do you have any example of how i could do to reuse the Geolocations latitud and longitud?
I have tried the actionrange method but it didn't work so thats why i cae up with the distanceTo solution instead.
W
Wolfgang Damm
said
about 11 years ago
setting the latitude/longitude property is quite simple just call:
Am I doing something wrong? because i cant alert the latitud or longitud.
W
Wolfgang Damm
said
about 11 years ago
alert does not work on Android. You should use the AR.logger functions for logging anything. Call AR.logger.activateDebugMode() to show the logging console on the device.
Additionally you can debug in a desktop browser by using the ADE (included in the SDK).
E
Emma
said
about 11 years ago
I use iOS and alert has worked before so that shouldn't be a problem. But is the code wrong or am i missing something?
W
Wolfgang Damm
said
about 11 years ago
Please use the ADE to see if the code works correctly. This also gives you the ability to debug the javascript code directly.
Emma