Results 1 to 6 of 6
Thread: Java Sprite animation
- 10-02-2011, 10:00 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 37
- Rep Power
- 0
- 10-02-2011, 10:04 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Java Sprite animation
A suggestion is to have an array of images, and a counter which goes through them (this may not be the best approach, but it's my thought)
Java Code:private Image[] images = new Image[3]; private int current = 0; public Image getNextImage(){ if(current==images.length){ current=0; } return images[current++]; }
- 10-02-2011, 10:09 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 37
- Rep Power
- 0
Re: Java Sprite animation
ok so how could i do this in a loop?
- 10-02-2011, 10:11 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Java Sprite animation
Each time you want to draw to the image/panel/whatever you are drawing to, you can call this method. Maybe use a Swing Timer and have it continuously draw the next method.
- 10-03-2011, 12:32 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 37
- Rep Power
- 0
Re: Java Sprite animation
ok got it sorted thanks i used a swing timer to switch between frames
- 10-14-2011, 03:26 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 37
- Rep Power
- 0
Re: Java Sprite animation
Ok so the swing timer is taking up to much memory in terms of file space and the ram memory how can i load an animated gif on the drawimage thing i have used imageIcon but it fails to load lots of images at the same time ..
here is my scenario i have a player class and is created by another class serval times
Java Code:import java.io.*; import javax.imageio.*; import java.net.*; import java.awt.*; import java.awt.image.*; import javax.swing.*; public class player { private Image walk_0,walk_1,walk_2,walk_3; private Image still_0,still_1,still_2,still_3; private Image playerImage_walk; private Image playerImage_still; public boolean walking = true; public int direction = 2; //set Player Database Stuff public String playername = "dave"; private String gender = ""; public int mapid = 1; public int x_pos = 150; public int y_pos = 150; public player() { getPlayerimages(); } public void getPlayerimages(){ try{ //Walking Images ImageIcon walk0file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_0.gif")); walk_0 = walk0file.getImage(); ImageIcon walk1file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_1.gif")); walk_1 = walk1file.getImage(); ImageIcon walk2file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_2.gif")); walk_2 = walk2file.getImage(); ImageIcon walk3file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/walk_3.gif")); walk_3 = walk3file.getImage(); ImageIcon still0file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_0.png")); still_0 = still0file.getImage(); ImageIcon still1file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_1.png")); still_1 = still1file.getImage(); ImageIcon still2file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_2.png")); still_2 = still2file.getImage(); ImageIcon still3file = new ImageIcon( new URL("http://webserver.xorbo.com/Zombies/Players/"+playername+"/still_3.png")); still_3 = still3file.getImage(); }catch(Exception e){ e.printStackTrace(); } } public void drawPlayer(Graphics g) { Graphics2D g2d = (Graphics2D) g; Font f = new Font("SansSerif", Font.BOLD, 11); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g2d.setFont(f); g2d.drawString(playername,x_pos,y_pos-4); if(walking == true){ if(direction == 0){ playerImage_walk = walk_0; }else if(direction == 1){ playerImage_walk = walk_1; }else if(direction == 2){ playerImage_walk = walk_2; }else if(direction == 3){ playerImage_walk = walk_3; } g.drawImage(playerImage_walk, x_pos, y_pos,32,48, null); }else{ if(direction == 0){ playerImage_still = still_0; }else if(direction == 1){ playerImage_still = still_1; }else if(direction == 2){ playerImage_still = still_2; }else if(direction == 3){ playerImage_still = still_3; } g.drawImage(playerImage_still, x_pos, y_pos,32,48, null); } } }
Similar Threads
-
import sprite
By Bimz in forum New To JavaReplies: 0Last Post: 08-28-2011, 12:15 PM -
Sprite-sheet vs individual images
By David M. in forum Java GamingReplies: 9Last Post: 08-13-2011, 08:02 PM -
Confisuion about sprite.getX() and sprite.getY()
By Basit781 in forum Java GamingReplies: 0Last Post: 01-10-2011, 06:16 AM -
Sprite Movement
By Curtiz in forum Java GamingReplies: 1Last Post: 04-26-2010, 01:31 PM -
[j2me] sprite rotation with degrees
By Rooneyz in forum CLDC and MIDPReplies: 0Last Post: 07-06-2009, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks