I have a question about the character movement when the mouse is clicked in the map. I created a Sprite_Button on the map and placed it exactly on Spriteset_Map. When you click the sprite button it takes you somewhere, it works fine. My problem is that when I click it, my character moves to that direction as well. Any way to distinguish the sprite button as button, so when I click it my character would stay on its position and not recognize it as a mouse gesture for movement?
Movement and Clickable Events in Map
Learning JavascriptYou can probably create some variable in Game_Temp, and add a condition inside the .isDestinationValid() that the new variable also has to be true. And in your button class set that new variable to true or false if the mouse is over it, using the .isButtonTouched() from the button class. If you're dealing with multiple buttons you could probably make that new variable an array, and every time you hover over a button add the button in that array only if it isn't already in it, and when you're not over it, remove it only if it's inside the array. Then in the .isDestinationValid() check if that array length is 0.
You might also be able to just use the $gameTemp.clearDestination() when ever you're hovering over your button. But that would probably depend on which class gets updated first, the player or the button.
I had to deal with this when making a touch hud. My approach is almost the same as my first idea, but instead I put the "canClick" variable inside Game Player, and added the new condition inside Scene_Map.prototype.processMapTouch() since I was already overriding it in my movement plugin.
You might also be able to just use the $gameTemp.clearDestination() when ever you're hovering over your button. But that would probably depend on which class gets updated first, the player or the button.
I had to deal with this when making a touch hud. My approach is almost the same as my first idea, but instead I put the "canClick" variable inside Game Player, and added the new condition inside Scene_Map.prototype.processMapTouch() since I was already overriding it in my movement plugin.
It works well now, it steps 1 tile though. What I did was to overwrite isDestinationValid that when the new variable from game temp is false then it would work the default way, when true it would just do nothing. I'll try to experiment what causes the character to move one tile before stopping the movement.
You might also be able to put it inside the Game_Temp.prototype.setDestination function and don't set the values if that variable is true. But it might probably be the same as putting it inGame_Temp.prototype isDestinationValid, but would be worth a try.
The character still moves one tile. Here's how I did it:
Game_Temp.prototype.setDestination = function(x, y) { if ($gameTemp._menuButtonClicked) { this._destinationX = 0; this._destinationY = 0; } else { this._destinationX = x; this._destinationY = y; } };and in the move by Input, I did this:
Game_Temp.prototype.setDestination = function(x, y) { if ($gameTemp._menuButtonClicked) { this._destinationX = 0; this._destinationY = 0; } else { this._destinationX = x; this._destinationY = y; } };and in the move by Input, I did this:
Code:
Game_Player.prototype.moveByInput = function() { if (!this.isMoving() && this.canMove()) { var direction = this.getInputDirection(); if (direction > 0) { $gameTemp.clearDestination(); } else if ($gameTemp.isDestinationValid()){ if (!$gameTemp._menuButtonClicked) { var x = $gameTemp.destinationX(); var y = $gameTemp.destinationY(); direction = this.findDirectionTo(x, y); } else { $gameTemp.clearDestination(); } } if (direction > 0) { this.executeMove(direction); } } };
Well you should set the values to null not 0.
Also I think I was having that issue too, and that's probably why I put the condition check inside Scene_Map in the end and did something like:
Scene_Map.prototype.processMapTouch = function() { if ((TouchInput.isTriggered() || this._touchCount > 0) && $gamePlayer.canClick()) { // reset of stuff }}but you could probably alias it like:
Also I think I was having that issue too, and that's probably why I put the condition check inside Scene_Map in the end and did something like:
Scene_Map.prototype.processMapTouch = function() { if ((TouchInput.isTriggered() || this._touchCount > 0) && $gamePlayer.canClick()) { // reset of stuff }}but you could probably alias it like:
Code:
var someAlias = stuffstuff;Scene_Map.prototype.processMapTouch = function() { if ($gameTemp._menuButtonClicked) return; someAlias.call(this);}
Hmmm, it still moves one pixel depending on the direction. This is how I roughly translated your help:
Code:
Game_Temp.prototype.setDestination = function(x, y) { if ($gameTemp._menuButtonClicked) { this._destinationX = null; this._destinationY = null; } else { this._destinationX = x; this._destinationY = y; } }; Game_Player.prototype.moveByInput = function() { if (!this.isMoving() && this.canMove()) { var direction = this.getInputDirection(); if (direction > 0) { $gameTemp.clearDestination(); } else if ($gameTemp.isDestinationValid()){ if (!$gameTemp._menuButtonClicked) { var x = $gameTemp.destinationX(); var y = $gameTemp.destinationY(); direction = this.findDirectionTo(x, y); } else { direction = this.findDirectionTo(null, null); $gameTemp.clearDestination(); } } if (direction > 0) { this.executeMove(direction); } } }; Scene_Map.prototype.processMapTouch = function() { if (TouchInput.isTriggered() || this._touchCount > 0) { if (TouchInput.isPressed()) { if (this._touchCount === 0 || this._touchCount >= 15) { var x = $gameMap.canvasToMapX(TouchInput.x); var y = $gameMap.canvasToMapY(TouchInput.y); if (!$gameTemp._menuButtonClicked) { $gameTemp.setDestination(x, y); } else { $gameTemp.setDestination(0, 0); } } this._touchCount++; } else { this._touchCount = 0; } } };
You shouldn't need that Game_Player.prototype.moveByInput function since you have the scene map one.
Try this ones for the scene_map:
Scene_Map.prototype.processMapTouch = function() { if ((TouchInput.isTriggered() || this._touchCount > 0) && this._menuButtonClicked) { if (TouchInput.isPressed()) { if (this._touchCount === 0 || this._touchCount >= 15) { var x = $gameMap.canvasToMapX(TouchInput.x); var y = $gameMap.canvasToMapY(TouchInput.y); $gameTemp.setDestination(x, y); } this._touchCount++; } else { this._touchCount = 0; } }};That's the way it worked for me.
You also used "$gameTemp.setDestination(0, 0);" in the proccessMapTouch function which might be causing the move, instead you should not set anything or use $gameTemp.clearDestination();
Try this ones for the scene_map:
Scene_Map.prototype.processMapTouch = function() { if ((TouchInput.isTriggered() || this._touchCount > 0) && this._menuButtonClicked) { if (TouchInput.isPressed()) { if (this._touchCount === 0 || this._touchCount >= 15) { var x = $gameMap.canvasToMapX(TouchInput.x); var y = $gameMap.canvasToMapY(TouchInput.y); $gameTemp.setDestination(x, y); } this._touchCount++; } else { this._touchCount = 0; } }};That's the way it worked for me.
You also used "$gameTemp.setDestination(0, 0);" in the proccessMapTouch function which might be causing the move, instead you should not set anything or use $gameTemp.clearDestination();
Okay, the character does not walk when clicked, but there is another problem. It seems I can't use the mouse tap now.
You could have taken a look at how Dekita does it in his plugin: http://forums.rpgmakerweb.com/index.php?/topic/46944-dmv-map-buttons/
Specifically this part:
"temp" is Game_Temp.prototype.
Specifically this part:
Code:
temp.setDestinationNotaButton = function(){ var bt, ix, iy; var x = TouchInput.x; var y = TouchInput.y; var s = this.map_buttons.length; for (var i = 0; i < s; i++){ bt = this.map_buttons[i]; ix = (x >= bt.x) && (x <= bt.x + bt.width); iy = (y >= bt.y) && (y <= bt.y + bt.height); if (ix && iy) {return false}; }; return true;};var setDest = temp.setDestination;temp.setDestination = function(x, y) { if (this.setDestinationNotaButton()){ setDest.apply(this, arguments); };};
Original thread: https://forums.rpgmakerweb.com/threads/movement-and-clickable-events-in-map.54046/