|
-
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.
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
|