Im trying to plan out the best way to develop a sample game. I would like to create a board game that act like words with friends (not game rules). For example, I want the board to be a six sided board that has a set number of game tiles. When a game piece (like a checker or ball) is placed on it, i would like to have that piece "snap" to the closest location. My question is, what is the best way to do design the board? Should this be done as a background image? Or draw the board live? If so, how do i create the "snap to" and register when a piece is on the board? I also want to make sure that the board is drawn to the correct dimensions for different phones.
Thanks for any suggestions
jason
This is probably a bit to broad of a question for SO. But I'll give it a go.
Little bit of preface, I have not personally attempted what you are trying. So please do not take what I suggest as what you must do.
If I were you I would create a GameBoard object and a GamePiece object. Put everything to do with making and holding information for the board in the GameBoard class. Whether you draw your board in java, or start with a graphic and build from there depends on a few different things. How specialized would you like the board to look?(you'll have more control if you start with a graphic if you want to make it fancy) If you're just looking for a grid of lines and nothing fancy I'd think you'd be fine to just draw it from java. Do you ever want to use more/less than 6 rows/columns? If you want to use a different number it may be easier to do that when the time comes if you do your drawing from java rather than from a stored graphic. Your game board will also need to be able to keep track of which pieces are on it and where they are.
As for the snap-to: You'll be creating a touch listener that allows you to drag a GamePiece along under your finger. inside of the Finger_Up event in your listener you'll check the piece's current Rect against the Rect's on the GameBoard and drop into whichever slot it intersects most. Fair warning while you are creating your touch listener you're going to have to use some very nitty gritty linear algebra to take raw motion events from the touch screen and tell it where to draw the piece next.
Related
(I tried to stuff the question with keywords in case someone else has this issue - I couldn't find much help.)
I have a custom View in Android that contains an LED bargraph that displays levels received via socket communication. It's basically just a clipped image. The higher the level, the less clipped the image is.
When I update the level and then invalidate the View, some devices seem to "collect" multiple updates and render them in chunks. The screen visibly hesitates for say 1/10th of a second, then rapidly paints multiple frames, and then hesitates again. It looks like it's overwhelmed and dropping frames.
However, when changing another UI control on the screen, the LED bargraph paints much more frequently and smoothly. I'm thinking Android is trying to help me by "collecting" multiple invalidations and then doing them all at once. Perhaps by manipulating controls, I'm "increasing" my frame rate simply by giving it "more to do" so it delays less between actual paints.
Unlike animation (with smooth transitions) I want to show the absolute latest value as quickly as possible. My data samples aren't faster than 10-20fps anyway.
Is there an easy way to "force" a paint at certain points, or is this a limit of how Views work? Should I be implementing this in a SurfaceView instead? (I have not played with that yet... want advice first.) Thanks in advance for suggestions.
(Later that same day...)
Update: I found a page in the Docs that does suggest implementing my widget as a SurfaceView is the way to go:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
(An hour after that...)
SurfaceView seems overkill for what I want to do. The best-practice method is to "own" the whole canvas, but I have already developed the rest of my controls and layouts and they work well. It must be possible to get some better performance with what I have, especially since interacting with the UI makes the redraw speed satisfactory.
It turns out SurfaceView was the way to go. I was benchmarking on an older phone which didn't help. (The frame rate using a standard View was fine on an ASUS eeePad). I had to throw away some code, but the end result is smoother and faster with SurfaceView. Further, I was able to re-use more code than I expected and actually dramatically simplified my multitouch handling code (since everything I want to touch is in the same SurfaceView.
FYI: I'm still only getting about 15fps on Droid X, but half of the CPU load appears to be data packet processing. The eeePad is doing almost 40fps now -- and my data rate is only 20 samples/sec.
So... a win I guess. I want the Droid X to run better, but it flies on a real tablet.
In my application I need to draw a large network (basically, little boxes connected with lines) and the user will be able to zoom and pan it around.
My first option was to draw the network directly to the canvas, but I thought that was not very efficient, because each time a pan event occurs, the drawing process begins again.
So I tried to use a large mutable bitmap and draw my entire network only once (or at least whenever zoom occurs), and blit the necessary areas to the canvas.
My problem is, since the network is rather big, I get OOM exception when creating the bitmap…
What should I do? Draw directly to the canvas? Use several smaller bitmaps?
Thanks,
Direz
My first question to you is how many sprites you have going at once? By far, the fastest mechanism for having many sprites on the screen is to use OpenGL due to the hardware accelleration. The besy way, on Android, I have found to do this is to use the Cocos2d android (not to be confused with the ios version) which is available on Google Projects. You will have to use the IOS documentation in order to understand it though and there are a few decent tutorials to get started with online..in particular, the hello world template here... Www.sketchydroide.com/Blog/p?=8. It is out of date compared to the latest IOS cocos2d but thats to be expected. I have found that the programs run MUCH faster when not connected to an active debugger session in my experiments.
If you want to stick with your current approach or the above is still not fast enough, you are going to have to attempt to cull any drawing which does not appear n the screen meaning a general function of the form "if the sprite's x and y values are outside the bounds of the visible area, dont draw it" which is basically how most tile base games handle the issue.
It sounds like you are doing the drawing manually if you are doing little squares. I think it is more adviseable to go ahead and draw on the canvas but to be very careful about managing your sprite counts and to avoid heavy for loop iteratiins that occur on
the frame update loops where possible. It is rather easy to max out your little handset with drawing operations.
Another option might be to draw the entire bitmap once into memory and then use a copy rectangle operation to transfer the image to the screen without drawing the full bitmap you have created. I think that copy rect should be a fast operation normally but if you are using it to draw the whole screen it seems like overkill and probably wont work as well.
You're probably not going to like this, but if all you're doing is drawing boxes and lines, the efficiency of the canvas is going to be pretty dang high. Are you getting UI lag or something?
One thing I have messed around with is drawing collections of subcomponents that won't change much or at all to a bitmap then rendering (scaling/moving aren't all that expensive if done at the right level) to the canvas can help efficiency. I have tried in the past to create a framework for rendering a tile-like subset of an existing larger image, but did not meet much success. I've made things work, but the code just gets ugly.
Oh, also a quick test to see if the component you are rendering is within the rectangle created by the screen can save you a bunch of processor time.
ok, I'm going to ask this because I really can not find an answer, after scouring for hours.
I'm making an android game, and I have a gameboard 'view'. My gameboard view has a few things - it has a background image, and then it has a grid of board pieces ontop of it.
I want the user to be able to double tap to zoom in, and then pan around the board.
I've been able to apply a transform in the past to get this effect (on other platforms) but I'm not sure how to do this in Android (I'm still new) - does anyone know how to apply transforms or achieve the above effect, WITHOUT me having to calculate the position of everything in my view?
Please dont suggest Canvas - I want my game pieces to be objects, and I want to be able to listen for 'on touch' events (AKA I do not want to have to figure out what the user touched when they touch the screen). I also want this for Android 2.1+
Thanks for any help ahead of time!!
Unfortunately doing this with views does require recalculating sizes and seems very tedious. I'm sure you know that Canvas has various matrix transformations one can apply.
If this functionality is important to you, I would think that figuring out how to have your Canvas objects receive touch events is the easier of the two paths, given that most games using the Canvas will also be receiving touch events and correlating them with the underlying game models.
I am a new Android developer, and though I have spent a LOT of time reading through the official developer guide/reference (as well has hours of guessing/checking), I'm still stumped about the best way to do my layout. I am creating a board game, and here is an image of the game board. On that board, the game pieces live on (and move between) the small inner hexes. From my limited understanding of Android development, I'm thinking I need to make each small hex a view. And the overall board layout would be a stacking together of these small hex images to form the board shape. The end result would look exactly like the gameBoard image I linked above. IMPORTANT: I want the game board to be on the bottom of the screen, with game info (players, scores, etc.) at the top. For now I am focusing on a portrait layout...I'll deal with landscape mode later.
The trouble I'm having is figuring out how to do the layout. I want to use XML tags for this portion of the layout, because the board itself will never change during the game play. Only the game pieces will move around on the static board. I have spent hours so far researching and trying different layout types, and combinations of different layouts...nothing I've tried gives me that board design. As you can see from the board image, the hexes don't line up into nice rows/columns. I think this is why I am struggling to get my small hex views to look like the board. Any ideas? Or am I going about this in the wrong way altogether?
You say the board will not change, then why don't you use a single (vector) image to represent the whole board? I am not sure if android supports SVG though
I'm having trouble porting a simple game I developed for the iPhone
over to Android. The game has an animated ball which moves from Point
A to Point B. The user must touch the ball before it reaches point B
or lose the game. This was easy to implement on the iPhone using Core
Animation since I could locate the current position of the ball by
accessing its animation layer. In Android, I attempted to recreate the
game using tweened animation and represented the ball as a Drawable.
My issue is that I can't determine if the user is touching the spot
because the Drawable apparently bounds do not update as the ball
visually moves - making the program think the ball is always in its
original position. While searching these forums I saw an Android team
dev. confirm that you can't get the current location in a tweened
animation but offered no solution for a workaround. Can I accomplish
this on the Android using my current approach? If not, what approach
should I use?
Best regards,
Michael
Can I accomplish this on the Android
using my current approach?
I doubt it. Android's tweened animations are not really designed for games.
If not, what approach should I use?
Implement the game using 2D drawing on the Canvas -- see the LunarLander sample in your SDK.