I propose a separate rangedAttack() function that will go over the math behind the line of flight of an arrow and will actually launch the arrow. Unfortunately, we don’t actually launch an arrow and we end up shooting through walls. We actually have ranged attack functionality. Now when the player touches the screen onMouseUp() calls checkMove() and checkMove takes over with a call to basicAttack() or world:moveHero(). It only calls checkMove with an “attack” action if it’s a valid attack and starts a checkMove() “move” by first translating the touch into a single step dx, dy. Like I said, it’s a pre-function for checkMove(). If y > then dx, dy = 0, 1 else dx, dy = 0, -1 end If x > then dx, dy = 1, 0 else dx, dy = -1, 0 end Local visibleMapTouched = (event.x = 0 and x = 0 and y deltaY then Local y = + math.ceil(event.y / TILE_HEIGHT) - 6 Local x = + math.ceil(event.x / TILE_WIDTH) - 6 MOUSE_UP events generate event.x and event.y variables which we can use to limit the responses the function makes. Now, we just need that functionality when we touch the map. After all, checkMove() already does everything we want our program to do: have the hero look, move or attack. onMouseUp() will be a pre-function for Game:checkMove(). This addEventListener() command will call Game:onMouseUp(). Self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self) Let’s do that in our main Game:init() function as well. In the Start, Victory and Death scenes, we added an EventListener for touches.
We’ve actually thought about this before. We need a way to respond to touches directly on the map. At this point, the only way to shoot an arrow at something (or look at it or swing a sword at it) is to walk up to it and bump into it.
The problem is ranged attacks are no better than melee attacks. But otherwise, it’s fairly easy to integrate self.active into Game:checkMove(). It isn’t quite as simple as “do this” for each button because there are some default behavior we want our function to do such as attack monsters when it’s obvious they should be attacked. allow them to bump and melee attack when moving Self.msg:add("Not a monster", MSG_DESCRIPTION) Local entry, layer, tile = self.world:getTileInfo( + dx, + dy) first we need the tile info for where the hero wants to move Then we use self.active in Game:checkMove() to respond accordingly. We keep track of which button is active with the new self.active variable.
#Gideros event key down update#
That is, we’ll indicate which action is chosen by update its VisualState and turning off the others when the button is clicked. :addEventListener("click", function() self:turnOver() end) :addEventListener("click", function() self:checkMove(1, 0) end) :addEventListener("click", function() self:checkMove(-1, 0) end) :addEventListener("click", function() self:checkMove(0, 1) end) :addEventListener("click", function() self:checkMove(0, -1) end) The only addition is a center button that will directly call turnOver() so the hero can finish their turn without moving. To react to the buttons, the compass logic will be the same in Game:init(). Local down = Bitmap.new(TextureRegion.new(iconTexture, 144, 144, 144, 144))īefore we created four compass buttons, now we’re going to get a total of nine buttons on the screen. Local up = Bitmap.new(TextureRegion.new(iconTexture, 0, 144, 144, 144)) these variables are for the buttons that respond to user input
local iconTexture = Texture.new("images/texturepack-icons-144px.png", true) So let’s revisit a and get our new icon images on the screen. We can load our Texture once and when peel off parts we need for each button with TextureRegion.new(). Instead of having individual files for each image, I decided to put all the icons together in some image file.
So, after a quick visit to our favorite gaming icons website and some playing around in our favorite paint program, let’s import something texturepack-icons-144px.png into our program.Īs you can see, I decided to redo the compass buttons too. And by buttons, I mean two images, one to show the icon and another to highlight the icon. To make this happen, the first thing we’re going to need is icons. So we need to give the player the power to select which weapon to use and to click directly on the screen to fire an arrow. This is where we want to be with the hero using her bow to shoot an arrow at a goblin. Our player can direct the hero to wander around and bump into monsters, hoping to kill them with her sword. You want to do what? Where? – Getting all touchy feely – Death from a distance – Flight time – Jagged little lines You want to do what? Where?
#Gideros event key down download#
Previous tutorial – Download files – Next tutorial