I have a couple of things I’d like to change in my recently published game, Flip it. Firstly, instead of having the arm rotate by 10 degrees, how do I make it so that once the arrow key is pressed, it rotates 180 degrees, clearly showing the rotation in the correct direction? Secondly, how do I make it so that when the right half or left half of the screen is clicked, the arm moves in the corresponding direction?
Can you share the part of your code where you handle rotation?
I imagine for your first point you can increase whatever you doing right now to have it rotate by 180 degrees, instead of 10.
For your second point, to find the direction of where your use clicked you can do something like this:
// initialize code called once per entity
Mouse.prototype.initialize = function() {
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
};
Mouse.prototype.onMouseDown = function (event) {
// If the left mouse button is pressed, change the cube color to red
if (event.button === pc.MOUSEBUTTON_LEFT) {
var side = event.x > this.app.graphicsDevice.canvas.width / 2 ? 1 : -1;
// when side is 1 he has clicked to the right, when -1 to the left;
}
};
Without stepping to your specific issue, I can see something fundamentally wrong in your implementation:
You are attaching new touch event handlers on each frame.
You should attach them once in your initialize method and they would fire as soon as input is detected. The way you do it now, apart from producing unexpected results can seriously impact performance.
Yes, it may be but I’d say at this point it’s a good step to refactor your code. Otherwise you are going to kill performance and make debugging quite difficult.
Also there is no reason to be running checkFunc() per frame since it is unlikely or impossible to change your user agent during gameplay. It should be enough to run it once in your initialize method.
Sorry for bringing other issues on the table, but it isn’t easy to debug the code when doing unorthodox things
It sounds like you want to make so that if you tap the left/right buttons, it rotates to 180 or -180 degrees over a period of time?
If so, I would set a target rotation whenever the user presses the buttons and have the rotation of the arm move towards the target rotation in the update.
No worries, should also mention you need the tween library from here:
Also has other easing methods instead of pc.Linear, they’ll change how your entity rotates, i.e. quickly moves to near the target and the slowly moves in, stuff like that. Play around with them and find one you like