|
-
January 21st, 2011, 11:21 AM
#1
How to repeat the scene from scratch in game programming
Hi to all,
Hope you all will be fine. I am making a cricket game. What is happening that game starts all the sprites position are set, the scene updated , ball move, when ball strikes with the fielder animation starts and etc. Now for second ball i want to repeat the same scene. Means when first ball starts and every thing has been done then for second ball the whole scene repeats from scratch. All the sprites restore their original positions for second ball and after six balls 1st over is bowled now second over starts.
How can i do it? I am using java Me. Here is my brief code in which i tell you what is responsible for my sprites position
Code:
public class DemoGameCanvas extends GameCanvas implements Runnable {
private int ballXFP ; // x position (as a MathFP)
private int ballYFP ; // y position (as a MathFP)
private int direction ; // current direction in degrees
private int xVelFP; // x velocity (as a MathFP)
private int yVelFP; // y velocity (as a MathFP)
private int maxVelFP;// = MathFP.toFP("2.0");
private int thrustFP ;
public DemoGameCanvas() {
super(true);
try {
//setFullScreenMode(true);
ballXFP = MathFP.toFP(140);
ballYFP = MathFP.toFP(100);
direction = 270;
thrustFP = MathFP.toFP("1.0");
maxVelFP = MathFP.toFP("2.0");
init();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void init() throws IOException {
timer = new Timer();
gameDesign = new GameDesign();
//Background image
cricketGround = gameDesign.getCrik();
//For all sprites positions
lm = new LayerManager();
gameDesign.updateLayerManagerForMatchGround(lm); // Responsible for all sprites position
ballImage = gameDesign.getBall5px();
ballSprite = gameDesign.getWhiteBall();
ballSprite.defineReferencePixel(3, 3);
.....
}
public void start() {
Thread t = new Thread(this);
t.start();
}
private void adjustViewPoint(int x, int y) {
//adjust the viewport
this.lm.setViewWindow(viewPortX, viewPortY, getWidth(), getHeight());
}
public void run() {
Graphics g = getGraphics();
while (!interrupted) {
updateScreen(g);
try {
Thread.sleep(5);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} // while (!interrupted)
} //run()
private void updateScreen(Graphics g) {
createBackGround(g);
for (int i=0; i<320; i+=10) {
g.setColor(255, 0, 0);
g.drawLine(0, i, getWidth(),i );
}
moveBall(g);
ballSprite.setRefPixelPosition(MathFP.toInt(ballXFP), MathFP.toInt(ballYFP));
playShots();
flushGraphics(0, 0, getWidth(), getHeight());
}
private void createBackGround(Graphics g) {
lm.paint(g, 0, 0);
}
private void moveBall(Graphics g) {
........
int dirRadians = MathFP.div(MathFP.toFP(direction), FP_DEGREES_PER_RADIAN);
//Calculate the accelaration for this cycle by multiplying the direction
//by the thrust on both x and y
int xAccFP = MathFP.mul(thrustFP, MathFP.cos(dirRadians)); // Vox = Vo * cos(thete)
int yAccFP = MathFP.mul(thrustFP, -MathFP.sin(dirRadians)); // Voy = Vo * sin(theta)
//increase the velocity by the amount of accelaration in this cycle
xVelFP = MathFP.add(xVelFP, xAccFP);
yVelFP = MathFP.add(yVelFP, yAccFP);
//Move ths ball according to its current velocity
ballXFP = MathFP.add(ballXFP, xVelFP);
ballYFP = MathFP.add(ballYFP, yVelFP);
.....
}
public void playShots() {
//Check for user input
int keyState = getKeyStates();
//if user is pressing the left button
if ((keyState & LEFT_PRESSED) != 0) {
if (lastDirection != LEFT) {
lastDirection = LEFT;
spriteShots.setFrameSequence(gameDesign.pullShot);
}
......
}
}
So please tell me how can i repeat the whole scene for the second ball.
Thank you.
-
January 24th, 2011, 11:45 PM
#2
Re: How to repeat the scene from scratch in game programming
So what is happening when you get to a point that you need the sprites to "reset"? Are they not resetting? If so what are to trying to do to reset them? I would think that a method that reset the sprite coordinates to some constant should be sufficient; checking for some reset state flag at the beginning of the paint method.
-
February 10th, 2011, 01:48 AM
#3
Re: How to repeat the scene from scratch in game programming
Hi ,
Thanks. Sorry for very very late reply . I have repeated the scene. You said right that just reinitialize all sprite positions after some time. But now i have to ask one question regarding game speed.
My question is how to control game speed. That is the game run with same speed on all platforms. when I run this game on Nokia hand sets the speed is not good but better. The first over run on very good speed but then game slows down. I know my animation thread is responsible for this(this is my first game on Java). Also no flickering on Nokia handsets. But when I run this game on black berry then the game run very slow and also there is a lot of flickering on blackberry handset.
Do you have any idea how can I control game speed. Any link or any help material regarding this and why on blackberry my game is run too slow and flickered.
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|