A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Automatically assign variable names

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    6

    Automatically assign variable names

    Hi, I'm working on a project using google maps. For part of it the user must enter coordinates and click a button in order to add a marker to a map. The code I have written so far allows this to be done once:

    play_btn2.addEventListener(MouseEvent.MOUSE_DOWN, submitdata);
    function submitdata(e:Event){
    var marker9:Marker = new Marker(
    new LatLng(Number(myTextBox1.text), Number(myTextBox2.text)),
    new MarkerOptions({
    radius: 8,
    hasShadow: true

    })
    );
    gotoAndStop("Home");
    map.addOverlay(marker9);
    }


    As it is the only one marker can be added as it keeps getting replaced. I want to make it so that the variable name changes each time a marker is to be added. I've made a few attempts using i++ but I'm not really sure what I'm doing.
    Anyone know how I can get this working?

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    There are a couple of ways to do this, but generally I use an array to store my different items. This is what I mean.
    PHP Code:
    var markerArr:Array = new Array();


    play_btn2.addEventListener(MouseEvent.MOUSE_DOWNsubmitdata);

    function 
    submitdata(e:Event){
        var 
    tempMarker:Marker = new Marker(
                        new 
    LatLng(Number(myTextBox1.text), Number(myTextBox2.text)),
                        new 
    MarkerOptions({ radius8hasShadowtrue})
                                        );
        
    markerArr.push(tempMarker);
        
    gotoAndStop("Home");
        
    map.addOverlay(markerArr[markerArr.length-1]);

    Of course you can get a dynamic class with a new name and then load the Marker class into it. I find that easier and allows me to track ALL of my similar objects.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Hi, thanks for the reply.
    I used your code and it works perfectly if the input boxes and map are on the same page but I have them on different pages. This is the code for when the user clicks into the page to input the values.

    PHP Code:
    sounds_btn.addEventListener(MouseEvent.CLICKgoSounds);
    function 
    goSounds (e:MouseEvent):void{
    gotoAndStop("Sounds");
        
    removeChild(aura);
        
    removeChild(volumeslider);
        
    removeChild(map);
        
    removeChild(circle);
        
    addEventListener(Event.ENTER_FRAMEremovecircle);
        
        
    addChild(myTextBox1);
        
    addChild(myTextBox2);
        
    addChild(myTextBox3);
        
    addChild(myTextBox4);

    var 
    markerArr:Array = new Array();

    play_btn2.addEventListener(MouseEvent.MOUSE_DOWNsubmitdata);

    function 
    submitdata(e:Event){
        var 
    tempMarker:Marker = new Marker(
        new 
    LatLng(Number(myTextBox1.text), Number(myTextBox2.text)),
        new 
    MarkerOptions({ radius8hasShadowtrue})
                                        );
        
    markerArr.push(tempMarker);
        
    gotoAndStop("Home");
        
    map.addOverlay(markerArr[markerArr.length-1]);

    I'm having problems placing this code anywhere else.
    Any idea what I can do to get this working?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center