Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Problems with addEventListener

I know I must be over looking something stupid. I got the click event working in another file, but this one is killing me. Is there something I am doing wrong. The image shows up, but no events are fired off.

And lastly,… Is there a limit to the amount of vars that you can assign? I have about 200. But I took them out to test this string of code separately, and that still did not work. All graphics show in simulator but no events fired!

code:::

var fiverhImage = Titanium.UI.createView({

height:18,

width:145,

top:320,

left:153,

backgroundImage:'../images/conf/5er-h.png'

});

window.add(fiverhImage);

fiverhImage.addEventListener('click', function()
{

//

//Ti.API.info('IN fiver')

alert("clicked fiver");

if(vessel == "nil"){

if(fiverh == "nil"){

    fiverh = "clicked";

    vessel = "fiverh";

}

else{

    alert("You already selected this vessel!");

    }

}

else{

alert("It seems that you already picked a vessel!");

}

});

— asked April 1st 2010 by Jason Brock
  • addeventhandler
  • iphone
0 Comments

13 Answers

  • Is it possible that you have another view or something over the top of this one elsewhere in your code? When I tried your code I was able to trigger the click event without any trouble.

    Could try commenting out any other views you add to the window to help narrow it down, or try setting the zIndex for this view to 100 or something to make sure its at the top if there are multiple views stacking up.

    Regarding a limit to the number of variables, 200 sounds like a lot but I'm not aware of any limitations so theoretically it should work as far as I know.

    — answered April 1st 2010 by Dan Giulvezan
    permalink
    0 Comments
  • Still no go. This is the whole file. Everything else is commented out.

    var window = Titanium.UI.createWindow({

    title:'The Enigma Code',
    
    backgroundImage:'../images/bg.png'
    

    });

    window.open({fullscreen:true});

    var fiverhImage = Titanium.UI.createView({

    height:18,
    
    width:145,
    
    top:320,
    
        left:153,
    
    backgroundImage:'../images/conf/5er-h.png'
    

    });

    window.add(fiverhImage);

    fiverhImage.addEventListener('touchmove', function()

    {

    fiverhImage.animate({center:{x:e.x,y:e.y}, duration:1});    
    

    });

    — answered April 1st 2010 by Jason Brock
    permalink
    0 Comments
  • Is it possible that I need to delete some build files? Clean it up?

    — answered April 1st 2010 by Jason Brock
    permalink
    0 Comments
  • Generally you don't but if you're using mobile SDK 1.1.0 you'll want to because it had a few issues.

    Heck, either way you might as well try it.

    To clean your project, go to the Terminal and into [your_project]/build/iphone and run

    xcodebuild clean -configuration Debug
    
    — answered April 1st 2010 by Dan Giulvezan
    permalink
    0 Comments
  • One more question: is this code going into your app.js or into it's own .js file?

    — answered April 1st 2010 by Dan Giulvezan
    permalink
    0 Comments
  • It's own .js file.

    I have the app.js going to this file from a tableview

    — answered April 1st 2010 by Jason Brock
    permalink
    0 Comments
  • Okay, this should work:

    var win = Ti.UI.currentWindow;
    var fiverhImage = Titanium.UI.createView({
        height:18,
        width:145,
        top:320,
        left:153,
        backgroundImage:'../images/conf/5er-h.png'
    });
    win.add(fiverhImage);
    fiverhImage.addEventListener('touchmove', function(e) {
        fiverhImage.animate({center:{x:e.x,y:e.y}, duration:1});
    });
    

    I made a few changes:

    • Took out the extra createWindow since you can just add the view to the currentWindow object
    • you were missing the e in function(e) so added that

    If you want to still set the backgroung image of that window, you can either do it from your app.js when you open that window, or from within this window you can do something like this:

    win.backgroundImage = '../images/bg.png';
    
    — answered April 1st 2010 by Dan Giulvezan
    permalink
    0 Comments
  • ok I will try this

    thank you

    — answered April 1st 2010 by Jason Brock
    permalink
    0 Comments
  • Ok, maybe it was the "e", what do you think.

    I thought I did not need the "e" if no input?

    — answered April 1st 2010 by Jason Brock
    permalink
    0 Comments
  • Thank you very much

    — answered April 2nd 2010 by Jason Brock
    permalink
    0 Comments
  • In this case you actually do need the e because you're using the response from that function to get the x and y coordinates of the image with e.x,y:e.y.

    — answered April 2nd 2010 by Dan Giulvezan
    permalink
    0 Comments
  • fiverhImage.addEventListener('touchmove', function()

    {

    fiverhImage.animate({center:{x:e.x,y:e.y}, duration:1});

    });

    Maybe the e in function(e)

    Why not test with click first as in

    fiverhImage.addEventListener('click', function()

    — answered April 2nd 2010 by Peter Lum
    permalink
    0 Comments
  • Yes I noticed it did not have to do with the e. It was using current window that worked. My problem is that I see now the first window along with the second window in one. I just wanted to switch windows like you would a form.

    — answered April 2nd 2010 by Jason Brock
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.