I am making a game of Gomoku in Android. I have made a grid out with drawLine(): http://i.stack.imgur.com/Mm4HZ.jpg
I am now trying to make the app detect presses within the small squares. I was thinking perhaps I could generate buttons to be in the center of each square. I have the height/width of a single square saved to varibles so getting the center points to each square shoudln't be difficult. However, how should I dynamically generate buttons where each corresponds to a certain [i][j] in a 10 * 10 matrix where the field data is stored?
You can register an onClickEvent with any view in android. What you want is an invisible view the area of the places were you want click events. I suggest using buttons or something visual so that you can see where the areas are, register the click events for each view, then hide the views so that they are invisible.
Related
I wan't to develop game named "Balda".
I have a 2d grid of imageviews (or buttons, maybe).
User should be able move his finger on the grid, and app should know wich images in grid was touched in this move.So, in the picture below is what I'm trying to achieve. A - is start point where user pressed on screen. B is end point where finger leaves the screen. And I need to know what images were touched (There are blue in the picture).
I know that I can do something like this. But I think that this is a wrong solution, because It contradicts the principle of giving functionality by responsibilitys.
I think that it is responsibility of the imageView to know when finger enters its borders and when it is leaving its borders.
I thought, that this would be in android API. And it has MotionEvent like EVENT_HOVER_ENTER and EVENT_HOVER_LEAVE but it's not working with finger. After finger is pressed on some View it will recieve all other MotionEvents, if I get it right.
I think that this is wrong. What can I do to get this functionality? Maybe I could create some custom listeners and custom Views, that supports them?
I think your requirement is slightly similar to custom gridview.
You can try below steps-
1)Create Custom view
2)Attach TouchListener to it.
3)Divide this view into 4*3 matrix.
4)Map your images to this 4*3 matrix
5)Write a function which gives the cell number respective toTouched Co-ordinates
6)After getting cell number;get the mapped image for that cell number
7)Put this image in arraylist
8)When user lifts his finger you will get arraylist of touched images(do whatever you want with it).
9)Remember to put this custom view in your activity
Tell me,if you have any doubt or concern
I want to create an interactive graphic for my app. It will essentially be a simple picture of a bus line where users can select 2 stops at a time (one for departure times and another for arrivals) I'm not sure how to create this image though, and have it have 20 or so different clickable points. Is there a framework I could use for this? Or is there a way to do this in pure android?
Thanks for the help.
I would suggest writing an onClick listener and using a collection of Rect instances to manage the collision/position of the 'click'. Check out the on click page and the rect page.
One thing to keep in mind are the origin point of your clicks, I'd assume you'll want to use one corner of your image as the point (0,0) and reference everything (clicks and rects) from there.
I would say try to create an ImageView to load your image and set a touch event listener or a click listener to that view. Hard code all places that you want your image to react upon a click.
Check click using an event listener would require you to handle both ACTION_DOWN and ACTION_UP in the MotionEvent object passed in. But it's easier to grab the coordinate of where the user clicks on the page, so you only need one listener but needs to put more work on handling the conversion from the coordinate passed in by MotionEvent to the coordinate on image. This is particular a major issue when your image can be sized larger than the screen size.
Using a click listener would save you from this trouble. As #smitec said, you need to overlay rectangles on your image as "buttons", so you can react to user input based on which buttons they pressed. This way you need to bind listeners to all of them (I suppose) and hard code their positions on your image. But, as mentioned earlier, it saves you from dealing with coordinates later on.
I am currently working on a Connect four game.
My game works by the user pressing the 'New game' button. This then draws 42 (7*6) green circles to screen. These circles are used to represent holes on the connect four board i.e. green circles currently do not contain a player's token/counter
At this moment in time I am working on adding the tokens/counters. I have got code working (to a certain point) that enables the user to pick a column. Using log.d() It seems that I am successful changing the colour value of the gaps. My problem is that I do not know how to send this changing in colour to the View i.e. so the gap actually changes from green to red (player's token colour).
How can I send a request to the View so the gap is re-drawn?
N.B. Sorry for the lack of code but it is currently messy and not fully working.
The View is controlled by a ConnectFourView.java (View), the game screen is Board.java (Model) and the code that works out which column has been selected Gaps.java (controller)
Just call invalidate() on your view and it will be redrawn
I am developing a puzzle game, where user has to arrange the images in the grid. The screenshot is given below
I want to able to drag the image of one cell of the grid to another cell. I searched many sites and every where I found examples with Drag and drop API (I.e. by using OnDragListener etc.) which was introduced in Android 3.0, but my application should run in Android 2.2.
So please help me how to implement it using Touching API (I.e. OnTouchListener etc.)
One way of doing it would be take the x & y location of the touch in relation to the grid.
IE. at 10x10 grid on a 100x100 area.
If the touch was at 25,25, it would choose square 2,2(using an array). You could then save that location to a variable (so as to move whatever piece to it that you are changing it with) and on drag update the bitmap x,y in relation to the touch.
Once you lift your finger at say, 75,75, it would set the puzzle piece at 7,7 and move that piece to 2,2.
I used something similar, less the drag, on my Lazer Maze Lite game. Mine was basically moving mirrors and bombs on touch though, but....
http://developer.android.com/guide/topics/ui/drag-drop.html
I need your advice on the best way to implement a scrollable playing field in a simple game. The field consists of multiple rows, with 9 cells in each row. The rows can be dynamically added and removed during the course of the game. Each cell has a digit in it, and optionally several overlayed images, like selection, selector over, crossed out, etc. (see the pic below). The user should be able to click on individual cells of a row to select/deselect/cross the touched cells. At any point in the game there can be from 0 to 3000 cells (0 to about 333 rows). The field should scroll up and down smoothly.
I was thinking about having a ListView with its each row being a row on the field. That way i could add/remove rows dynamically during the game. However, how exactly should i implement a row: have one bitmap representing a row, and then when the user touches it -- get the coordinates of the touch area, and find out what cell was affected, and then act upon that. Is it possible to get the touch coordinates of a row in ListView? If not, should I place 9 dummy image placeholders in each row, and then act on user touching those? What about performance?
Or should I have one huge bitmap / canvas representing the entire field, place it inside a ScrollView and then calculate all the coordinates and update it as the user interacts with it? Is it going to be faster or slower than the ListView?
Any other solutions?
I prefer it to be a "regular" app type game, not a loop-based game, as I don't think I really need to redraw 30 times a second.
I am pretty new to Android. Thank you for your suggestions.
You can easily make that kind of setup run quickly with a "game loop" / SurfaceView combination. That means no ListView, only custom drawing and event handling. Luckily the event handling isn't that difficult, and you'll win later on because you'll have much greater control over the interface than if you had gone with a bunch of customized views and layouts.
I'd avoid that www.droidnova.com tutorial, it uses HashMaps unnecessarily, which will only cost you in performance.
Here's how I'd go about it:
Create an object to hold your cell data. For this example, I'd use an object with an id (to print), and an x and y for where to draw on the screen.
Decide on a board size which will fit on your screen without scrolling, say 10x10. You'll add the scrolling later.
Create a 2-dimensional array of cell objects with lengths boardSize x boardSize. Fill the objects with id and x and y position on the screen.
In your custom onDraw, iterate through each "row" and "column" of your single array and draw the object at its stored x and y value.
So now you've got a grid of objects displaying on your screen. Now you want to restrict the number of rows currently displayed and add some functionality to change which rows are visible. This is done as follows:
During initialization, set up some global ints as mCurrentRow = 0 and mNumVisibleRows = 3. These define your "view window".
Modify your drawing code to only draw rows starting at mCurrentRow and ending at mCurrentRow + mNumVisibleRows. The result of this should be that you only ever see mNumVisibleRows rows, and which group of rows you see depends on what you set mCurrentRow to.
Add some triangles to the right of your grid drawing and have tap touch events in those areas map to increments/decrements of mCurrentRow. Obviously, you should not allow that value to go outside your row count bounds.
If you want to get classy, draw a line between your triangles for a scroll area, and map touch events there to something like newCurrentRow = (touch.y / canvas.height()) * boardSize; That needs some tweaking, but you get the idea.
This approach has the downside of only showing a full set of rows at a time, so scrolling wouldn't be smooth. However, you have complete control over your canvas and what you draw, so with a little more math you could implement a smooth scrolling mechanism which would offset by a fractional row height in the y-direction instead of whole rows.
I dont know if you can create a game like you described with good performance. I would look into basic tile game programming.
But by avoiding the standard view components are have to write all the logic yourself requires quite some work. Things like handling "click" events on different rows needs to be calculated by the tile position relative to the game camera. So theres alot of new stuff in learning to develop game at a lower level.
You also have to take the rendering of your game into your own hands by developing a game loop that constantly updates and draw's your tiles from a background thread to reflect the scroll / state of your game.
You can get more infomation on the basics of a game loop at:
http://www.rbgrn.net/content/54-getting-started-android-game-development
If you want to learn more you should see the following keynotes from android.
http://developer.android.com/videos/index.html#v=U4Bk5rmIpic
http://developer.android.com/videos/index.html#v=7-62tRHLcHk
Those give you a very good insight in developing games for andriod at a low level where you can fine tune for performance.