Sign in to follow this  

[java] Question About Moveable Sprites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am setting out to make a small game based on an old megadrive game SmashTV. It consists of a static back ground image and a sprite which can move around the screen. At random intervals enemies enter the screen from the edges and attack the player. I am fairly competent at Java coding but I am not sure where to start with this. In perticular how to implement the movable sprite, and how it will overlay the background image. Here is what have got so far ... (Main loop of the Game class) private void mainLoop() { // Get an object representing the area within the window borders. Container clientArea = this.getContentPane(); // Get the Graphics object associated with the client area. Graphics g = clientArea.getGraphics(); // Make a new Player for this level. Player player = new Player(); // The main game loop ... // Should i put while(true)? It crashes my comp if I do :/ for (int a = 1; a <=1000; a++) { // Update player location. move vars are held as booleans ... if(moveright) player.x += 10; if(moveleft) player.x -= 10; if(moveup) player.y += 10; if(movedown) player.y -= 10; // draw background drawBackground(g); // call the player's draw method player.draw(g); // **** Will include buffering later to make graphics smoother. } } (Mouse listener method to get keyboard input, inside Game class) public void keyPressed(KeyEvent ke) { switch(ke.getKeyCode()) { case KeyEvent.VK_LEFT: moveleft = true; break; case KeyEvent.VK_RIGHT: moveright = true; break; case KeyEvent.VK_UP: moveup = true; break; case KeyEvent.VK_DOWN: movedown = true; break; } } (Draw method of the Player class, x,y coord stored within Player class) public void draw(Graphics g) { if (orientation == 0) { Image img = Toolkit.getDefaultToolkit().getImage("down.jpg"); } else if (orientation == 1) { Image img = Toolkit.getDefaultToolkit().getImage("left.jpg"); } else if (orientation == 2) { Image img = Toolkit.getDefaultToolkit().getImage("right.jpg"); } else if (orientation == 3) { Image img = Toolkit.getDefaultToolkit().getImage("up.jpg"); } // Not sure what to do here to draw graphic on screen, do I pass an instance // of Game to the Player class ? // g.drawImage(img,x,y,?); } Is this heading in the right direction ? Ive come a bit unstuck when it comes to drawing the player sprite from the player class, what do I use for the parameters ? Any comments appreciated ... Note from Lessbread: Moved from game programming forum and given a title

Share this post


Link to post
Share on other sites
You need to look into isometric games if you are trying to achieve 3D effects in a 2D game. Basically, you draw a bunch of tiles and any tiles the player ends up behind get drawn after the player is drawn. You need to keep a z-order to know what order to draw things in.

Share this post


Link to post
Share on other sites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this