Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by cosmostail

Hi all,

It is a simple issue but I have spent hours and hours without make progress.. can someone help?

The issue was , "I need a way to detect whether an entity is clicked/touched".. I have made it works under impactJs by using the following code. The same code also works under AppMobi emulator... But once I pushed to the cloud and tested on iphone, it doesn't work anymore...

In main.js, I bind the Mouse1 event
ig.input.bind( ig.KEY.MOUSE1, 'my_click');

In my entity js, I have added the following code in update
update: function(){
if (ig.input.pressed('my_click') && this.inFocus()) {
//console.log('clicked');
this.kill();
}
},

inFocus: function() {
return (
(this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
(this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
);
},

First I thought it might be to do with directCanvas, so I did the following test, added a
<div id="Shootbutn" ontouchstart="handleInput('myTouch');" ontouchend="handleInput('myTouch');"></div>

and bind it in index.html as follow
AppMobi.canvas.execute('ig.input.actions[\'my_click\']=true;ig.input.presses[\'my_click\']=true;');

I'm pretty sure the touch event is fired because once I remove the this.inFocus() in updates, it did kill all my entities... but it seems the touch doesn't react right away or sometime requires few more touches...

At this moment, I'm really confused how the things work between impactJs and AppMobi...

1 decade ago by TylerAppmobi

If you put this code in your main.js on line 1 (so it has global scope), you'll have touch forwarding from the webview layer to the directCanvas layer.

var nX;
var nY;


Canvas.addEventListener('touchstart', function(event){
               
               nX=event.touches[0].pageX;//event.clientX;
               nY=event.touches[0].pageY;//event.clientY;
               
            }, false);

From there, just replace ig.input.mouse.x and y with nX and nY and that should work.

You can also insert an event you want triggered into the event listener. If you do this, then I'd recommend sensing for touchend also and turning that event off.

If there is any clarification needed, just ask :D
Page 1 of 1
« first « previous next › last »