I’m trying to make my first android game, just a pong clone really, I have a “PongView” class that extends SurfaceView and is my only view. It has objects of my “Ball” and “Paddle” classes. I just started moving code related to things, like detecting wall collisions, to the Ball and Paddle classes to tidy up my main view a bit and realised that i’d need to give these classes a way to know the views width and height. At the moment my work around is just to intialise a global variable inside the surfaceviews surfaceChanged method that stores the width and height of the view, like so:
//at the top of my class
private int viewWidth;
private int viewHeight;
..
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
//INITIALISE viewWidth and viewHeight here
//so that they can be passed as parameters
viewWidth = getWidth();
viewHeight = getHeight();
//some other irrelevant code here
}
Then I pass them as parameters:
ball.handleWallCollision(viewWidth, viewHeight);
However i’m not sure this is the way to go about it, as i will need to pass them in quite often i imagine. I thought it would be better if i had a copy of the current PongView in each class? But i’m not sure if thats true or how/when to go about getting it.
What would you recommend? Thanks
use object of surface view class that was used to setting view in activity.That object is live and will contain all changes regarding that surface view
Now you want to exit from that activity.Then in onDestroy() used
CommonClass.ObjectVraible=sur;
this line will save current state of the surfaceview