The proper way to create a 2d android game - android

After getting help with starting android pong, I realized that I painted myself into a corner.
For reference on the code(Its long) and advice, here is a link to the last thread Updating player score in an android game
Basically the frame rate is halved when i touch the screen. I think it is due to updating everything or drawing on the canvas. This also lends itself to bad communication between the pieces of the application(the view, frame, and oncreate). Is there a recommended way to create an android 2d game? This is based off the lunar landing game and it seemed fine but when i tried to recreate it , it seemed much worse. I could not find a lot of resource on a best practice for drawing and so far this seems a bit discouraging.
So in short
How would you modify the code posted in the link to improve performance? Or did I paint myself into a poorly made corner, and it is time to start a new? If so , how?
Thanks

You don't need invalidate() in onTouch because you are already running a frame animation by the additional thread.
Try to define all the fixed values/variables in the constructor of the SurfaceView or in onSizeChanged() where you know the size of the canvas and bitmaps loaded, so not to make any unneccesary variable creation, requests or calcualtions in onDraw, onTouch or other methods to handle game logic, otherwise it will slow down.

Related

How to draw cleverly a complex shape in Android

I'm working on a university project in which I need to visualize on a smartphone datas from pressure sensors built in an insole.
I need to draw on a View, as a background, a footprint, something like the image below but just for one foot.
I don't want to use a static image because with different screens resolution it could lose too much quality, so I'm trying to do it by code.
The main problem is that I'm not very skilled in graphic programming so I have not a smart approach to this problem.
The first and only idea that I had was to take the insole's CAD representation, with its real dimensions, scale it in function of screen's ones and put it together using simple shapes (arc, circle, ecc...) available in Android.
In this way I'm creating a Path which will compose the whole footprint, once I will draw it with a Canvas.
This method will let me to do the work but is awful and needs an exceptional amount of work and time to set every part.
I have searched for some similar questions but I haven't found anything to solve my problem.
Is there (of course there is) a smarter way to do this stuff, saving time and energies?
Thank you
of course, you can always use open gl es
this method might not save your time and energy but will give you a better graphic and scope to improve it for later purpose
the concept is to create a float buffer with all the points of your footwear and then draw is with connected lines on a surfaceView
to get started with open gl es you can use this tutorial : https://developer.android.com/guide/topics/graphics/opengl.html
and here is an example : https://developer.android.com/training/graphics/opengl/index.html

Android 2D animation on characters

I have created a character and I wonder how I have to animate it regarding the fact that :
It has to move when on static position, I mean for example eyes blinking, mouth closing.
It has to move on the screen regarding to player gesture.
It has to be involved in collision.
I read about, AnimationDrawable, SurfaceView, Canvas ... but which way is the best ?
Thank very much !!!
If you're trying to make a game you should consider GLSurfaceView with textures.
This is a pretty good tutorial, though it contains a few coding errors, it makes the concept very clear!
The OpenGLES method would give much much more freedom, than animating Views, but maybe harder to master.

Fuzziness in drawn frames (using canvas)

I'm hobby-writing a 2d game & I stumbled upon a certain problem.
My game is tile based (about 12x9 tiles) generally everything is fine, but from time to time when the character moves the frame becomes fuzzy and black segments appear.
My background is tile-based and I think it is caused by misplacing them.
In my opinion, it is happening because I have an update thread and a drawing thread that both work with the same variables, example:
private int x, y;
public void update() {x++; y += 2;}
public void draw(Canvas canvas) {canvas.drawBitmap(someImage, x, y);}
(Note: I just made this up so it will be a bit easier to understand what I am talking about.)
And the result is that there are (black) gaps between some of the tiles that are updated while drawn.
If I really am right, I guess I need to solve the critical-section problem.
So I tried just mutexing the data by syncing the threads and making them work in a serial way.
The result was really sad: it was REALLY slow on my galaxy S, absolutely unplayable!
My other idea (which I really don't wanna do) is to add a "drawAction" class to every drawable object in the game that will save a "snapshot" of it's object and only this collection of "snapshots" will be mutexed and so I can let the drawing thread do his work without being interrupted by the update thread.
Please share with me your ideas.
I hope you have better solutions or if I'm wrong about the problem, maybe you can help me gat to the source.
Edit:
My game generally works fine, but on slow devices (like the emulator) the interference is more visible.
When the player character doesn't move it is drown fine and almost always while walking.
I prefer to use only my code so I wold like to solve this problem and not just do a work around like using some engine or framework.
This doesn't directly answer your question, but it should help all the same. Consider using the game engine AndEngine. For 2D tile based games it is stupid smooth and really easy to learn.
The problem: I got a "writing" thread and a "reading" thread who used the dame source/destination.
I solved my problem by creating a "snapshot" or a "shadow" (technicly a copy..) of my mutual data right before the reading and hold the writing thriead while doing so, thus some of the read data was "old" but right to the same date with the rest.
The black things I saw were the differance in drawing (x, y) coused by updating the data while drawing it.

How to animate in Android Canvas?

I want to draw something at about 30 frames per seconds on Android Canvas or other convenient object for this purpose. In my application different graphic objects are drawn and if any of the graphic object is touched, the graphic object changes its shape. I looked at the
onDraw(Canvas canvas) callback of View subclass but calling invalidate() does not help here: first I cannot control the frame rate and second if the objects are moving too fast, the motion appears jerky.
I personally dislike Android's built-in Animation classes, so I tend to do all animations with Canvas by hand. I have found the most luck with creating a list of the images you want to use in your animation and then an int variable to store the current "frame" you are on. To advance the frame, I create a thread that sleeps for, say, 30 ms and then update the frame variable accordingly. Then in whatever update handler you are using, you can just create a switch statement or something of the like, and draw the respective frame.
It may seem like a lot of work, but it really isn't. Shove it all into a class and you will love yourself for many animations to come.

GridView with images vs custom drawn Canvas for Android memory game

Say I'd like to make a memory/pairs game. I have currently made a draft that works on a Canvas, and cards are drawn into a grid.
This works for my current basic version, but I'd like show do an animation (when the card is turned, it will flip around and scale to higher size; or when the match is found, the cards would rotate around and then go back.
I can't imagine doing this on Canvas, I'd have to make a lot of timers and do the animation by hand, it seems overly complex for this simple task.
I think I could could subclass View for a control that would display a card, and then react to touch events for that control. It would also make drawing scaling of the images done by Android itself, and, most importantly, I could use Tween Animation for some effects.
My question is - would it be OK to use a View for each card in the game (I could have 5x6 or 4x5 cards), and arrange them in a GridView? Are there some pitfalls with this approach? Or should I continue with completely custom-drawn Canvas?
For such a simple game you should be fine using a collection of Views. As you mention using Views rather than trying to do it manually you get access to a lot of nice Animation functionality for free.
It also makes implement the user interface a lot simpler as you can just add onClickListeners to each view to capture user touches. If you're drawing it all manually to a Canvas then you'd have to interpret the touches yourself and decide which card was touched etc. While this isn't too hard, then I think subclassing View is a better model and will most likely result in cleaner code.
As you are only going to have 30 cards, then I can't imagine you having performance issues either - if you were thinking 100+, then maybe you'd have an issue, but I think you're fine. Also, if I understand your game correctly, the majority of your cards won't be animating most of the time so that's yet another reason not to worry - if you ever run into performance issues with the animations you can easily save off all the unanimated Views onto a Bitmap (Canvas) for the duration of the animation.

Categories

Resources