Start a new topic

How to react on triggers only once

How to react on triggers only once


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

       var drumSounds = "http://www.wikitude.com/doc/tutorial/audio_12.mp3";

        if (!audio1.src) //If src does NOT exist

            {

          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?

 

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. 

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. 

setting the latitude/longitude property is quite simple just call:

myGeoLocation.latitude = lat;
myGeoLocation.longitude = lon;

 

var myGeoLocation = null;

 

function locationChanged(lat, lon, alt, acc) { 

        myGeoLocation = new AR.GeoLocation(lat,lon);

}

AR.context.onLocationChanged = locationChanged;

 

myGeoLocation.latitude = lat;

myGeoLocation.longitude = lon;

 

alert(lat);

 

Am I doing something wrong? because i cant alert the latitud or longitud. 

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).

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?

Please use the ADE to see if the code works correctly. This also gives you the ability to debug the javascript code directly. 
Login or Signup to post a comment