Javascript & touch problem, maybe wrong software version?

hello
i have developed a game with cocos2d js 3.0 rc1. now, at the end i cant solve one problem: i want to use touch, to move the player, but all methods i tested doesn’t work.it gives an exception at this.setTouchEnabled, this.setToiuchMode etc. it doesn’t react on the eventlisteener. my other program verions are: ant 1.9.4, ndk r9d, adt / sdk with android 17 pack + all tools + all extras. cocos2d code ide. programming at windows 8 64bit for android: samsung s3. i followed many tutorials e.g. https://www.youtube.com/watch?v=iZNYL8aCSKk + even the downloaded files doesn’t work

why is it not working? the methods this.settouchenabled etc. results in a type exception while running. my code looks like this (short version):

var  GameScene = cc.Scene.extend({
    _meter:0,  //some variables, used in enter and update
    _bildhohe:-600,
    _label:null,
    _image:null,
    _audio:null,
    _panel:null,
    _platforms:null,
    _speedplus:1,
    _zahler:0,
    onEnter:function () {
        this._super();
        this.setTouchEnabled(true);  // if i comment this no error, but also no reaction
        var gamescreen = ccs.SceneReader.getInstance().createNodeWithSceneFile("res/publish/NewScene2.json"); //by cocos studio
        this.addChild(gamescreen, 0,1);
        this._audio = cc.AudioEngine.getInstance();
        this._audio.playMusic(res.music2, true);
        var player = gamescreen.getChildByTag(10009);
        this._label = gamescreen.getChildByTag(10005).getChildByTag(3).getChildByTag(13);
        this._image = gamescreen.getChildByTag(10005).getChildByTag(3).getChildByTag(4);
        this._panel = gamescreen.getChildByTag(10005).getChildByTag(3).getChildByTag(14);
        
        cc.eventManager.addListener(cc.EventListener.create({

                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: true,
                onTouchesBegan: function (touches, event) {
                    cc.log("Single touch has occured at A");
                    return true;
                }
        }),this);

        this.scheduleUpdate();
        return true;
    },                         
    onTouchBegan:function(touch, event)
    {
        cc.log("Single touch has occured at B");
    },
    update:function (dt) {
             //some updating code for the gui
    },
});

may i need to install cocos2d-x (not js) to use all classes?where could be the problem?

Hi @Kolonka,

We have delete the setTouchEnabled function from layer in v3.0.

You should use the new way to add the touch event: cc.eventManager.addListener, here is a sample in NewEventManagerTest.js:
// Make sprite1 touchable
var listener1 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget();

            var locationInNode = target.convertToNodeSpace(touch.getLocation());
            var s = target.getContentSize();
            var rect = cc.rect(0, 0, s.width, s.height);

            if (cc.rectContainsPoint(rect, locationInNode)) {
                cc.log("sprite began... x = " + locationInNode.x + ", y = " + locationInNode.y);
                target.opacity = 180;
                return true;
            }
            return false;
        },
        onTouchMoved: function (touch, event) {
            var target = event.getCurrentTarget();
            var delta = touch.getDelta();
            target.x += delta.x;
            target.y += delta.y;
        },
        onTouchEnded: function (touch, event) {
            var target = event.getCurrentTarget();
            cc.log("sprite onTouchesEnded.. ");
            target.opacity = 255;
        }
    });
    cc.eventManager.addListener(listener1, this._sprite);

I think we should add setTouchEnabled function to compatible v2.x.

Thanks for feedback.
David

Hi @ludingping!
Thanks for your answer! I tried it and the resulution is:

//cc.eventManager.addListener(listener1, this); //scene
//cc.eventManager.addListener(listener1, this._image); //imageview
//cc.eventManager.addListener(listener1, this._panel); //panel
cc.eventManager.addListener(listener1, this._label); //label
//cc.eventManager.addListener(listener1, gamescreen.getChildByTag(10005)); //GUIComponent

the touch listener works only on the label.which it is dependent, that the touch listener works on a node? is there a way to add it to eg. the panel?

@edit:
now i know the problem: the label is just in the corner, but i don’t know why, it is on fullscreen, so i only can touch the label -_-. so right now, i can touch the label, but i want to move some other object: this._player.

onTouchMoved: function (touch, event) {
      var delta = touch.getDelta();
      this._player.x += delta.x;
}, 

this doesn’t work because i cant access to the player.i try to find out, how to access to this global variable. if maybe sb. other knows, please tell me to make it faster :smiley:

@edit:
mission accomplished! special thanks to @ludingping