Now that we move a Sprite, the ideal would encourage using multiple images one after another. To do this we must improve the Sprite class. The new upgrade features a couple of methods; resizeArray, setFrame and GetFrame. Is basically identical to the first one did, only this time working with an array that stores the names of image files that conform the animation. The operation differs slightly, if before the image we were assigned setSprite, now we can only assign a single image, because if we repeat the process with setSprite, we managed to store the next image and so on until we run out of memory . The variable responsible for displaying the image we want is the frame, which will be managed with the methods and setFrame GetFrame.
Code:
import java.awt .*;
javax.swing.ImageIcon import;
/ **
*
* @ Author TechArena
* /
public class SpriteTrial {
private int x, y, frame, / / coordinates
private boolean visible; / / if visible or not
SpriteTrial private String [] / / The name of the image
public SpriteTrial () / / Constructor
{
SpriteTrial = new String [1];
frame = 0;
visible = true;
x = y = 0;
}
isVisble public boolean () / / we indicate whether or not it is visible
{
return visible;
}
public void setVisible (boolean state) / / I will indicate whether it is visible or not
{
visible = state;
}
public int getX () / / Get the current horizontal coordinate SpriteTrial
{
return x;
}
public void setX (int value) / / Assign the current horizontal coordinate SpriteTrial
{
x = value;
}
public int getY () / / Get the current vertical coordinate SpriteTrial
{
return y;
}
public void setY (int value) / / Assign the current vertical coordinate SpriteTrial
{
y = value;
}
public int getWidth () / / Width of SpriteTrial
{
return new ImageIcon (getClass (). getResource (SpriteTrial [frame])). getImage (). getWidth (null);
}
public int getHeight () / / Height of SpriteTrial
{
return new ImageIcon (getClass (). getResource (SpriteTrial [frame])). getImage (). getHeight (null);
}
private static Object resizeArray (Object oldArray, NewSize int) {
java.lang.reflect.Array.getLength oldSize = int (oldArray)
Class elementType = oldArray.getClass (). GetComponentType ();
Object newArray = java.lang.reflect.Array.newInstance (elementType, NewSize)
preserveLength int = Math.min (oldSize, NewSize)
if (preserveLength> 0)
System.arraycopy (oldArray, 0, newArray, 0, preserveLength)
return newArray;
}
public void setSpriteTrial (String name) / / Assign the image file to SpriteTrial
{
SpriteTrial [SpriteTrial.length-1] = name;
SpriteTrial = (String []) resizeArray (SpriteTrial, SpriteTrial.length +1);
}
public void setFrame (int value)
{
frame = value;
}
public int GetFrame ()
{
return frame;
}
getSpriteTrial public String () / / It returns the image of the SpriteTrial file
{
return SpriteTrial [frame];
}
public void putSpriteTrial (Graphics graphics, int coordinateHorizontal, coordinateVertical int) / / Paste the SpriteTrial on the screen
{
x = coordinateHorizontal;
y = coordinateVertical;
if (visible) graph.drawImage (new ImageIcon (getClass (). getResource (SpriteTrial [frame])). getImage (), x, y, null);
}
}
Yes but, and animation? Worth it, the animation manually will work with the above methods, and GetFrame setFrame. But if you like to have a special function or method that does it all, and would have to work on the object or derivative Character Sprite, because not always all the characters or visual objects have the same behavior. So it is advisable to customize.
Bookmarks