Touchstart and touchend issue

I am facing issue with mobile touch .I have used touchstart and touchend events but it seems like not triggering. I also have attached my code below as well as screenshot of bird entity. How can I solve it ?.

var Bird = pc.createScript('Bird');


Bird.attributes.add('GAME', { type: 'entity' });
Bird.attributes.add('GAMEOVER', { type: 'entity' });


Bird.prototype.initialize = function () {
    this.flag = 0;
    this.isHit = 0;
    this.jumpVelocity = 7;
    this.maxJumpHeight = 4;
    this.velocity = 0; 
    this.isPressed = 0; 

    this.entity.element.on('touchstart', this.ontouchstart, this)
    this.entity.element.on('touchend', this.ontouchstart, this)
};



Bird.prototype.ontouchstart = function (event) {
    var pos = this.entity.getLocalPosition().y;
    if (pos < 3 && this.isPressed == 0 && this.entity.enabled == true) {
        this.entity.sound.play("Touch");
        this.velocity = this.jumpVelocity + this.velocity;
        this.entity.tween(this.entity.getLocalEulerAngles()).rotate(new pc.Vec3(0, 0, 25), 0.1, pc.Linear).start();
    }
};

Bird.prototype.ontouchend = function (event) {
    this.entity.tween(this.entity.getLocalEulerAngles()).rotate(new pc.Vec3(0, 0, -15), 0.1, pc.Linear).start();
};


Bird.prototype.update = function (dt) {
    this.entity.translate(0, this.velocity * dt, 0);
    this.velocity -= 9.8 * dt;
    if (this.velocity > this.maxJumpHeight) {
        this.velocity = this.maxJumpHeight;
    }

    var pos = this.entity.getLocalPosition().y;
    if (pos <= -2.0) {
        this.entity.sound.play("Die");
        this.GAME.enabled = false;
        this.GAMEOVER.enabled = true;
    }
};

Hi @gouthamamin,

How do you see that it’s not firing? Have you added any logs to your event callbacks to see if they fire or not? Make sure you have enabled Use Input on your element component in editor.

And also note touchend will fire when the touch will end on the element.

If you are able share your project or a sample project that reproduces your issue.

I have solved it. I just didnt enable Use Input in element component

1 Like